Guest User

Untitled

a guest
Apr 13th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import requests
  2. import requests.auth
  3. import json
  4. from ConfigParser import ConfigParser
  5.  
  6. # init configs
  7. config = ConfigParser()
  8. config.read('config.ini')
  9. client_id = config.get('Auth', 'client_id')
  10. client_secret = config.get('Auth', 'client_secret')
  11. username = config.get('Auth', 'username')
  12. password = config.get('Auth', 'password')
  13. if config.has_option('Init', 'latest'):
  14. latest = config.get('Init', 'latest')
  15. else:
  16. latest = 'null'
  17.  
  18. # config request
  19. client_auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
  20. post_data = {'grant_type': 'password', 'username': username, 'password': password}
  21. headers = {'User-Agent': 'DealsBot by ArqadeDeals'}
  22. response = requests.post('https://www.reddit.com/api/v1/access_token', auth=client_auth, data=post_data, headers=headers)
  23. access_token = response.json()['access_token']
  24. headers = {'Authorization': 'bearer ' + access_token, 'User-Agent': 'DealsBot by ArqadeDeals'}
  25.  
  26. # check for new posts
  27. if latest != 'null':
  28. response = requests.get('https://oauth.reddit.com/r/gamedeals/new?before=' + latest, headers = headers)
  29. else:
  30. response = requests.get('https://oauth.reddit.com/r/gamedeals/new', headers=headers)
  31.  
  32. # parse deals
  33. deals = []
  34. ids = []
  35. if 'error' not in response.json().keys() and len(response.json()['data']['children']) > 0:
  36. for post in response.json()['data']['children']:
  37. d = []
  38. d.append(post['data']['title'].encode('utf-8', 'replace'))
  39. d.append(post['data']['url'].encode('utf-8', 'replace'))
  40. deals.append(d)
  41.  
  42. # write deals to output
  43. with open('deals.txt', 'w') as outfile:
  44. for deal in deals:
  45. outfile.write(deal[0] + ' ?\\|/? ' + deal[1] + '\n')
  46.  
  47. # write latest to config
  48. latest = response.json()['data']['children'][0]
  49. latest_fullname = latest['kind'] + '_' + latest['data']['id']
  50. config.set('Init', 'latest', latest_fullname)
  51. with open('config.ini', 'wb') as configfile:
  52. config.write(configfile)
  53. else:
  54. with open('deals.txt', 'w') as outfile:
  55. outfile.write('nothing')
Add Comment
Please, Sign In to add comment