Guest User

Untitled

a guest
Mar 13th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. #!/usr/bin/python
  2. import urllib2, argparse, logging
  3. import os, sys, re, time
  4. import httplib
  5.  
  6. log = logging.getLogger('inb4404')
  7.  
  8. def load(url):
  9.     return urllib2.urlopen(url).read()
  10.  
  11. def main():
  12.     parser = argparse.ArgumentParser(description='inb4404')
  13.     parser.add_argument('thread', nargs=1, help='url of the thread')
  14.     args = parser.parse_args()
  15.  
  16.     logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(message)s', datefmt='%I:%M:%S %p')
  17.  
  18.     workpath = os.path.dirname(os.path.realpath(__file__))
  19.     board = ''.join(args.thread).split('/')[3]
  20.     thread = ''.join(args.thread).split('/')[5].split('#')[0]
  21.  
  22.     directory = os.path.join(workpath, 'downloads', board, thread)
  23.     if not os.path.exists(directory):
  24.         os.makedirs(directory)
  25.  
  26.     os.chdir(directory)
  27.  
  28.     while len(args.thread):
  29.         for t in args.thread:
  30.             try:
  31.                 for link, img in re.findall('(\/\/i.4cdn.org/\w+\/(\d+\.(?:jpg|png|gif|webm)))', load(t)):
  32.                     if not os.path.exists(img):
  33.                         log.info(img)
  34.                         data = load('https:' + link)
  35.                         with open(img, 'w') as f:
  36.                             f.write(data)
  37.             except urllib2.HTTPError, err:
  38.                 log.info('%s 404\'d', t)
  39.                 args.thread.remove(t)
  40.                 continue
  41.             except (urllib2.URLError, httplib.BadStatusLine, httplib.IncompleteRead):
  42.                 log.warning('something went wrong')
  43.         print('.')
  44.         time.sleep(20)
  45.  
  46. if __name__ == '__main__':
  47.     try:
  48.         main()
  49.     except KeyboardInterrupt:
  50.         pass
Advertisement
Add Comment
Please, Sign In to add comment