Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import time
- import requests
- import json
- import serial
- import unicodedata
- mysubreddit = 'worldnews'
- mysleeptime = 25
- print 'Sleeptime = ' + str(mysleeptime)
- time.sleep(mysleeptime) # sleep to allow rpi to connect to wifi
- parameters = {'limit': 25,}
- ser = serial.Serial('/dev/ttyUSB0','57600')
- time.sleep(5)
- headers = {
- 'User-Agent': 'PiReddituino - JSON Grabber Bot '
- }
- #set variable to count requests to server
- intreq=0
- #loop forever
- while(1==1):
- ser = serial.Serial('/dev/ttyUSB0','57600')
- time.sleep(5)
- intreq = intreq + 1
- print 'Loaded subreddit ' + str(intreq) + ' times'
- r = requests.get(r'http://www.reddit.com/r/' + mysubreddit + '/.json',headers=headers,params = parameters)
- data = r.json()
- print 'Loaded articles from reddit.com/r/' + mysubreddit + '\n'
- article = 1
- for child in data['data']['children']:
- mystring = unicodedata.normalize('NFKD', child['data']['title'])
- mystring = '[' + str(article) + '][' + str(child['data']['score']) + '] ' + mystring
- mystring = mystring.encode('ASCII', 'ignore')
- mystring = mystring.replace("'", "")
- mystring = mystring.replace("\"","")
- #mystring = ' [' + str(child['data']['score']) + '] ' + mystring
- if len(mystring) > 200:
- mystring = mystring[:200] + '... '
- mystring = mystring + ' \n'
- print 'arduino serial data : (' + str(mystring) + ')'
- print 'data length:' + str(len(mystring))
- print 'serial data length:' + str(len(mystring))
- ser.write(mystring)
- #print message to console
- print 'Sleeping for ' + str(mysleeptime) + ' seconds.'
- time.sleep(mysleeptime)
- article = article + 1
Add Comment
Please, Sign In to add comment