Last active
April 4, 2018 08:19
-
-
Save sospedra/e9ecaad15280249496593c3d4e12291c to your computer and use it in GitHub Desktop.
For Onion Omega 2. You need a config.json file alongside this one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import base64 | |
import urllib3 | |
import unicodedata as ud | |
from OmegaExpansion import oledExp | |
http = urllib3.PoolManager() | |
baseUrl = "https://api.twitter.com" | |
bearerToken = "" | |
# function to perform applcation-only authentication with Twitter | |
def twitterApiAuthenticate(consumerKey, consumerSecret): | |
url = baseUrl + "/oauth2/token" | |
# in the future, may have to RFC 1738 encode the consumer key and secret | |
# base64 encode the concetanated consumer key and secret | |
consumerCredentials = consumerKey + ":" + consumerSecret | |
encodedConsumerCredentials = base64.b64encode(consumerCredentials) | |
# create the request headers and body | |
headers = { | |
'Authorization': 'Basic ' + encodedConsumerCredentials, | |
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | |
} | |
body = "grant_type=client_credentials" | |
# perform the request | |
r = http.request( | |
'POST', | |
url, | |
headers=headers, | |
body=body | |
) | |
# convert the response data to a dictionary | |
responseData = json.loads(r.data.decode('utf-8')) | |
if 'token_type' in responseData and responseData['token_type'] == 'bearer' and 'access_token' in responseData: | |
global bearerToken | |
bearerToken = responseData['access_token'] | |
return True | |
else: | |
bearerToken = responseData | |
return False | |
def accent_folding(chars): | |
return ud.normalize('NFD', chars).encode('ascii', 'ignore') | |
def strip_links(plain): | |
chain = '' | |
for word in plain.split(' '): | |
chain += '' if word.startswith('http') else word | |
chain += ' ' | |
return chain | |
# function to read the last tweet from a user's timeline | |
def twitterApiGetLastTweet(userId): | |
url = baseUrl + '/1.1/statuses/user_timeline.json' | |
params = { | |
'screen_name': userId, | |
'count': 1 | |
} | |
# create the request headers | |
headers = { | |
'Authorization': 'Bearer ' + bearerToken | |
} | |
# execute the GET request | |
r = http.request( | |
'GET', | |
url, | |
headers=headers, | |
fields=params | |
) | |
# convert the response data to a dictionary | |
responseData = json.loads(r.data.decode('ISO-8859-1')) | |
lastTweet = responseData[0] | |
if 'text' in lastTweet: | |
ret = { | |
'text': strip_links(accent_folding(lastTweet['text'])), | |
'date': lastTweet['created_at'] | |
} | |
return ret | |
else: | |
return False | |
def oledWriteTweet(user, text, date): | |
if oledExp.driverInit() != 0: | |
print 'ERROR: Could not initialize the OLED Expansion' | |
return False | |
# write out the date | |
date = date.split(' ') | |
oledExp.write(date[1] + ' ' + date[2] + ' ' + date[3]) | |
# write out the name of the account | |
oledExp.setCursor(1,0) | |
oledExp.write('@' + user + ':') | |
# write out the tweet | |
oledExp.setCursor(2,0) | |
oledExp.write(text) | |
### MAIN PROGRAM ### | |
def mainProgram(): | |
# find the directory of the script | |
dirName = os.path.dirname(os.path.abspath(__file__)) | |
# read the config file | |
with open( '/'.join([dirName, 'config.json']) ) as f: | |
config = json.load(f) | |
# authenticate with twitter | |
authSuccess = twitterApiAuthenticate(config['authorization']['consumerKey'], config['authorization']['consumerSecret']) | |
if not authSuccess: | |
print "ERROR: Invalid API credentials!" | |
exit() | |
# use twitter api to get last tweet of specified user | |
tweet = twitterApiGetLastTweet(config['application']['user']) | |
if not tweet: | |
print "ERROR: Could not retreive Tweet!" | |
exit() | |
print 'Got tweet! ', tweet | |
# display the tweet on the OLED | |
oledWriteTweet(config['application']['user'], tweet['text'], tweet['date']) | |
print 'Done!' | |
if __name__ == "__main__": | |
oledExp.write('# update') | |
mainProgram() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment