Skip to content

Commit

Permalink
fix: retry failed torrent downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
KotaHv committed Jul 26, 2024
1 parent 15d8ab4 commit 9718749
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
30 changes: 24 additions & 6 deletions backend/src/module/database/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@ def __init__(self, session: Session):
self.session = session

def add(self, data: Torrent):
self.session.add(data)
result = self.session.exec(
select(Torrent).where(Torrent.hash == data.hash)
).first()
if result is None:
result = data
for key, value in data.model_dump(exclude_unset=True).items():
setattr(result, key, value)
self.session.add(result)
self.session.commit()
self.session.refresh(data)
logger.debug(f"Insert {data.name} in database.")
self.session.refresh(result)
logger.debug(f"Insert {result.name} in database.")

def add_all(self, datas: list[Torrent]):
for index, data in enumerate(datas):
result = self.session.exec(
select(Torrent).where(Torrent.hash == data.hash)
).first()
if result:
for key, value in data.model_dump(exclude_unset=True).items():
setattr(result, key, value)
datas[index] = result
self.session.add_all(datas)
self.session.commit()
logger.debug(f"Insert {len(datas)} torrents in database.")
Expand All @@ -43,6 +58,9 @@ def search(self, _id: int) -> Torrent:
def search_all(self) -> list[Torrent]:
return self.session.exec(select(Torrent)).all()

def search_all_downloaded(self) -> list[Torrent]:
return self.session.exec(select(Torrent).where(Torrent.downloaded)).all()

def search_rss(self, rss_id: int) -> list[Torrent]:
return self.session.exec(select(Torrent).where(Torrent.rss_id == rss_id)).all()

Expand All @@ -53,10 +71,10 @@ def search_bangumi(self, bangumi_id: int) -> list[Torrent]:

def check_new(self, torrents_list: list[Torrent]) -> list[Torrent]:
new_torrents = []
old_torrents = self.search_all()
old_urls = [t.url for t in old_torrents]
downloaded_torrents = self.search_all_downloaded()
downloaded_url = [t.url for t in downloaded_torrents]
for torrent in torrents_list:
if torrent.url not in old_urls:
if torrent.url not in downloaded_url:
new_torrents.append(torrent)
return new_torrents

Expand Down
3 changes: 2 additions & 1 deletion backend/src/module/downloader/download_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def add_torrent(self, torrent: Torrent | list, bangumi: Bangumi) -> bool:
with RequestContent() as req:
for t in torrent:
t.bangumi_id = bangumi.id
t.downloaded = True
if "magnet" in t.url:
torrent_urls.append(t.url)
t.hash = torrent_hash.from_magnet(t.url)
Expand All @@ -147,6 +146,8 @@ def add_torrent(self, torrent: Torrent | list, bangumi: Bangumi) -> bool:
category="Bangumi",
):
logger.debug(f"[Downloader] Add torrent: {bangumi.official_title}")
for t in torrent:
t.downloaded = True
return True
else:
logger.debug(f"[Downloader] Torrent added before: {bangumi.official_title}")
Expand Down

0 comments on commit 9718749

Please sign in to comment.