Repository navigation
Expand file tree
/
Copy pathtweets.py
More file actions
150 lines (124 loc) · 4.24 KB
/
Copy pathtweets.py
File metadata and controls
150 lines (124 loc) · 4.24 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- encoding: utf-8 -*-
"""twitter demo for CodeBug Tether.
Call with no arguments to pull latest tweets from home timeline.
Call with string argument to search for that term
"""
# only needed for testing
import os
import sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
import time
import threading
import sys
import os
try:
import twitter # http://mike.verdone.ca/twitter/
except ImportError:
print("You need to install Python Twitter Tools "
"(http://mike.verdone.ca/twitter/).")
sys.exit(1)
from codebug_tether import CodeBug
UPDATE_INTERVAL = 60
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
# CONSUMER_KEY = ""
# CONSUMER_SECRET = ""
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""
class NoTweetsError(Exception):
pass
class TwitterTicker(object):
def __init__(self, codebug, oauth_token, oauth_secret, search_term=None):
self.twitter = twitter.Twitter(
auth=twitter.OAuth(
oauth_token, oauth_secret,
CONSUMER_KEY, CONSUMER_SECRET)
)
self.search_term = search_term
self.codebug = codebug
try:
self.current_tweet = self.get_latest_tweet()
except NoTweetsError:
self.current_tweet = None
self.display_tweet(self.current_tweet)
self.timer = None
# @property
# def page(self):
# return self._current_page
# @page.setter
# def page(self, new_page):
# num_pages = 1 + int(len(self.current_tweet['text']) / PAGE_WIDTH)
# new_page %= num_pages
# self.display_tweet(self.current_tweet, new_page)
def get_latest_tweet(self):
if self.search_term is None:
return self.twitter.statuses.home_timeline()[0]
try:
latest_tweets = self.twitter.search.tweets(
q=self.search_term,
since_id=self.current_tweet['id'])['statuses']
except AttributeError:
latest_tweets = self.twitter.search.tweets(
q=self.search_term)['statuses']
try:
return latest_tweets[0]
except IndexError:
raise NoTweetsError()
def update(self, event=None):
"""Updated the screen with the latest tweet."""
print("Updating...")
try:
latest_tweet = self.get_latest_tweet()
except NoTweetsError:
return
else:
if self.current_tweet is None or \
latest_tweet['id'] != self.current_tweet['id']:
self.current_tweet = latest_tweet
self.display_tweet(self.current_tweet)
def auto_update(self):
self.update()
# update again soon
self.timer = threading.Timer(UPDATE_INTERVAL, self.auto_update)
self.timer.start()
def display_tweet(self, tweet, page=0):
self._current_page = page
text = tweet['text']
print("DISPLAYING TWEET", text)
self.codebug.clear()
delay = 0.05
for i in range(len(text) * 5 + 5):
self.codebug.write_text(5 - i, 0, text)
time.sleep(delay)
def close(self):
if self.timer is not None:
self.timer.cancel()
self.cad.lcd.clear()
self.cad.lcd.backlight_off()
def next_page(self, event=None):
self.page += 1
def previous_page(self, event=None):
self.page -= 1
if __name__ == "__main__":
try:
search_term = sys.argv[1]
except IndexError:
search_term = None
print("Using home timeline.")
else:
print("Searching for", search_term)
twitter_creds = os.path.expanduser('~/.twitter_piface_demo_credentials')
if not os.path.exists(twitter_creds):
twitter.oauth_dance("CodeBug Tether Twitter",
CONSUMER_KEY,
CONSUMER_SECRET,
twitter_creds)
oauth_token, oauth_secret = twitter.read_token_file(twitter_creds)
codebug = CodeBug()
global twitterticker
twitterticker = TwitterTicker(codebug,
oauth_token,
oauth_secret,
search_term)
twitterticker.auto_update() # start the updating process