Skip to content

Commit

Permalink
feat: new async rss poller.
Browse files Browse the repository at this point in the history
  • Loading branch information
EstrellaXD committed Jan 3, 2024
1 parent b1707e6 commit 62d99eb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend/src/module/core/sub_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self):

async def __loop_mission(self):
async with RSSEngine() as engine:
await engine.rss_checker(self.analyser, self.stop_event)
await engine.rss_poller(self.analyser, self.stop_event)

def rss_loop(self):
asyncio.run(self.__loop_mission())
Expand Down
14 changes: 9 additions & 5 deletions backend/src/module/rss/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ def __init__(self):
self._to_refresh = False
self.db_status = False

async def rss_checker(self, callback: Callable = None):
while 1:
async def rss_poller(self, callback: Callable = None):
# Main RSS Loop
while True:
with Database() as database:
rss_items = database.rss.search_active()
if rss_items:
tasks = []
for item in rss_items:
tasks.append(self.pull_rss(item, database, callback))
await asyncio.gather(*tasks)
await asyncio.sleep(settings.rss.interval)
await asyncio.gather(*tasks)
await asyncio.sleep(settings.program.rss_time)

@staticmethod
async def _get_torrents(rss: RSSItem) -> list[Torrent]:
Expand All @@ -38,12 +39,13 @@ async def _get_torrents(rss: RSSItem) -> list[Torrent]:
return torrents

async def pull_rss(
self, rss_item: RSSItem, database: Database, callback: Callable = None, **kwargs
self, rss_item: RSSItem, database: Database = None, callback: Callable = None, **kwargs
) -> list[Torrent]:
torrents = await self._get_torrents(rss_item)
new_torrents = database.torrent.check_new(torrents)
if callback:
await callback(rss_item, new_torrents, **kwargs)
database.torrent.add_all(new_torrents)
return new_torrents

@staticmethod
Expand Down Expand Up @@ -118,3 +120,5 @@ async def download_bangumi(bangumi: Bangumi, database: Database):
@staticmethod
def __connect_database():
return Database(engine)


6 changes: 3 additions & 3 deletions backend/src/module/update/cross_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from urllib3.util import parse_url

from module.rss import RSSEngine
from module.rss import RSSManager
from module.utils import save_image
from module.network import RequestContent


async def from_30_to_31():
async with RSSEngine() as db:
async with RSSManager() as db:
db.migrate()
# Update poster link
bangumis = db.bangumi.search_all()
Expand All @@ -33,7 +33,7 @@ async def from_30_to_31():


async def cache_image():
async with RSSEngine() as db, RequestContent() as req:
async with RSSManager() as db, RequestContent() as req:
bangumis = db.bangumi.search_all()
for bangumi in bangumis:
if bangumi.poster_link:
Expand Down

0 comments on commit 62d99eb

Please sign in to comment.