Enjoy ✌
Code
#4chan image downloader with thread-rechecking, gif inclusion/exclusion
from bs4 import BeautifulSoup
import urllib2
import time
import os
def imli(url, anim, timer, savepath):
print "[*] I'll not die til thread is 404'ed :-)"
count = 0
while True:
try:
content = urllib2.urlopen(url)
except urllib2.HTTPError, e:
if e.code == 404:
print "Oops...Thread 404'ed!"
return
board = url.split('/')[3]
if board == 'b':
folder = savepath + '/random/' + url.split('/')[-1]
elif board == 'g':
folder = savepath + '/technology/' + url.split('/')[-1]
elif board == 'w':
folder = savepath + '/animeWallpapers/' + url.split('/')[-1]
elif board == 'wg':
folder = savepath + '/wallpapers/' + url.split('/')[-1]
elif board == 'g':
folder = savepath + '/literature/' + url.split('/')[-1]
else:
folder = savepath + '/board/' + url.split('/')[-1]
if not os.path.exists(folder):
os.makedirs(folder)
html = content.read()
content.close()
soup = BeautifulSoup(html)
links = soup.findAll('a', {'class': 'fileThumb'})
fimg = []
for l in links:
iu = 'http:' + l.get('href')
if not anim:
if iu.split('.')[-1].upper() != 'GIF':
fimg.append(iu)
else:
fimg.append(iu)
for i in fimg:
img = i.split('/')[-1]
if not os.path.exists(folder + '/' + img):
try:
iurl = urllib2.urlopen(i)
f = open(folder + '/' + img, 'wb')
meta = iurl.info()
size = int(meta.getheaders("Content-Length")[0]) / 1024
count += 1
print "%s -> %s : %s KB" % (count, img, size)
f.write(urllib2.urlopen(i).read())
f.close()
except urllib2.HTTPError, e:
print "Image not found...Thread 404'ed?"
time.sleep(timer)
return
imli(raw_input('Enter thread url:'), False, 15, 'd:/4chan')
raw_input('~Press any key to exit~')
exit(0)
