Skip to content

Commit

Permalink
v0.7 - small bug fix and extensive code refactoring in output module
Browse files Browse the repository at this point in the history
  • Loading branch information
mpentler committed Feb 8, 2018
1 parent 0f6b2c0 commit 4c4d47a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### v0.7
- compressed three different tweet output functions into one! i guess this is slower to run but i like smaller, cleaner files...
- fixed still display bug in write_home_timeline function

### v0.6
- New mode added: show latest tweets from a certain user
- cmd line switches changed to accommodate user mode
Expand Down
8 changes: 4 additions & 4 deletions teletext-twitter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def parse_args():
parser.add_argument("-m", "--mode", type=str, help="choose between different modes - home, user or search")
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")
parser.add_argument("-d", "--delay", type=int, default=60, help="seconds between timeline scrapes (minimum is 60 seconds - lower values have no effect)")
parser.add_argument("-v", "--version", action="version", version="0.6")
parser.add_argument("-v", "--version", action="version", version="0.7")
parser.add_argument("-Q", "--quiet", action="store_true", default=False, help="suppresses all output to the terminal except warnings and errors")

args = parser.parse_args()
Expand Down Expand Up @@ -52,15 +52,15 @@ def main():
if args.mode == "home":
if not args.quiet:
print("[*] Beginning home timeline scraping", file=sys.stdout)
write_home_timeline(twitter_object, config)
write_tweets(twitter_object, args.mode, config)
elif args.mode == "search":
if not args.quiet:
print("[*] Getting recent tweets containing: " + args.query, file=sys.stdout)
write_search_term(twitter_object, args.query, config)
write_tweets(twitter_object, args.mode, config, args.query)
elif args.mode == "user":
if not args.quiet:
print("[*] Getting recent tweets from user: @{}".format(args.query), file=sys.stdout)
write_user_timeline(twitter_object, args.query, config)
write_tweets(twitter_object, args.mode, config, args.query)
if not args.quiet:
print("[*] Page updated. Waiting {} seconds until next scrape".format(args.delay), file=sys.stdout)
except OSError as e:
Expand Down
51 changes: 7 additions & 44 deletions teletext-twitter/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,13 @@ def write_header(config): # write a header for the page and pop a nice banner at
page_title + logo_spacer + ESCAPE + chr(mosaic_colours["cyan"]) + TWITTER_BIRD + "\r\n")
file.write("OL,3," + ESCAPE + chr(mosaic_colours[config["header_separator"]]) + (chr(35) * 39) + "\r\n")

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

for status in statuses: # iterate through our responses
tweet_text = clean_tweet(status.text)
tweet_time = time.strptime(status.created_at,"%a %b %d %H:%M:%S +0000 %Y")
tweet_human_time = time.strftime("%d-%b-%Y %H:%M", tweet_time) # reformat time/date output
tweet_username = status.user.screen_name
tweet_text = textwrap.wrap(tweet_text, 38) # make sure our lines fit on the screen

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

with open(config["tti_path"] + "P" + str(config["page_number"]) + ".tti", "a") as file:
write_tweet_info(file, line_position, tweet_username, tweet_human_time, config)
line_position += 1
for line in tweet_text:
write_tweet_line(file, line_position, line, config)
line_position += 11

def write_search_term(twitter_object, search_term, config): # search recent tweets with a particular search term
statuses = twitter_object.GetSearch(term=search_term, result_type="recent", count=5)
line_position = 4

for status in statuses: # iterate through our responses
tweet_text = clean_tweet(status.text) # process the tweet text
tweet_time = time.strptime(status.created_at,"%a %b %d %H:%M:%S +0000 %Y")
tweet_human_time = time.strftime("%d-%b-%Y %H:%M", tweet_time) # reformat time/date output
tweet_username = status.user.screen_name
tweet_text = textwrap.wrap(tweet_text, 38) # make sure our lines fit on the screen

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

with open(config["tti_path"] + "P" + str(config["page_number"]) + ".tti", "a") as file:
write_tweet_info(file, line_position, tweet_username, tweet_human_time, config)
line_position += 1
for line in tweet_text:
write_tweet_line(file, line_position, line, config)
line_position += 1

def write_user_timeline(twitter_object, username, config):
statuses = twitter_object.GetUserTimeline(screen_name=username, count=5)
def write_tweets(twitter_object, mode, config, query=""): # grab the latest timeline - only 5 tweets for now
if mode == "home":
statuses = twitter_object.GetHomeTimeline(count = 5)
elif mode == "search":
statuses = twitter_object.GetSearch(term=query, result_type="recent", count=5)
elif mode == "user":
statuses = twitter_object.GetUserTimeline(screen_name=query, count=5)
line_position = 4

for status in statuses: # iterate through our responses
Expand Down

0 comments on commit 4c4d47a

Please sign in to comment.