Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib, urllib2, webbrowser, time, os, os.path, getpass
- from BeautifulSoup import BeautifulSoup
- #Returns HTML from Danbooru for a specific tag, rating, and page
- def checkDanbooru(searchTerms, rating, page):
- header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.'}
- url = 'http://danbooru.donmai.us/posts?page='+str(page)+'&utf8=%E2%9C%93&tags='+str(searchTerms)
- if(rating == 1):
- url += '+rating%3Aexplicit'
- request = urllib2.Request(url, None, header)
- u = urllib2.urlopen(request)
- return u.read()
- #Parses a Danbooru page's HTML and returns a list of all the image URLs.
- def parseDanbooru(html):
- linkList = []
- soup = BeautifulSoup(html)
- for i in soup.findAll('article'):
- try:
- linkList.append(str('http://danbooru.donmai.us')+str(i['data-file-url']))
- except:
- pass
- return linkList
- #Processes a list of image URLs and stores them in 'Organized Pictures!~'
- #on the user's desktop. Names them based on your choice. Optionally opens in browser.
- #Prints the number of the image in relation to the page. (New page, starts counting over).
- #Also prints a number of percentage marks at the end of the image's name to prevent duplicate names.
- def process(fileName, links, turnOn, wait):
- user = getpass.getuser()
- orgPic = 'C:/Users/'+str(user)+'/Desktop/Organized Pictures!~/'
- for i in range(0, len(links)):
- if(turnOn == 1):
- webbrowser.open(links[i])
- number = len([g for g in os.listdir(orgPic) if os.path.isfile(orgPic + str(g))])
- suffix = number * '%'
- name = str('C:\\Users\\'+str(user)+'\\Desktop\\Organized Pictures!~\\')+str(fileName)+str(suffix)+str('.' + str(links[i].split('.')[-1]))
- print 'Image ' + str(i)
- urllib.urlretrieve(links[i], name)
- time.sleep(wait)
- #Main code
- #It does things
- search = raw_input('Enter search terms (Ex. takanashi_rikka): ').replace(' ', '+')
- rating = int(raw_input('NSFW? 1 or 0: '))
- fileName = raw_input('Enter what to name the file: ')
- waitPeriod = int(raw_input('Seconds between searches: '))
- openPage = int(raw_input('Would you like them to open in your default browser? 1 or 0: '))
- for i in range(0,999):
- response = checkDanbooru(search, rating, i+1)
- if('Nobody here but us chickens!' not in response):
- links = parseDanbooru(response)
- process(fileName, links, openPage, waitPeriod)
- else:
- break
- raw_input('\nFinished.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement