Skip to content

fix: Mikan some torrents are not a valid torrent file #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.1-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ sqlmodel==0.0.8
sse-starlette==1.6.5
semver==3.0.1
openai==0.28.1
torrentool==1.2.0
lxml==5.2.2
34 changes: 29 additions & 5 deletions backend/src/module/downloader/download_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging

from lxml import etree

from module.conf import settings
from module.models import Bangumi, Torrent
from module.network import RequestContent
from module.utils import check_torrent

from .path import TorrentPath

Expand Down Expand Up @@ -118,23 +121,44 @@ def add_torrent(self, torrent: Torrent | list, bangumi: Bangumi) -> bool:
if not bangumi.save_path:
bangumi.save_path = self._gen_save_path(bangumi)
with RequestContent() as req:

def get_torrent_or_magnet(_torrent: Torrent):
content = req.get_content(_torrent.url)
if check_torrent(content):
return content, None
if _torrent.homepage:
magnet = req.get_magnet(_torrent.homepage)
if magnet:
return None, magnet
logger.error(
f'[Downloader] {_torrent.name} torrent is corrupted; it is recommended to manually add the magnet link to qBittorrent, with the save path: "{bangumi.save_path}".'
)
return None, None

if isinstance(torrent, list):
if len(torrent) == 0:
logger.debug(f"[Downloader] No torrent found: {bangumi.official_title}")
logger.debug(
f"[Downloader] No torrent found: {bangumi.official_title}"
)
return False
if "magnet" in torrent[0].url:
torrent_url = [t.url for t in torrent]
torrent_file = None
else:
torrent_file = [req.get_content(t.url) for t in torrent]
torrent_url = None
torrent_file = []
torrent_url = []
for t in torrent:
file, magnet = get_torrent_or_magnet(t)
if file:
torrent_file.append(file)
if magnet:
torrent_url.append(magnet)
else:
if "magnet" in torrent.url:
torrent_url = torrent.url
torrent_file = None
else:
torrent_file = req.get_content(torrent.url)
torrent_url = None
torrent_file, torrent_url = get_torrent_or_magnet(torrent)
if self.client.add_torrents(
torrent_urls=torrent_url,
torrent_files=torrent_file,
Expand Down
8 changes: 8 additions & 0 deletions backend/src/module/network/request_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re
import xml.etree.ElementTree

from lxml import etree

from module.conf import settings
from module.models import Torrent

Expand Down Expand Up @@ -73,3 +75,9 @@ def get_rss_title(self, _url):
soup = self.get_xml(_url)
if soup:
return soup.find("./channel/title").text

def get_magnet(self, _url) -> str | None:
html = etree.HTML(self.get_html(_url))
magnet = html.xpath('//a[starts-with(@href, "magnet")]/@href')
if magnet:
return magnet[0]
3 changes: 2 additions & 1 deletion backend/src/module/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .cache_image import save_image, load_image
from .cache_image import save_image, load_image
from .torrent import check_torrent
11 changes: 11 additions & 0 deletions backend/src/module/utils/torrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from torrentool.bencode import Bencode
from torrentool.exceptions import BencodeDecodingError


def check_torrent(content: bytes) -> bool:
try:
if Bencode.decode(content):
return True
return False
except BencodeDecodingError:
return False