SHARE
TWEET

Images.py

a guest Dec 17th, 2015 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib, urllib2, webbrowser, time, os, os.path, getpass
  2. from BeautifulSoup import BeautifulSoup
  3.  
  4. #Returns HTML from Danbooru for a specific tag, rating, and page
  5. def checkDanbooru(searchTerms, rating, page):
  6.     header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.'}
  7.     url = 'http://danbooru.donmai.us/posts?page='+str(page)+'&utf8=%E2%9C%93&tags='+str(searchTerms)
  8.     if(rating == 1):
  9.         url += '+rating%3Aexplicit'
  10.     request = urllib2.Request(url, None, header)
  11.     u = urllib2.urlopen(request)
  12.     return u.read()
  13.  
  14. #Parses a Danbooru page's HTML and returns a list of all the image URLs.
  15. def parseDanbooru(html):
  16.     linkList = []
  17.     soup = BeautifulSoup(html)
  18.     for i in soup.findAll('article'):
  19.         try:
  20.             linkList.append(str('http://danbooru.donmai.us')+str(i['data-file-url']))
  21.         except:
  22.             pass
  23.     return linkList
  24.  
  25. #Processes a list of image URLs and stores them in 'Organized Pictures!~'
  26. #on the user's desktop. Names them based on your choice. Optionally opens in browser.
  27. #Prints the number of the image in relation to the page. (New page, starts counting over).
  28. #Also prints a number of percentage marks at the end of the image's name to prevent duplicate names.
  29. def process(fileName, links, turnOn, wait):
  30.     user = getpass.getuser()
  31.     orgPic = 'C:/Users/'+str(user)+'/Desktop/Organized Pictures!~/'
  32.     for i in range(0, len(links)):
  33.         if(turnOn == 1):
  34.             webbrowser.open(links[i])
  35.         number = len([g for g in os.listdir(orgPic) if os.path.isfile(orgPic + str(g))])
  36.         suffix = number * '%'
  37.         name = str('C:\\Users\\'+str(user)+'\\Desktop\\Organized Pictures!~\\')+str(fileName)+str(suffix)+str('.' + str(links[i].split('.')[-1]))
  38.         print 'Image ' + str(i)
  39.         urllib.urlretrieve(links[i], name)
  40.         time.sleep(wait)
  41.  
  42. #Main code
  43. #It does things
  44. search = raw_input('Enter search terms (Ex. takanashi_rikka): ').replace(' ', '+')
  45. rating = int(raw_input('NSFW? 1 or 0: '))
  46. fileName = raw_input('Enter what to name the file: ')
  47. waitPeriod = int(raw_input('Seconds between searches: '))
  48. openPage = int(raw_input('Would you like them to open in your default browser? 1 or 0: '))
  49. for i in range(0,999):
  50.     response = checkDanbooru(search, rating, i+1)
  51.     if('Nobody here but us chickens!' not in response):
  52.         links = parseDanbooru(response)
  53.         process(fileName, links, openPage, waitPeriod)
  54.     else:
  55.         break
  56. raw_input('\nFinished.')
RAW Paste Data
Top