Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import requests.auth
- import json
- from ConfigParser import ConfigParser
- # init configs
- config = ConfigParser()
- config.read('config.ini')
- client_id = config.get('Auth', 'client_id')
- client_secret = config.get('Auth', 'client_secret')
- username = config.get('Auth', 'username')
- password = config.get('Auth', 'password')
- if config.has_option('Init', 'latest'):
- latest = config.get('Init', 'latest')
- else:
- latest = 'null'
- # config request
- client_auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
- post_data = {'grant_type': 'password', 'username': username, 'password': password}
- headers = {'User-Agent': 'DealsBot by ArqadeDeals'}
- response = requests.post('https://www.reddit.com/api/v1/access_token', auth=client_auth, data=post_data, headers=headers)
- access_token = response.json()['access_token']
- headers = {'Authorization': 'bearer ' + access_token, 'User-Agent': 'DealsBot by ArqadeDeals'}
- # check for new posts
- if latest != 'null':
- response = requests.get('https://oauth.reddit.com/r/gamedeals/new?before=' + latest, headers = headers)
- else:
- response = requests.get('https://oauth.reddit.com/r/gamedeals/new', headers=headers)
- # parse deals
- deals = []
- ids = []
- if 'error' not in response.json().keys() and len(response.json()['data']['children']) > 0:
- for post in response.json()['data']['children']:
- d = []
- d.append(post['data']['title'].encode('utf-8', 'replace'))
- d.append(post['data']['url'].encode('utf-8', 'replace'))
- deals.append(d)
- # write deals to output
- with open('deals.txt', 'w') as outfile:
- for deal in deals:
- outfile.write(deal[0] + ' ?\\|/? ' + deal[1] + '\n')
- # write latest to config
- latest = response.json()['data']['children'][0]
- latest_fullname = latest['kind'] + '_' + latest['data']['id']
- config.set('Init', 'latest', latest_fullname)
- with open('config.ini', 'wb') as configfile:
- config.write(configfile)
- else:
- with open('deals.txt', 'w') as outfile:
- outfile.write('nothing')
Add Comment
Please, Sign In to add comment