forked from OnionIoT/oled-twitter-display
-
Notifications
You must be signed in to change notification settings - Fork 6
/
oledTwitterDisplay.py
126 lines (99 loc) · 3.34 KB
/
oledTwitterDisplay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import json
import base64
import urllib3
http = urllib3.PoolManager()
from OmegaExpansion import oledExp
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
# 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('utf-8'))
lastTweet = responseData[0]
if 'text' in lastTweet:
ret = {
'text': lastTweet['text'].encode('utf-8'),
'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 name of the account
oledExp.write('@' + user + ':')
# set the cursor to the next line
oledExp.setCursor(1,0)
# write out the tweet
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__":
mainProgram()