From a9188fe07a2103d710c9925a5aba12e24c847a3a Mon Sep 17 00:00:00 2001 From: muskit <15199219+muskit@users.noreply.github.com> Date: Fri, 13 Jan 2023 02:55:25 -0800 Subject: [PATCH] include co. name in msg, filter mentions to co. --- src/listen.py | 7 +++++++ src/talent_lists.py | 20 +++++++++++--------- src/twapi.py | 12 +++++++++++- src/util.py | 5 ++++- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/listen.py b/src/listen.py index 7a757a2..3734f13 100644 --- a/src/listen.py +++ b/src/listen.py @@ -30,15 +30,22 @@ def run(): sc = tweepy.StreamingClient(api_secrets.bearer_token()) # clear rules + print('Clearing streaming rules...') rules_resp = sc.get_rules() if rules_resp.data: + print('Deleted a rule!') sc.delete_rules(rules_resp.data) # create new rules + print('Creating new streaming rules...') for rule in tl.get_twitter_rules(): sc.add_rules(tweepy.StreamRule(rule)) + print('--------------------------------------------') + print(sc.get_rules().data) + print('--------------------------------------------') sc.on_response=on_response + print('Starting listening stream...') sc.filter( expansions=TwAPI.TWEET_EXPANSIONS, media_fields=TwAPI.TWEET_MEDIA_FIELDS, diff --git a/src/talent_lists.py b/src/talent_lists.py index c1f1561..0044b26 100644 --- a/src/talent_lists.py +++ b/src/talent_lists.py @@ -8,7 +8,8 @@ talents = dict() test_talents = dict() -def __create_dict(file, _dict): +# TODO: talents(id) -> (name, company) +def __create_dict(file, _dict, company): print(f'Initializing talents\' account list from {file}...') global talents with open(file, 'r') as f: @@ -16,7 +17,7 @@ def __create_dict(file, _dict): words = line.split() if len(words) == 2 and line[0] != '#': name, id = line.split() - name = util.get_username_online(id, default=name) # attempt to get updated name + name = f'{util.get_username_online(id, default=name)} ({company})' # attempt to get updated name talents[int(id)] = name _dict[int(id)] = name def init(): @@ -27,13 +28,14 @@ def init(): global test_talents # holoEN - __create_dict(f'{util.get_project_dir()}/lists/holoen.txt', holo_en) + __create_dict(f'{util.get_project_dir()}/lists/holoen.txt', holo_en, 'holoEN') # holoID - __create_dict(f'{util.get_project_dir()}/lists/holoid.txt', holo_id) + __create_dict(f'{util.get_project_dir()}/lists/holoid.txt', holo_id, 'holoID') # nijiEN - __create_dict(f'{util.get_project_dir()}/lists/nijien.txt', niji_en) + __create_dict(f'{util.get_project_dir()}/lists/nijien.txt', niji_en, 'nijiEN') # nijiexID - __create_dict(f'{util.get_project_dir()}/lists/nijiexid.txt', niji_exid) + __create_dict(f'{util.get_project_dir()}/lists/nijiexid.txt', niji_exid, 'nijiex-ID') + # TODO: nijiex-KR test_talents = holo_en @@ -42,12 +44,12 @@ def get_twitter_rules(): rules = list() names = list(talents.values()) - curr_rule = f'from:{names[0]}' + curr_rule = f'from:{names[0].split()[0]}' for name in list(talents.values())[1:]: - test_rule = curr_rule + f' OR from:{name}' + test_rule = curr_rule + f' OR from:{name.split()[0]}' if len(test_rule) > 512: rules.append(curr_rule) - curr_rule = f'from:{name}' + curr_rule = f'from:{name.split()[0]}' else: curr_rule = test_rule return rules \ No newline at end of file diff --git a/src/twapi.py b/src/twapi.py index 50383ca..3ac6f9d 100644 --- a/src/twapi.py +++ b/src/twapi.py @@ -6,6 +6,7 @@ import tweepy import api_secrets import talenttweet as tt +import talent_lists as tl import util class TwAPI: @@ -22,6 +23,8 @@ class TwAPI: # media_fields=['url'], # tweet_fields=['created_at', 'in_reply_to_user_id'], # expansions=['entities.mentions.username', 'referenced_tweets.id.author_id'] + # + # VALUES IN TUPLE ARE NONE OR INT. @staticmethod def get_mrq(response): tweet = response.data @@ -54,6 +57,13 @@ class TwAPI: try: mentions.remove(qrt) except: pass + + mention_list = list(mentions) + for uid in mention_list: + if uid not in tl.talents.keys(): + mentions.remove(uid) + if reply_to not in tl.talents.keys(): + reply_to = None return (mentions, reply_to, qrt) @@ -215,7 +225,7 @@ class TwAPI: mention_usernames = [f'@/{util.get_username(x)}' for x in print_mention_ids] ret += ( 'mentioning ' - f'{" ".join(mention_usernames)}\n' + f'{", ".join(mention_usernames)}\n' ) ret += '\n' ret += '(this is a missed tweet)\n' if is_catchup else '' diff --git a/src/util.py b/src/util.py index 4f79740..7a490a5 100644 --- a/src/util.py +++ b/src/util.py @@ -1,6 +1,7 @@ ## Shared utility functions. import os +import sys import traceback from datetime import datetime @@ -52,7 +53,9 @@ def get_key_from_value(d, val): async def create_ttweet_image(ttweet): tc = TweetCapture() - tc.driver_path = '/usr/bin/chromedriver' # Linux chromedriver path + if 'linux' in sys.platform: + # Linux chromedriver path + tc.driver_path = '/usr/bin/chromedriver' filename = f'{get_project_dir()}/img.png' url = ttweet_to_url(ttweet) img = None