From 1a7275c08ea2357acfb8fba1065066f498c10b45 Mon Sep 17 00:00:00 2001 From: Mathew Cha Date: Thu, 25 Apr 2024 14:04:49 -0700 Subject: [PATCH] Remove Twitter plugin --- ircbot/ircbot.py | 8 +--- ircbot/plugin/twitter.py | 84 ---------------------------------------- 2 files changed, 1 insertion(+), 91 deletions(-) delete mode 100644 ircbot/plugin/twitter.py diff --git a/ircbot/ircbot.py b/ircbot/ircbot.py index 11626b2..9376476 100644 --- a/ircbot/ircbot.py +++ b/ircbot/ircbot.py @@ -112,7 +112,6 @@ def __init__( googlesearch_key, googlesearch_cx, kanboard_apikey, - twitter_apikeys, ): self.recent_messages: DefaultDict[str, Any] = collections.defaultdict( functools.partial(collections.deque, maxlen=NUM_RECENT_MESSAGES), @@ -127,7 +126,6 @@ def __init__( self.googlesearch_key = googlesearch_key self.googlesearch_cx = googlesearch_cx self.kanboard_apikey = kanboard_apikey - self.twitter_apikeys = twitter_apikeys self.listeners: Set[Listener] = set() self.plugins: Dict[str, ModuleType] = {} self.extra_channels: Set[str] = set() # plugins can add stuff here @@ -432,15 +430,11 @@ def main(): googlesearch_key = conf.get('googlesearch', 'key') googlesearch_cx = conf.get('googlesearch', 'cx') kanboard_apikey = conf.get('kanboard', 'apikey') - twitter_apikeys = ( - conf.get('twitter', 'apikey'), - conf.get('twitter', 'apisecret'), - ) bot = CreateBot( celery_conf, nickserv_password, rt_password, weather_apikey, mysql_password, googlesearch_key, googlesearch_cx, - kanboard_apikey, twitter_apikeys, + kanboard_apikey, ) # Start the bot! diff --git a/ircbot/plugin/twitter.py b/ircbot/plugin/twitter.py deleted file mode 100644 index 712163c..0000000 --- a/ircbot/plugin/twitter.py +++ /dev/null @@ -1,84 +0,0 @@ -"""Post the contents of tweets.""" -import html -from functools import lru_cache - -import requests - -TWITTER_API = 'https://api.twitter.com' - - -def register(bot): - bot.listen(r'https?://(?:mobile\.|www\.|m\.)?twitter\.com/[^/]+/status/([0-9]+)', show_tweet) - - -@lru_cache(maxsize=1) -def _get_token(apikeys): - resp = requests.post( - f'{TWITTER_API}/oauth2/token', - data={'grant_type': 'client_credentials'}, - auth=apikeys, - ) - resp.raise_for_status() - - authorization = resp.json() - assert authorization['token_type'] == 'bearer' - return authorization['access_token'] - - -def _get_tweet(apikeys, status_id, retry=True): - bearer_token = _get_token(apikeys) - - resp = requests.get( - '{}/1.1/statuses/show.json?id={}&tweet_mode=extended'.format( - TWITTER_API, - status_id, - ), headers={ - 'Authorization': f'Bearer {bearer_token}', - }, - ) - if resp.status_code == 404 or resp.status_code == 403: - # 403 indicates protected account, so just give up - return None - if resp.status_code == 401 and retry: - _get_token.cache_clear() - # make sure not to get stuck in an infinite loop of 401s - return _get_tweet(apikeys, status_id, False) - resp.raise_for_status() - - return resp.json() - - -def _format_media(media, url): - media_urls = [ - medium['media_url_https'] - for medium in media if medium['type'] == 'photo' - ] - if any([medium['type'] != 'photo' for medium in media]): - media_urls += [url] - return ' '.join(media_urls) - - -def _format_tweet(tweet): - contents = html.unescape(tweet['full_text']) - media = tweet.get('extended_entities', {}).get('media') - if media: - # url is the same for all media - url = media[0]['url'] - contents = contents.replace( - url, - _format_media(media, url), - ) - contents = contents.replace('\n', ' ') - handle = tweet['user']['screen_name'] - return '@\x02{handle}\x02 ({realname}): {contents}'.format( - handle=handle, - realname=tweet['user']['name'], - contents=contents, - ) - - -def show_tweet(bot, msg): - """Show the user and content of a linked tweet.""" - tweet = _get_tweet(bot.twitter_apikeys, msg.match.group(1)) - if tweet: - msg.respond(_format_tweet(tweet), ping=False)