From 8f4b8679dfc671320291c310cadbeb87ae6528dd Mon Sep 17 00:00:00 2001 From: muskit <15199219+muskit@users.noreply.github.com> Date: Sun, 25 Sep 2022 18:31:50 -0700 Subject: [PATCH] fundamental progression, fixes --- README.md | 2 +- lists/holoen.txt | 2 +- queue.txt | 0 src/catchup.py | 51 ++++++++++++++++++++++++++++++++++------ src/main.py | 2 +- src/talent_lists.py | 5 +++- src/talenttweet.py | 3 ++- src/{api.py => twapi.py} | 0 src/util.py | 10 +++++--- 9 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 queue.txt rename src/{api.py => twapi.py} (100%) diff --git a/README.md b/README.md index b3e3917..ce3d6b4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ *Twitter bot that follows interactions between Nijisanji EN/ID and hololive EN/ID members.* ...because some folks are that desperate. Like me! -**This project is intended to run [this account](https://twitter.com/NijiHoloEN_Msgs).** +**This project is intended to run [this account](https://twitter.com/NijiHolo_EN_ID).** ## Roadmap * Read past tweets of members from both companies diff --git a/lists/holoen.txt b/lists/holoen.txt index 1b115a5..7a6c076 100644 --- a/lists/holoen.txt +++ b/lists/holoen.txt @@ -4,7 +4,7 @@ # --- [Myth] --- gawrgura 1283657064410017793 -watsonameliaen 283656034305769472 +watsonameliaen 1283656034305769472 moricalliope 1283653858510598144 ninomaeinanis 1283650008835743744 takanashikiara 1283646922406760448 diff --git a/queue.txt b/queue.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/catchup.py b/src/catchup.py index d218c73..7cf0396 100644 --- a/src/catchup.py +++ b/src/catchup.py @@ -11,10 +11,12 @@ import twint from util import * from talent_lists import * -from api import TwAPI +from twapi import TwAPI import talenttweet as tt -## Returns list of tweets present in queue.txt +def get_queue_file(): + return f'{util.get_project_dir()}/queue.txt' + def get_local_queue(): # f = open(os.path.join(get_project_dir(), 'queue.txt')) pass @@ -29,10 +31,17 @@ def get_user_tweet_ids(id, limit=None): c.Store_object_tweets_list = tweets c.Hide_output = True - twint.run.Search(c) - return [x.id for x in tweets] + user_str = f'{id} ({util.get_username(id)})' + print(f'Finding tweets from {user_str})') + try: + twint.run.Search(c) + return [x.id for x in tweets] + except: + print(f'Had trouble getting tweets from {user_str}') + return list() -def work_on_queue(): +def work_on_queue(file): + print('TODO: implement work_on_queue') # while Queue.txt has lines present # attempt to deserialize first line of Queue.txt # exit program if failed, stating error @@ -43,8 +52,31 @@ def work_on_queue(): # we're done! post tweet announcing done with archives pass +# If queue.txt doesn't exist, creates and populates it. +# Returns a list of sorted and filtered TalentTweets (should +# be equivalent to queue.txt) +def create_ttweets_queue(path) -> list: + print('Creating ttweets queue') + if not os.path.exists(path): + ttweets = list() + with open(path, 'x') as f: + for talent_id in talents.keys(): + tweet_ids = get_user_tweet_ids(talent_id) + print(f'retrieved {len(tweet_ids)} tweets') + for tweet_id in tweet_ids: + ttweet = tt.TalentAPITweet(tweet_id) + if ttweet.is_cross_company(): + ttweets.append(ttweet) + + ttweets.sort(key=lambda ttweet: ttweet.tweet_id) + for ttweet in ttweets: + f.write(f'{ttweet.serialize()}\n') + return ttweets + else: + return list() + + async def run(): - pass # if Queue.txt exists # work through the tweets in Queue.txt # else @@ -52,4 +84,9 @@ async def run(): # sort the list by tweet_id # create Queue.txt and save all tweets through there # post a tweet announcing archival intent - # work through the tweets in Queue.txt \ No newline at end of file + # work through the tweets in Queue.txt + queue_path = get_queue_file() + if os.path.exists(queue_path): + work_on_queue(queue_path) + else: + ttweets = create_ttweets_queue(queue_path) \ No newline at end of file diff --git a/src/main.py b/src/main.py index c01cfd1..d8c3c1d 100644 --- a/src/main.py +++ b/src/main.py @@ -9,7 +9,7 @@ import talent_lists import api_secrets import catchup import listen -from api import TwAPI +from twapi import TwAPI MODES_HELP_STR = '''mode to run the bot at: l,listen: listen for new tweets from all accounts; will not terminate unless error occurs diff --git a/src/talent_lists.py b/src/talent_lists.py index 5605957..f5c249a 100644 --- a/src/talent_lists.py +++ b/src/talent_lists.py @@ -7,14 +7,17 @@ niji_exid = dict() talents = dict() def __create_dict(file, _dict): + print(f'Initializing talents\' account list from {file}...') global talents with open(file, 'r') as f: for line in f: words = line.split() if len(words) == 2 and line[0] != '#': name, id = line.split() - _dict[int(id)] = name talents[int(id)] = name + name = util.get_username_online(id) # attempt to get updated name + talents[int(id)] = name + _dict[int(id)] = name def init(): global holo_en global holo_id diff --git a/src/talenttweet.py b/src/talenttweet.py index c8ca059..9917366 100644 --- a/src/talenttweet.py +++ b/src/talenttweet.py @@ -3,7 +3,7 @@ import platform import pytz -from api import * +from twapi import * import talent_lists class TalentTweet: @@ -121,6 +121,7 @@ class TalentAPITweet(TalentTweet): if tweet and mrq: self.tweet = tweet elif tweet_id: + tweet_id = int(tweet_id) resp = TwAPI.instance.get_tweet_response(tweet_id) self.tweet = resp.data mrq = TwAPI.get_mrq(self.tweet, resp) diff --git a/src/api.py b/src/twapi.py similarity index 100% rename from src/api.py rename to src/twapi.py diff --git a/src/util.py b/src/util.py index 08439e7..28bc8f7 100644 --- a/src/util.py +++ b/src/util.py @@ -5,11 +5,11 @@ import os import twint from tweetcapture import TweetCapture -from talent_lists import * +import talent_lists import talenttweet as tt # returns system path to this project, which is -# up one level from this file's directory (src). +# up one level from this file's directory (effective path: ..../src/../). def get_project_dir(): return os.path.join(os.path.dirname(__file__), os.pardir) @@ -43,6 +43,9 @@ def ttweet_to_url(ttweet): return f'https://twitter.com/{username}/status/{ttweet.tweet_id}' def get_username(user_id): + return talent_lists.talents.get(user_id, f'#{id}') + +def get_username_online(user_id): c = twint.Config() c.User_id = user_id c.Store_object = True @@ -50,6 +53,7 @@ def get_username(user_id): try: twint.run.Lookup(c) user = twint.output.users_list[0] + twint.output.users_list.clear() return user.username except: - return talents.get(user_id, f'#{user_id}') \ No newline at end of file + return f'#{user_id}' \ No newline at end of file