Advertisement
Guest User

Untitled

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