TankorSmash

working

Apr 14th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import requests
  2. import json
  3. import time
  4. from pprint import pprint as pp  #for simplicity
  5.  
  6. ## LOGIN to reddit ##
  7. url = r'http://www.reddit.com/api/login/'
  8. payload = {'user': 'USERNAME',
  9.            'passwd': 'PASSWORD',
  10.            'api_type': 'json'}
  11.  
  12. r = requests.post(url, payload)
  13. #save the cookie
  14. redditCookie = r.cookies
  15. #print the response
  16. print 'Login Response:'
  17. j = json.loads(r.text)
  18. pp(j)
  19. #save the modhash
  20. mh = j['json']['data']['modhash']
  21.  
  22. ##SUBMIT to reddit ##
  23. url = r'http://www.reddit.com/api/submit'
  24. payload = {'title' : 'API TEST!',
  25.            'text': '{}'.format(time.asctime()),
  26.            'sr': 'tankorsmash',
  27.            'kind': 'self',
  28.            'uh': mh,
  29.            'r': 'tankorsmash',
  30.            }
  31. #post the story
  32. r = requests.post(url, payload, cookies=redditCookie)
  33. print 'posted story with hash:', mh
  34. print 'and cookies:', redditCookie
  35.  
  36. #here's the response:
  37. j = json.loads(r.text)
  38. print 'response from API after submission'
  39. pp(j)
Add Comment
Please, Sign In to add comment