You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
#!/usr/bin/python
|
|
|
|
import re, urllib, urllib2, argparse, os
|
|
|
|
parser = argparse.ArgumentParser(description='Downloads all full-size images in an arbitrary 4chan thread.')
|
|
|
|
parser.add_argument('urllist', metavar='url', type=str, nargs=1,
|
|
help='the URL of the thread')
|
|
parser.add_argument('-n', '--newdir', dest='newdir', action='store_true',
|
|
help='create a new directory for this thread in the current directory')
|
|
|
|
args = parser.parse_args()
|
|
options = vars(args)
|
|
|
|
regex = 'href="(\/\/images\.4chan\.org\/b\/src\/[0-9]+\.[a-z]+)"'
|
|
url = options['urllist'][0]
|
|
|
|
try:
|
|
page = urllib2.urlopen(url).read()
|
|
except ValueError:
|
|
print "That does not look like a valid URL."
|
|
exit(1)
|
|
|
|
if options['newdir'] == True:
|
|
thread_id = url.split('/')[-1]
|
|
target_dir = "%s/" % thread_id
|
|
|
|
if not os.path.exists(thread_id):
|
|
os.makedirs(thread_id)
|
|
else:
|
|
target_dir = ""
|
|
|
|
search = re.compile(regex)
|
|
matches = search.finditer(page)
|
|
|
|
urls = []
|
|
|
|
for match in matches:
|
|
if match.group(1) not in urls:
|
|
urls.append(match.group(1))
|
|
|
|
current = 1
|
|
total = len(urls)
|
|
|
|
print "Parsed thread. Total images: %d" % total
|
|
|
|
for downloadurl in urls:
|
|
downloadurl = "http:%s" % downloadurl
|
|
filename = downloadurl.split('/')[-1]
|
|
path = target_dir + filename
|
|
urllib.urlretrieve(downloadurl, path)
|
|
print "Downloaded %s (%d/%d)." % (filename, current, total)
|
|
current += 1
|