Skip to content

Commit 45147df

Browse files
committed
v0.8 - subpage support added among other things!
1 parent 4c4d47a commit 45147df

5 files changed

Lines changed: 43 additions & 28 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Before you get this up and running there are two other things you need to do fir
2020
* VBIT2 (Peter Kwan): https://github.com/peterkvt80/vbit2
2121

2222
## Setup
23-
After getting these up and running there are some setup tasks to do. Rename config.py-default to config.py and open the file in a text editor. You need to add:
23+
After getting these up and running there are some setup tasks to do. Rename config.py-default to config.py and open the file in a text editor. You need to:
2424

25-
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.
25+
1) Add 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.
2727
3) Change the theme if you want: Theme support is detailed below
2828

@@ -34,7 +34,7 @@ When you've setup your config.py you can run the script like this example that g
3434

3535
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/).
3636

37-
All of the files in that folder are sent across to the TV every so often, therefore the script constantly overwrites it with new tweets (up to 5 - space permitting!) so that it will update on your screen.
37+
All of the files in that folder are sent across to the TV every so often, therefore the script constantly overwrites it with new tweets (up to 5 - space permitting!) so that it will update on your screen. Up to 99 subpages of tweets will be displayed.
3838

3939
## Theme support
4040
In the config.py file you can see a theme section. The teletext level I am using supports 7 colours:
@@ -53,7 +53,6 @@ You can also change the colours of a tweet poster's username, timestamp, and the
5353
Finally, the title text at the top can be changed, although there is a 30 character limit or things look wonky.
5454

5555
## Notes
56-
* At this moment in time the script reads 5 tweets. Further versions will improve on this by writing multiple tweets, possibly in subpages :-O
5756
* New tweets are grabbed every 60 seconds by default. This is configurable with the -d option, but you do have be aware of the Twitter API limits.
5857
* Emoji stripping is now included. Mostly.
5958
* Due to the limited teletext character set substitutions are implemented. Things like replacing underscores with hyphens and also making sure # works correctly. You also have to replace curly apostrophes with straight ones, as that's all the specification allows

changelog.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
## Changelog
22

3+
### v0.8
4+
- major feature addition: subpage support to handle a larger number of tweets
5+
- added cycle_time parameter to config, this is cycle time between subpages
6+
- added count argument to specify number of tweets to download
7+
- a few more bug fixes: line 24 now writable and no more dropped tweets (they go to subpages instead)
8+
- moved a few bits of code around for logic and readability
9+
310
### v0.7
4-
- compressed three different tweet output functions into one! i guess this is slower to run but i like smaller, cleaner files...
11+
- compressed three different tweet output functions into one!
512
- fixed still display bug in write_home_timeline function
613

714
### v0.6

config.py-default

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ access_key = "XXXXXXXX-xxXXxXXxxXxxxXxXXxXxXxXxxxXxxxxXxXXxXxxXX"
77
access_secret = "XxXXXXXXXXxxxXXXxXXxXxXxxXXXXXxXxxXXXXx"
88

99
# theme section
10-
# page title - 30 chars maximum
10+
# page title - 27 chars maximum
1111
page_title = "TELETEXT TWITTER"
1212
# display colours - must be in quotes! allowed colours are:
1313
# red, green, yellow, blue, magenta, cyan, white
@@ -20,3 +20,4 @@ timestamp_colour = "yellow"
2020
# miscellaneous
2121
tti_path = "/home/pi/teletext/"
2222
page_number = 153
23+
cycle_time = 20

teletext-twitter/__main__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ def parse_args():
2323

2424
parser.add_argument("-m", "--mode", type=str, help="choose between different modes - home, user or search")
2525
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")
26-
parser.add_argument("-d", "--delay", type=int, default=60, help="seconds between timeline scrapes (minimum is 60 seconds - lower values have no effect)")
27-
parser.add_argument("-v", "--version", action="version", version="0.7")
26+
parser.add_argument("-c", "--count", type=int, default=5, help="number of tweets to download (default is 5, maximum is 800)")
27+
parser.add_argument("-d", "--delay", type=int, default=60, help="seconds between timeline scrapes (default is 60 seconds - lower values have no effect)")
28+
parser.add_argument("-v", "--version", action="version", version="0.8")
2829
parser.add_argument("-Q", "--quiet", action="store_true", default=False, help="suppresses all output to the terminal except warnings and errors")
2930

3031
args = parser.parse_args()
32+
args.count = min(args.count, 800)
3133
args.delay = max(60, args.delay)
3234

3335
if args.mode == "search" and not args.query:
3436
print("[!] Search mode selected but no query specfied with -q. Exiting...", file=sys.stderr)
3537
sys.exit(1)
36-
3738
if args.mode == "user" and not args.query:
3839
print("[!] User timeline mode selected but no username specified. Exiting...", file=sys.stderr)
3940
sys.exit(1)
@@ -48,23 +49,23 @@ def main():
4849

4950
while True:
5051
try:
51-
write_header(config)
5252
if args.mode == "home":
5353
if not args.quiet:
5454
print("[*] Beginning home timeline scraping", file=sys.stdout)
55-
write_tweets(twitter_object, args.mode, config)
55+
write_tweets(twitter_object, args.mode, args.count, config)
5656
elif args.mode == "search":
5757
if not args.quiet:
5858
print("[*] Getting recent tweets containing: " + args.query, file=sys.stdout)
59-
write_tweets(twitter_object, args.mode, config, args.query)
59+
write_tweets(twitter_object, args.mode, args.count, config, args.query)
6060
elif args.mode == "user":
6161
if not args.quiet:
6262
print("[*] Getting recent tweets from user: @{}".format(args.query), file=sys.stdout)
63-
write_tweets(twitter_object, args.mode, config, args.query)
63+
write_tweets(twitter_object, args.mode, args.count, config, args.query)
6464
if not args.quiet:
6565
print("[*] Page updated. Waiting {} seconds until next scrape".format(args.delay), file=sys.stdout)
6666
except OSError as e:
67-
print("[!] Error accessing teletext data file, exiting: {}".format(e.strerror), file=sys.stderr)
67+
print("[!] Error accessing teletext data file, {}".format(e.strerror), file=sys.stderr)
68+
print("[!] Exiting...", file=sys.stderr)
6869
sys.exit(1)
6970
except twitter.error.TwitterError as e:
7071
for error in e.message:

teletext-twitter/output.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,32 @@ def write_tweet_line(file, line_num, line, config):
2929
string += "\r\n"
3030
file.write(string)
3131

32-
def write_header(config): # write a header for the page and pop a nice banner at the top
33-
page_title = config["page_title"]
32+
def write_header(subpage, config, mode): # write a header for the page and pop a nice banner at the top
33+
page_title = config["page_title"] + " " + "{:02d}".format(subpage)
3434
logo_spacer = " " * (39 - (4 + len(page_title) + 5))
35-
with open(config["tti_path"] + "P" + str(config["page_number"]) + ".tti", "w+") as file:
36-
file.write("DE,Autogenerated by Teletext-Twitter\r\n")
37-
file.write("PN," + str(config["page_number"]) + "00\r\n")
38-
file.write("SC,0000\r\n")
39-
file.write("PS,8000\r\n")
35+
with open(config["tti_path"] + "P" + str(config["page_number"]) + ".tti", mode) as file:
36+
if mode == "w+":
37+
file.write("DE,Autogenerated by Teletext-Twitter\r\n")
38+
file.write("SC,0000\r\n")
39+
file.write("PS,8000\r\n")
40+
file.write("CT," + str(config["cycle_time"]) + ",T\r\n")
41+
file.write("PN," + str(config["page_number"]) + "{:02}\r\n".format(subpage))
4042
file.write("OL,1," + ESCAPE + chr(text_colours[config["header_colour"]]) + SET_BACKGROUND +
4143
DOUBLE_HEIGHT + ESCAPE + chr(text_colours["white"]) +
4244
page_title + logo_spacer + ESCAPE + chr(mosaic_colours["cyan"]) + TWITTER_BIRD + "\r\n")
4345
file.write("OL,3," + ESCAPE + chr(mosaic_colours[config["header_separator"]]) + (chr(35) * 39) + "\r\n")
4446

45-
def write_tweets(twitter_object, mode, config, query=""): # grab the latest timeline - only 5 tweets for now
47+
def write_tweets(twitter_object, mode, count, config, query=None): # grab the latest timeline
4648
if mode == "home":
47-
statuses = twitter_object.GetHomeTimeline(count = 5)
49+
statuses = twitter_object.GetHomeTimeline(count=count)
4850
elif mode == "search":
49-
statuses = twitter_object.GetSearch(term=query, result_type="recent", count=5)
51+
statuses = twitter_object.GetSearch(term=query, result_type="recent", count=count)
5052
elif mode == "user":
51-
statuses = twitter_object.GetUserTimeline(screen_name=query, count=5)
53+
statuses = twitter_object.GetUserTimeline(screen_name=query, count=count)
54+
5255
line_position = 4
56+
subpage = 1
57+
write_header(subpage, config, "w+")
5358

5459
for status in statuses: # iterate through our responses
5560
tweet_text = clean_tweet(status.text)
@@ -58,10 +63,12 @@ def write_tweets(twitter_object, mode, config, query=""): # grab the latest time
5863
tweet_username = status.user.screen_name
5964
tweet_text = textwrap.wrap(tweet_text, 38) # make sure our lines fit on the screen
6065

61-
if (line_position + len(tweet_text) + 1) > 24: # are we going to fill the page?
62-
break # yep! dump the last tweet!
63-
6466
with open(config["tti_path"] + "P" + str(config["page_number"]) + ".tti", "a") as file:
67+
post_length = len(tweet_text) + 1 # how long is our next tweet? (including info line)
68+
if (line_position + post_length) > 25: # are we about to go over the page?
69+
subpage += 1 # yes! new page, please
70+
write_header(subpage, config, "a") # append mode to do subpages
71+
line_position = 4 # and reset our cursor
6572
write_tweet_info(file, line_position, tweet_username, tweet_human_time, config)
6673
line_position += 1
6774
for line in tweet_text:

0 commit comments

Comments
 (0)