X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=inline;f=twitter_renderer.py;h=23c36f87131d64a12591f84f5c2e7b012a2479c8;hb=05b56fa9d94c3dac31c08b28b8e6adfe7d8d2a4a;hp=f2859f138ac3fd6b4e06c73132db469ece4f58dd;hpb=c06bfef53f70551e7920bc4facce27f47b89e2ba;p=kiosk.git diff --git a/twitter_renderer.py b/twitter_renderer.py index f2859f1..23c36f8 100644 --- a/twitter_renderer.py +++ b/twitter_renderer.py @@ -1,23 +1,28 @@ #!/usr/bin/env python3 +import logging import random import re -import tweepy -from typing import Dict, List +from typing import Dict, List, Optional + +import tweepy # type: ignore +from scottutilz import profanity_filter import file_writer import renderer -import profanity_filter -import secrets +import kiosk_secrets as secrets + + +logger = logging.getLogger(__name__) -class twitter_renderer(renderer.debuggable_abstaining_renderer): +class twitter_renderer(renderer.abstaining_renderer): def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None: - super(twitter_renderer, self).__init__(name_to_timeout_dict, False) + super().__init__(name_to_timeout_dict) self.debug = True - self.tweets_by_author = {} - self.handles_by_author = {} - self.filter = profanity_filter.profanity_filter() + self.tweets_by_author: Dict[str, List[tweepy.models.Status]] = {} + self.handles_by_author: Dict[str, str] = {} + self.filter = profanity_filter.ProfanityFilter() self.urlfinder = re.compile( "((http|https)://[\-A-Za-z0-9\\.]+/[\?\&\-A-Za-z0-9_\\.]+)" ) @@ -54,24 +59,50 @@ class twitter_renderer(renderer.debuggable_abstaining_renderer): elif key == "Shuffle Tweets": return self.shuffle_tweets() else: - raise error("Unexpected operation") + raise Exception("Unexpected operation") def fetch_tweets(self) -> bool: try: tweets = self.api.home_timeline(tweet_mode="extended", count=200) - except: + except Exception as e: + logger.exception(e) print("Exception while fetching tweets!") return False for tweet in tweets: + # j = tweet._json + # import json + # print(json.dumps(j, indent=4, sort_keys=True)) + # print("------") + author = tweet.author.name author_handle = tweet.author.screen_name self.handles_by_author[author] = author_handle if author not in self.tweets_by_author: self.tweets_by_author[author] = [] - l = self.tweets_by_author[author] - l.append(tweet) + x = self.tweets_by_author[author] + x.append(tweet) return True + def get_hashtags(self, tweet) -> str: + ret = " " + if "entities" in tweet._json: + entities = tweet._json["entities"] + if "hashtags" in entities: + for x in entities["hashtags"]: + ret += f'#{x["text"]}, ' + ret = re.sub(", $", "", ret) + return ret + + def get_media_url(self, tweet) -> Optional[str]: + if "entities" in tweet._json: + entities = tweet._json["entities"] + if "media" in entities: + media = entities["media"] + for x in media: + if "media_url_https" in x: + return x["media_url_https"] + return None + def shuffle_tweets(self) -> bool: authors = list(self.tweets_by_author.keys()) author = random.choice(authors) @@ -89,11 +120,17 @@ class twitter_renderer(renderer.debuggable_abstaining_renderer): for tweet in tweets: text = tweet.full_text if (text not in already_seen) and ( - not self.filter.contains_bad_words(text) + not self.filter.contains_bad_word(text) ): already_seen.add(text) text = self.linkify(text) - f.write("
{text} | " + text += f'