Guest User

Hakology Python Reddit Code

a guest
Nov 23rd, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. #!/usr/bin/python
  2. import time
  3. import requests
  4. import json
  5. import serial
  6. import unicodedata
  7. mysubreddit = 'worldnews'
  8. mysleeptime = 25
  9. print 'Sleeptime = ' + str(mysleeptime)
  10. time.sleep(mysleeptime) # sleep to allow rpi to connect to wifi
  11. parameters = {'limit': 25,}
  12. ser = serial.Serial('/dev/ttyUSB0','57600')
  13. time.sleep(5)
  14. headers = {
  15.     'User-Agent': 'PiReddituino - JSON Grabber Bot '
  16. }
  17. #set variable to count requests to server
  18. intreq=0
  19. #loop forever
  20. while(1==1):
  21.     ser = serial.Serial('/dev/ttyUSB0','57600')
  22.     time.sleep(5)
  23.     intreq = intreq + 1
  24.     print 'Loaded subreddit ' + str(intreq) + ' times'
  25.     r = requests.get(r'http://www.reddit.com/r/' + mysubreddit  + '/.json',headers=headers,params = parameters)
  26.     data = r.json()
  27.     print 'Loaded articles from reddit.com/r/' + mysubreddit + '\n'
  28.     article = 1
  29.     for child in data['data']['children']:
  30.         mystring = unicodedata.normalize('NFKD', child['data']['title'])
  31.         mystring = '[' + str(article) + '][' + str(child['data']['score']) + '] ' + mystring
  32.         mystring = mystring.encode('ASCII', 'ignore')
  33.         mystring = mystring.replace("'", "")
  34.         mystring = mystring.replace("\"","")
  35.         #mystring = ' [' + str(child['data']['score']) + '] ' + mystring
  36.         if len(mystring) > 200:
  37.             mystring = mystring[:200] + '...  '
  38.         mystring = mystring + ' \n'
  39.         print 'arduino serial data : (' + str(mystring) + ')'
  40.         print 'data length:' + str(len(mystring))
  41.         print 'serial data length:' + str(len(mystring))
  42.         ser.write(mystring)
  43.         #print message to console
  44.         print 'Sleeping for ' + str(mysleeptime) + ' seconds.'
  45.         time.sleep(mysleeptime)
  46.         article = article + 1
Add Comment
Please, Sign In to add comment