Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Import the modules
- import os, requests, json, urllib.request, sys
- #Header
- print("RedditRipper version 2 by Grifter")
- print("~~~~~~~~~~~~~~~~~~~~~~~")
- print(" ")
- #UserAuth
- username = input('Reddit username: ')
- password = input('Password: ')
- try:
- print("Attempting authentication....")
- user_pass_dict = {'user': username,
- 'passwd': password,
- 'api_type': 'json'}
- s = requests.Session()
- s.headers.update({'User-Agent' : 'Image link acquirer : ' + username})
- r = s.post(r'http://www.reddit.com/api/login', data=user_pass_dict)
- print("Authentication succesful")
- except:
- print('Authentication failed')
- input()
- sys.quit()
- #Global variables
- subreddit= input('Which subreddit? reddit.com/r/')
- count = 0
- ids = []
- #Check local save directory existence
- if not os.path.exists(subreddit):
- os.makedirs(subreddit)
- # Get the feed
- r = requests.get(r'http://www.reddit.com/r/%s/.json?limit=100' %subreddit)
- # Convert it to a Python dictionary
- data = r.json()
- #Request-download iterations
- while True:
- #Request json packet
- if count != 0:
- idnext = str(ids[count-1])
- r = requests.get(r'http://www.reddit.com/r/%s/.json?limit=100&after=t3_%s' %(subreddit,idnext))
- else:
- r = requests.get(r'http://www.reddit.com/r/%s/.json?limit=100' %subreddit)
- #Convert Json packet to python data
- data = r.json()
- #Iterate downloads
- for child in data['data']['children']:
- count += 1
- #Record id for json request reference & filenames
- ids.append( child['data']['id'])
- #Determine imgur domain
- if child['data']['domain'] == 'imgur.com' or child['data']['domain'] == 'i.imgur.com':
- #Differentiate filetypes
- if child['data']['url'][-3:] == 'gif':
- try:
- urllib.request.urlretrieve (child['data']['url'], subreddit +'\\'+child['data']['id']+'.gif')
- print(count)
- except:
- print(str(count) + " error")
- else:
- try:
- urllib.request.urlretrieve (child['data']['url']+'.png', subreddit +'\\'+child['data']['id']+'.png')
- print(count)
- except:
- print(str(count) + " error")
Add Comment
Please, Sign In to add comment