Skip to content

Commit ba35cff

Browse files
committed
Merge branch 'usertimeline'
2 parents 43ef699 + 9c5b21a commit ba35cff

6 files changed

Lines changed: 68 additions & 36 deletions

File tree

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# teletext-twitter
2-
Reads your Twitter timeline or searches for tweets and turns them into teletext pages for your Raspberry Pi
2+
Display Twitter timelines or searches for tweets and turns them into teletext pages for your Raspberry Pi
33

44
![Screenshot](https://i.imgur.com/xwnfUw3.jpg "Screenshot of output 1")
55
![Screenshot](https://i.imgur.com/XuiyOsE.jpg "Screenshot of output 2")
@@ -24,11 +24,13 @@ After getting these up and running there are some setup tasks to do. Rename conf
2424

2525
1) Your Twitter access tokens: You can find a good guide for doing this here - https://iag.me/socialmedia/how-to-create-a-twitter-app-in-8-easy-steps/ You will need to pick a unique name for the app. Which is annoying. Pick anything you want that isn't taken. Maybe add your name at the end.
2626
2) Check where your pages will be saved to: Change the tti_path line too if you've changed your Teefax location. You can also customise the page number.
27-
3) Change the colours if you want: Theme support is detailed below
27+
3) Change the theme if you want: Theme support is detailed below
28+
29+
## Usage
2830

2931
When you've setup your config.py you can run the script like this example that grabs your home timeline:
3032

31-
`python3 teletext-twitter -t` (add -h to show options - also listed below)
33+
`python3 teletext-twitter -m home` (add -h to show options - also listed below)
3234

3335
The script will constantly update your chosen page number (default is 153 - chosen because it used to be used for this purpose on Teefax in the past) in the main teletext folder (which defaults in VBIT2 to /home/pi/teletext/).
3436

@@ -48,7 +50,7 @@ You can change the colours of the main header bar and the thin separator.
4850

4951
You can also change the colours of a tweet poster's username, timestamp, and the text of the tweet itself.
5052

51-
Finally, the title text at the top can be changed, although there is a character limit.
53+
Finally, the title text at the top can be changed, although there is a 30 character limit or things look wonky.
5254

5355
## Notes
5456
* At this moment in time the script reads 5 tweets. Further versions will improve on this by writing multiple tweets, possibly in subpages :-O
@@ -61,17 +63,16 @@ Apart from those notes, things should work ok. Have fun, turn back the clock, an
6163
### -h/--help output
6264

6365
<pre>
64-
usage: teletext-twitter.py [-h] [-t] [-s] [-q QUERY] [-d DELAY] [-v] [-Q]
66+
usage: teletext-twitter.py [-h] [-m MODE] [-q QUERY] [-d DELAY] [-v] [-Q]
6567

6668
Reads your Twitter timeline and turns it into teletext pages for your Raspberry Pi.
6769

6870
optional arguments:
6971
-h, --help show this help message and exit
70-
-t, --timeline download your latest home timeline
71-
-s, --search specify a term to search for
72+
-m, --mode
7273
-q QUERY, --query QUERY
73-
a search query, hashtags supported if you put quotes
74-
around the string
74+
a search query - username or search term. hashtags
75+
supported if you put quotes around the string
7576
-d DELAY, --delay DELAY
7677
seconds between timeline scrapes (minimum is 60
7778
seconds - lower values have no effect)

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## Changelog
22

3+
### v0.6
4+
- New mode added: show latest tweets from a certain user
5+
- cmd line switches changed to accommodate user mode
6+
- logo looks less like a dog
7+
- bug fix: tweet cleaning now works in search mode
8+
- bug fix: added curly quote->normal quote replacement
9+
- default config file wasn't formatted right
10+
311
### v0.5.3
412
- Page number now user-customisable
513

config.py-default

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
# twitter keys
44
consumer_key = "XxXxXxxXXXxxxxXXXxXX"
5-
consumer_secret =
6-
"xXXXXXXXXxxxxXxXXxxXxxXXxXxXxxxxXxXXxxxXXx"
7-
access_key =
8-
"XXXXXXXX-xxXXxXXxxXxxxXxXXxXxXxXxxxXxxxxXxXXxXxxXX"
5+
consumer_secret = "xXXXXXXXXxxxxXxXXxxXxxXXxXxXxxxxXxXXxxxXXx"
6+
access_key = "XXXXXXXX-xxXXxXXxxXxxxXxXXxXxXxXxxxXxxxxXxXXxXxxXX"
97
access_secret = "XxXXXXXXXXxxxXXXxXXxXxXxxXXXXXxXxxXXXXx"
108

119
# theme section

teletext-twitter/__main__.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,21 @@
2121
def parse_args():
2222
parser = argparse.ArgumentParser(description="Reads your Twitter timeline and turns it into teletext pages for your Raspberry Pi.")
2323

24-
parser.add_argument("-t", "--timeline", action="store_true", dest="home_timeline", help="download your latest home timeline", default=False)
25-
parser.add_argument("-s", "--search", action="store_true", dest="search", help="specify a term to search for")
26-
parser.add_argument("-q", "--query", type=str, help="a search query, hashtags supported if you put quoted around the string")
24+
parser.add_argument("-m", "--mode", type=str, help="choose between different modes - home, user or search")
25+
parser.add_argument("-q", "--query", type=str, help="a search query, either a search term or a username. hashtags supported if you put quotes around the string")
2726
parser.add_argument("-d", "--delay", type=int, default=60, help="seconds between timeline scrapes (minimum is 60 seconds - lower values have no effect)")
28-
parser.add_argument("-v", "--version", action="version", version="0.5.3")
27+
parser.add_argument("-v", "--version", action="version", version="0.6")
2928
parser.add_argument("-Q", "--quiet", action="store_true", default=False, help="suppresses all output to the terminal except warnings and errors")
3029

3130
args = parser.parse_args()
3231
args.delay = max(60, args.delay)
3332

34-
if not args.home_timeline and not args.search:
35-
print("[!] No mode specified. Use -t or -s arguments. Exiting...", file=sys.stderr)
33+
if args.mode == "search" and not args.query:
34+
print("[!] Search mode selected but no query specfied with -q. Exiting...", file=sys.stderr)
3635
sys.exit(1)
3736

38-
if args.home_timeline and args.search:
39-
print("[!] Home timeline and search options cannot be selected together. Exiting...", file=sys.stderr)
40-
sys.exit(1)
41-
42-
if args.search and not args.query:
43-
print("[!] Search option selected but no query specfied with -q. Exiting...", file=sys.stderr)
37+
if args.mode == "user" and not args.query:
38+
print("[!] User timeline mode selected but no username specified. Exiting...", file=sys.stderr)
4439
sys.exit(1)
4540

4641
return args
@@ -54,14 +49,18 @@ def main():
5449
while True:
5550
try:
5651
write_header(config)
57-
if args.home_timeline:
52+
if args.mode == "home":
5853
if not args.quiet:
5954
print("[*] Beginning home timeline scraping", file=sys.stdout)
60-
write_timeline(twitter_object, config)
61-
elif args.search:
55+
write_home_timeline(twitter_object, config)
56+
elif args.mode == "search":
6257
if not args.quiet:
6358
print("[*] Getting recent tweets containing: " + args.query, file=sys.stdout)
6459
write_search_term(twitter_object, args.query, config)
60+
elif args.mode == "user":
61+
if not args.quiet:
62+
print("[*] Getting all tweets from user: @{}".format(args.query), file=sys.stdout)
63+
write_user_timeline(twitter_object, args.query, config)
6564
if not args.quiet:
6665
print("[*] Page updated. Waiting {} seconds until next scrape".format(args.delay), file=sys.stdout)
6766
except OSError as e:

teletext-twitter/output.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ESCAPE = chr(27)
1111
DOUBLE_HEIGHT = ESCAPE + chr(77)
1212
SET_BACKGROUND = ESCAPE + chr(93)
13-
TWITTER_BIRD = chr(105) + chr(108) + chr(39)
13+
TWITTER_BIRD = chr(41) + chr(108) + chr(39)
1414
text_colours = {"red" : 65, "green" : 66, "yellow" : 67 , "blue" : 68, "magenta" : 69, "cyan" : 70, "white" : 71}
1515
mosaic_colours = {"red" : 81, "green" : 82, "yellow" : 83, "blue" : 84, "magenta" : 85, "cyan" : 86, "white" : 87}
1616

@@ -42,7 +42,7 @@ def write_header(config): # write a header for the page and pop a nice banner at
4242
page_title + logo_spacer + ESCAPE + chr(mosaic_colours["cyan"]) + TWITTER_BIRD + "\r\n")
4343
file.write("OL,3," + ESCAPE + chr(mosaic_colours[config["header_separator"]]) + (chr(35) * 39) + "\r\n")
4444

45-
def write_timeline(twitter_object, config): # grab the latest timeline - only 5 tweets for now
45+
def write_home_timeline(twitter_object, config): # grab the latest timeline - only 5 tweets for now
4646
statuses = twitter_object.GetHomeTimeline(count = 5)
4747
line_position = 4
4848

@@ -51,7 +51,7 @@ def write_timeline(twitter_object, config): # grab the latest timeline - only 5
5151
tweet_time = time.strptime(status.created_at,"%a %b %d %H:%M:%S +0000 %Y")
5252
tweet_human_time = time.strftime("%d-%b-%Y %H:%M", tweet_time) # reformat time/date output
5353
tweet_username = status.user.screen_name
54-
tweet_text = textwrap.wrap(tweet_text, 39) # make sure our lines fit on the screen
54+
tweet_text = textwrap.wrap(tweet_text, 38) # make sure our lines fit on the screen
5555

5656
if (line_position + len(tweet_text) + 1) > 24: # are we going to fill the page?
5757
break # yep! dump the last tweet!
@@ -61,7 +61,7 @@ def write_timeline(twitter_object, config): # grab the latest timeline - only 5
6161
line_position += 1
6262
for line in tweet_text:
6363
write_tweet_line(file, line_position, line, config)
64-
line_position += 1
64+
line_position += 11
6565

6666
def write_search_term(twitter_object, search_term, config): # search recent tweets with a particular search term
6767
statuses = twitter_object.GetSearch(term=search_term, result_type="recent", count=5)
@@ -72,7 +72,30 @@ def write_search_term(twitter_object, search_term, config): # search recent twee
7272
tweet_time = time.strptime(status.created_at,"%a %b %d %H:%M:%S +0000 %Y")
7373
tweet_human_time = time.strftime("%d-%b-%Y %H:%M", tweet_time) # reformat time/date output
7474
tweet_username = status.user.screen_name
75-
tweet_text = textwrap.wrap(status.text, 39) # make sure our lines fit on the screen
75+
tweet_text = textwrap.wrap(tweet_text, 38) # make sure our lines fit on the screen
76+
77+
if (line_position + len(tweet_text) + 1) > 24: # are we going to fill the page?
78+
break # yep! dump the last tweet!
79+
80+
with open(config["tti_path"] + "P" + str(config["page_number"]) + ".tti", "a") as file:
81+
write_tweet_info(file, line_position, tweet_username, tweet_human_time, config)
82+
line_position += 1
83+
for line in tweet_text:
84+
write_tweet_line(file, line_position, line, config)
85+
line_position += 1
86+
87+
def write_user_timeline(twitter_object, username, config):
88+
print("user mode, username: @{}".format(username))
89+
90+
statuses = twitter_object.GetUserTimeline(screen_name=username, count=5)
91+
line_position = 4
92+
93+
for status in statuses: # iterate through our responses
94+
tweet_text = clean_tweet(status.text)
95+
tweet_time = time.strptime(status.created_at,"%a %b %d %H:%M:%S +0000 %Y")
96+
tweet_human_time = time.strftime("%d-%b-%Y %H:%M", tweet_time) # reformat time/date output
97+
tweet_username = status.user.screen_name
98+
tweet_text = textwrap.wrap(tweet_text, 38) # make sure our lines fit on the screen
7699

77100
if (line_position + len(tweet_text) + 1) > 24: # are we going to fill the page?
78101
break # yep! dump the last tweet!

teletext-twitter/processor.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# character replacement and stripping function
1010
def clean_tweet(tweet):
11+
# first, get rid of as many emojis as possible
1112
emoji_pattern = re.compile("[" # our unicode ranges go here. this will need frequent tweaking
1213
u"\U00002300-\U000023FF" # misc technical
1314
u"\U000024C2-\U0001F251" # enclosed characters including flags
@@ -17,8 +18,10 @@ def clean_tweet(tweet):
1718
"]+", flags=re.UNICODE)
1819
tweet = emoji_pattern.sub(r'', tweet)
1920

20-
tweet = tweet.replace("’", "'") # replacing some problematic characters here
21-
tweet = tweet.replace("_", "-") # the teletext English character set doesn't
22-
tweet = tweet.replace("#", "_") # support a lot of things!
21+
# Now our character substitutions. The teletext English character set doesn't support a lot of characters!
22+
tweet = tweet.replace("’", "'")
23+
tweet = tweet.replace("_", "-")
24+
tweet = tweet.replace("#", "_")
25+
tweet = tweet.replace('“', '"')
2326

2427
return tweet

0 commit comments

Comments
 (0)