Guest User

/wg/ 1080p downloader

a guest
Mar 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. #! /usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import urllib.request, urllib.error, urllib.parse, urllib.request, urllib.parse, urllib.error, http.cookiejar
  4. import http.client
  5. import json
  6. import pprint
  7. import os.path
  8.  
  9. URL_WGCAT = "http://a.4cdn.org/wg/catalog.json"
  10. #http://a.4cdn.org/wg/thread/4667225.json
  11. URL_THREAD = "http://a.4cdn.org/wg/thread/"
  12. URL_IMG = "https://i.4cdn.org/wg/"
  13. def newOpener():
  14.         cj = http.cookiejar.CookieJar()
  15.         opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
  16.         opener.addheaders.append(('User-agent', "wg image downloader"))
  17.         return opener
  18.  
  19. def postStuff(opener,site,postData, refer="",printResp =False):
  20.         postData = urllib.parse.urlencode(postData)
  21.         if refer != "":
  22.                 opener.addheaders.append( ('Referer', refer) )
  23.  
  24.         resp = opener.open(site, postData,timeOut)
  25.         if printResp:
  26.                 return resp.read();
  27.  
  28.  
  29. def main():
  30.     o = newOpener()
  31.     resp = o.open(URL_WGCAT)
  32.    
  33.     pp = pprint.PrettyPrinter(indent=2)
  34.    
  35.     pages = json.load(resp)
  36.     threads = []
  37.     for p in pages:
  38.         print((p['page']))
  39.         for t in p['threads']:
  40.             print((t['no']))
  41.             threads.append(t['no'])
  42.    
  43.     for t in threads:
  44.         tjson = o.open(URL_THREAD+str(t)+".json")
  45.         tvar  = json.load(tjson)
  46.         print(( "thread: " +str(t)))
  47.         for post in tvar['posts']:
  48.             try:
  49.                 #print "width: "+str(post['w']) + ", height: " + str(post['h'])+",ratio: "+str(post['w']/float(post['h']))+", image id: "+ str(post['tim'])+str(post['ext'])
  50.          
  51.                 if (post['w']==1920)  and (post['h'] ==1080):
  52.  
  53.                     if os.path.isfile(str(post['tim'])+str(post['ext'])):
  54.                         continue
  55.                        
  56.                     print(("width: "+str(post['w']) + ", height: " + str(post['h'])+",ratio: "+str(post['w']/float(post['h']))+", image id: "+ str(post['tim'])+str(post['ext'])))
  57.                     img = o.open(URL_IMG+str(post['tim'])+str(post['ext'])).read()
  58.                     f = open(str(post['tim'])+str(post['ext']),'wb')
  59.                     f.write(img)
  60.                     f.close()
  61.                    
  62.                     #ratio219.append(str(post['tim'])+str(post['ext']))
  63.             except KeyError:
  64.                 #print "post has no image"
  65.                 pass
  66.             except Exception as e:
  67.                 print((repr(e)))
  68.     #pp.pprint(json.load(resp))
  69.    
  70.    
  71. if __name__ == "__main__":
  72.     #Run as main program
  73.     main()
Add Comment
Please, Sign In to add comment