-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathanalyser.py
More file actions
107 lines (97 loc) · 4.03 KB
/
analyser.py
File metadata and controls
107 lines (97 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import logging
import re
from module.conf import settings
from module.models import Bangumi, ResponseModel, RSSItem, Torrent
from module.network import RequestContent
from module.parser import TitleParser
from .engine import RSSEngine
logger = logging.getLogger(__name__)
class RSSAnalyser(TitleParser):
async def official_title_parser(self, bangumi: Bangumi, rss: RSSItem, torrent: Torrent):
if rss.parser == "mikan":
try:
poster_link, official_title = await self.mikan_parser(
torrent.homepage
)
if official_title:
bangumi.official_title = official_title
if poster_link:
bangumi.poster_link = poster_link
except AttributeError:
logger.warning("[Parser] Mikan torrent has no homepage info.")
elif rss.parser == "tmdb":
tmdb_title, season, year, poster_link = await self.tmdb_parser(
bangumi.official_title, bangumi.season, settings.rss_parser.language
)
bangumi.official_title = tmdb_title
bangumi.year = year
bangumi.season = season
bangumi.poster_link = poster_link
else:
pass
if bangumi.official_title:
bangumi.official_title = re.sub(r"[/:.\\]", " ", bangumi.official_title)
@staticmethod
async def get_rss_torrents(rss_link: str, full_parse: bool = True) -> list[Torrent]:
async with RequestContent() as req:
if full_parse:
rss_torrents = await req.get_torrents(rss_link)
else:
rss_torrents = await req.get_torrents(rss_link, "\\d+-\\d+")
return rss_torrents
async def torrents_to_data(
self, torrents: list[Torrent], rss: RSSItem, full_parse: bool = True
) -> list:
new_data = []
seen_titles: set[str] = set()
for torrent in torrents:
bangumi = self.raw_parser(raw=torrent.name)
if bangumi and bangumi.title_raw not in seen_titles:
await self.official_title_parser(bangumi=bangumi, rss=rss, torrent=torrent)
if not full_parse:
return [bangumi]
seen_titles.add(bangumi.title_raw)
new_data.append(bangumi)
logger.info(f"[RSS] New bangumi founded: {bangumi.official_title}")
return new_data
async def torrent_to_data(self, torrent: Torrent, rss: RSSItem) -> Bangumi:
bangumi = self.raw_parser(raw=torrent.name)
if bangumi:
await self.official_title_parser(bangumi=bangumi, rss=rss, torrent=torrent)
bangumi.rss_link = rss.url
return bangumi
async def rss_to_data(
self, rss: RSSItem, engine: RSSEngine, full_parse: bool = True
) -> list[Bangumi]:
rss_torrents = await self.get_rss_torrents(rss.url, full_parse)
torrents_to_add = engine.bangumi.match_list(rss_torrents, rss.url)
if not torrents_to_add:
logger.debug("[RSS] No new title has been found.")
return []
# New List
new_data = await self.torrents_to_data(torrents_to_add, rss, full_parse)
if new_data:
# Add to database
engine.bangumi.add_all(new_data)
return new_data
else:
return []
async def link_to_data(self, rss: RSSItem) -> Bangumi | ResponseModel:
torrents = await self.get_rss_torrents(rss.url, False)
if not torrents:
return ResponseModel(
status=False,
status_code=406,
msg_en="Cannot find any torrent.",
msg_zh="无法找到种子。",
)
for torrent in torrents:
data = await self.torrent_to_data(torrent, rss)
if data:
return data
return ResponseModel(
status=False,
status_code=406,
msg_en="Cannot parse this link.",
msg_zh="无法解析此链接。",
)