Advertisement
ashground

ffffind.py

Oct 15th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. """
  2.  
  3.     ffffind.py
  4.     by aaron@andcuriouser.com
  5.    
  6.     Automatically downloads all images from ffffound saved by a specific user.
  7.     Will first try to download the image from the original source (to get the highest quality possible).
  8.     If that fails, it'll download the cached version from ffffound.
  9.    
  10.     Prerequisities:
  11.         Beautiful Soup (http://www.crummy.com/software/BeautifulSoup/)
  12.    
  13.     Usage:
  14.         python ffffind.py username
  15.  
  16. """
  17.  
  18.  
  19.  
  20. import os, sys, urllib, imghdr
  21. from BeautifulSoup import BeautifulSoup
  22. from urlparse import urlparse
  23. from posixpath import basename, dirname
  24.  
  25. def main(user):
  26.     offset = 0
  27.     page = 1
  28.     while True:
  29.         print "Capturing page "+str(page)+" ..."
  30.         print
  31.         f = urllib.urlopen("http://ffffound.com/home/"+user+"/found/?offset="+str(offset))
  32.         s = f.read()
  33.         f.close()
  34.         if "<div class=\"description\">" in s:
  35.             images = []
  36.             offset += 25
  37.             count = 0
  38.             soup = BeautifulSoup(s)
  39.             for i in soup.findAll("div", { "class" : "description" }):
  40.                 images.append({"url": urlparse("http://" + str(i).split("<br />")[0].replace("<div class=\"description\">", ""))})
  41.             for i in soup.findAll("img"):
  42.                 if str(i).find("_m.") != -1:
  43.                     images[count]["backup"] = str(i).split("src=\"")[1].split("\"")[0]
  44.                     count += 1
  45.             for i in images:
  46.                 if os.path.exists(user+"/"+basename(i["url"].path)):
  47.                     print basename(i["url"].path) + " exists, stopping."
  48.                     print
  49.                     sys.exit()
  50.                 else:
  51.                     print "Downloading " + basename(i["url"].path),
  52.                     try:
  53.                         urllib.urlretrieve(i["url"].geturl(), user+"/"+basename(i["url"].path))
  54.                         print "... done."
  55.                         if not imghdr.what(user+"/"+basename(i["url"].path)) in ["gif", "jpeg", "png", None]:
  56.                             print "... unfortunately, it seems to be a bad image.\nDownloading backup",
  57.                             try:
  58.                                 urllib.urlretrieve(i["backup"], user+"/"+basename(i["url"].path))
  59.                                 print "... which seems to have worked."
  60.                             except:
  61.                                 print "... which also failed."
  62.                         if os.path.getsize(user+"/"+basename(i["url"].path)) < 5000:
  63.                             raise
  64.                     except:
  65.                         print "... failed. Downloading backup",
  66.                         try:
  67.                             urllib.urlretrieve(i["backup"], user+"/"+basename(i["url"].path))
  68.                             print "... which seems to have worked."
  69.                         except:
  70.                             print "... which also failed."
  71.                 print
  72.             page += 1
  73.         else:
  74.             print "Reached the end of the list, stopping."
  75.             break
  76.  
  77. if __name__ == '__main__':
  78.     print
  79.     print("ffffound image downloader")
  80.     print
  81.     if len(sys.argv) < 2:
  82.         print "Usage:\n\t python ffffind.py username"
  83.         print
  84.     else:
  85.         try:
  86.             if not os.path.exists("./"+sys.argv[1]):
  87.                 os.mkdir(sys.argv[1])
  88.         except:
  89.             print "Error creating directory."
  90.             sys.exit()
  91.         user = sys.argv[1]
  92.         print "Downloading all pictures from user '"+user+"'"
  93.         print
  94.         main(user)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement