Skip to content

Commit

Permalink
Make sure no articles are skipped for each task loop (#118)
Browse files Browse the repository at this point in the history
* Add depedency

* Change logger setting

* Provide JSON data for genshin stuff

* Implement Genshin RSS feed

* Loop articles until found the latest one

* Code refactor
  • Loading branch information
Iqrar99 authored Jan 25, 2024
1 parent e63145f commit ca55845
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions bot/cogs/genshin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, bot: WarnetBot) -> None:
async def on_connect(self) -> None:
self._rss.start()

@tasks.loop(minutes=2)
@tasks.loop(minutes=10)
async def _rss(self) -> None:
info_channel = self.bot.get_channel(genshin_config.INFORMATION_CHANNEL_ID)

Expand All @@ -30,29 +30,34 @@ async def _rss(self) -> None:
latest_entry_id = data['rss']['latest_pub_id']

feed = feedparser.parse(genshin_config.RSS_FEED_URL)
new_entry = feed.entries[0]
if new_entry.id != latest_entry_id:
logger.info(f'NEW GENSHIN ARTICLE FOUND! ID={new_entry.id}')
for entry in feed.entries[:10]: # get only 10 latest articles
if entry.id != latest_entry_id:
logger.info(f'NEW GENSHIN ARTICLE FOUND! ID={entry.id}')

data['rss']['latest_pub_id'] = new_entry.id
pattern = re.compile(r'src="(https://.+\.(?:jpg|png))"')
try:
entry_image_link = pattern.search(entry.content[0].value).group(1)
except AttributeError:
entry_image_link = None

embed = discord.Embed(
title=entry.title,
url=entry.link,
description=entry.summary,
color=discord.Color.dark_embed(),
)
embed.set_image(url=entry_image_link)
await info_channel.send(embed=embed)

else:
break

newest_entry = feed.entries[0]
if data['rss']['latest_pub_id'] != newest_entry.id:
data['rss']['latest_pub_id'] = newest_entry.id
with open(genshin_config.RSS_PUBLISHED_ID_DATA_PATH, 'w') as f:
json.dump(data, f, indent=4)

pattern = re.compile(r'src="(https://.+\.(?:jpg|png))"')
try:
entry_image_link = pattern.search(new_entry.content[0].value).group(1)
except AttributeError:
entry_image_link = None

embed = discord.Embed(
title=new_entry.title,
url=new_entry.link,
description=new_entry.summary,
color=discord.Color.dark_embed(),
)
embed.set_image(url=entry_image_link)
await info_channel.send(embed=embed)

@_rss.before_loop
async def _before_rss(self):
await self.bot.wait_until_ready()
Expand Down

0 comments on commit ca55845

Please sign in to comment.