This commit is contained in:
Zeva Rose 2022-02-19 20:05:40 -07:00
parent e453762d37
commit f48af6fc80

View file

@ -29,14 +29,16 @@ async def twitter(bot: Snake, logger: Logger) -> None:
# Go through all actively followed accounts # Go through all actively followed accounts
async for account in accounts: async for account in accounts:
logger.debug(f"Checking account {account.handle}")
# Check if account needs updated (handle changes) # Check if account needs updated (handle changes)
if account.last_sync + timedelta(hours=1) <= datetime.utcnow(): if account.last_sync + timedelta(hours=1) <= datetime.utcnow():
logger.debug(f"Account {account.handle} out of sync, updating") logger.debug(f"Account {account.handle} out of sync, updating")
user = api.get_user(id=account.twitter_id) user = api.get_user(user_id=account.twitter_id)
account.update(q(handle=user.screen_name, last_sync=datetime.utcnow())) account.update(q(handle=user.screen_name, last_sync=datetime.utcnow()))
# Get new tweets # Get new tweets
if tweets := api.user_timeline(id=account.twitter_id, since_id=account.last_tweet): if tweets := api.user_timeline(user_id=account.twitter_id, since_id=account.last_tweet):
logger.debug(f"{account.handle} has new tweets")
tweets = sorted(tweets, key=lambda x: x.id) tweets = sorted(tweets, key=lambda x: x.id)
follows = TwitterFollow.find(q(twitter_id=account.twitter_id)) follows = TwitterFollow.find(q(twitter_id=account.twitter_id))
follows_to_delete = [] follows_to_delete = []
@ -45,12 +47,12 @@ async def twitter(bot: Snake, logger: Logger) -> None:
# Go through follows and send tweet if necessary # Go through follows and send tweet if necessary
async for follow in follows: async for follow in follows:
num_follows += 1 num_follows += 1
guild = await bot.fetch_guild(follow.guild_id) guild = await bot.fetch_guild(follow.guild)
if not guild: if not guild:
logger.warning(f"Follow {follow.id}'s guild no longer exists, deleting") logger.warning(f"Follow {follow.id}'s guild no longer exists, deleting")
follows_to_delete.append(follow) follows_to_delete.append(follow)
continue continue
channel = await guild.fetch_channel(follow.channel_id) channel = await bot.fetch_channel(follow.channel)
if not channel: if not channel:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting") logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
follows_to_delete.append(follow) follows_to_delete.append(follow)
@ -74,7 +76,7 @@ async def twitter(bot: Snake, logger: Logger) -> None:
if num_follows == 0: if num_follows == 0:
accounts_to_delete.append(account) accounts_to_delete.append(account)
else: else:
newest = tweets[0] newest = tweets[-1]
account.update(q(last_tweet=newest.id)) account.update(q(last_tweet=newest.id))
await account.commit() await account.commit()