From 961b6abb4032c8ac8281ac9cde39a846fb83e951 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 22 Jan 2024 13:57:27 +0800 Subject: [PATCH] fix: TweetDB update too late --- defs/sqlite.py | 6 ++++++ defs/update.py | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/defs/sqlite.py b/defs/sqlite.py index 13855b5..97f468e 100644 --- a/defs/sqlite.py +++ b/defs/sqlite.py @@ -26,6 +26,12 @@ class TweetDB: tweets.append(tid) db[f"{TweetDB.prefix}{username}"] = tweets + @staticmethod + def remove(username: str, tid: int) -> None: + tweets = TweetDB.get_all(username) + tweets.remove(tid) + db[f"{TweetDB.prefix}{username}"] = tweets + class UserDB: @staticmethod diff --git a/defs/update.py b/defs/update.py index cb80b10..1975226 100644 --- a/defs/update.py +++ b/defs/update.py @@ -95,10 +95,7 @@ async def send_username_changed(user: str): async def send_check(user_data: User) -> bool: - need_send_tweets = [ - tweet for tweet in user_data.tweets - if not TweetDB.check_id(user_data.username, tweet.id) - ] + need_send_tweets = user_data.tweets if len(need_send_tweets): logs.info(f"{user_data.name} (@{user_data.username}) 需要推送 {len(need_send_tweets)} 条推文") for tweet in need_send_tweets: @@ -106,14 +103,24 @@ async def send_check(user_data: User) -> bool: await send_to_user(user_data, tweet) except Exception: logs.error(f"推送 {user_data.name} 的推文 {tweet.id} 失败") - TweetDB.add(user_data.username, tweet.id) + TweetDB.remove(user_data.username, tweet.id) return len(need_send_tweets) > 0 +def filter_tweets(username: str, data: List[Tweet]) -> List[Tweet]: + tweets = [] + for tweet in data: + if not TweetDB.check_id(username, tweet.id): + TweetDB.add(username, tweet.id) + tweets.append(tweet) + return tweets + + async def async_get_user(user_data: Dict, username: str) -> None: try: data = await get_user(username) if data: + data.tweets = filter_tweets(username, data.tweets) user_data[username] = data else: user_data[username] = None