Skip to content

Commit

Permalink
feat(rss): 适配refresh rss
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyanling committed Jan 10, 2024
1 parent 30c6def commit 151b40c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
43 changes: 43 additions & 0 deletions app/rsschecker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import json
import time
import traceback
Expand Down Expand Up @@ -478,6 +479,48 @@ def __parse_userrss_result(self, taskinfo):
ExceptionUtils.exception_traceback(err)
log.error(f"【RssChecker】任务 {task_name} RSS地址 {rss_url} 获取的订阅报文无法解析:{str(err)}")
continue
elif rss_parser.get("type") == "FRESH_XML":
try:
namespaces = {"media": "http://search.yahoo.com/mrss/"}
for prefix, uri in namespaces.items():
etree.register_namespace(prefix, uri)
result_tree = etree.XML(ret.text.replace('\n', '').replace('\t', '').encode("utf-8"))
item_list = result_tree.xpath(rss_parser_format.get("list")) or []
for item in item_list:
rss_item = {}
for key, attr in rss_parser_format.get("item", {}).items():
item_html = etree.tostring(item, encoding='unicode')
if key == "enclosure":
enclosure_matches = re.findall(r'magnet:\?xt=urn:btih:(\w+)', item_html)
if enclosure_matches and len(enclosure_matches) >= 1:
value = f"magnet:?xt=urn:btih:{enclosure_matches[0]}"
rss_item.update({key: value})
else:
continue
elif key == "size":
size_matches = re.findall(r'\b(\d+(\.\d+)?)\s*([KMGTP]i?B)\b', item_html)
if size_matches and len(size_matches[0]) >= 3:
size_value = size_matches[0][0]
size_unit = size_matches[0][2]
value = f"{size_value} {size_unit}"
rss_item.update({key: value})
else:
continue
else:
if attr.get("path"):
values = item.xpath(attr.get("path", ""), namespaces=attr.get("namespaces", {}))
rss_item.update({key: values[0]})
elif attr.get("value"):
values = attr.get("value")
rss_item.update({key: values[0]})
else:
continue
rss_item.update({"address_index": i + 1})
rss_result.append(rss_item)
except Exception as err:
ExceptionUtils.exception_traceback(err)
log.error(f"【RssChecker】任务 {task_name} RSS地址 {rss_url} 获取的订阅报文无法解析:{str(err)}")
continue
elif rss_parser.get("type") == "JSON":
try:
result_json = json.loads(ret.text)
Expand Down
64 changes: 44 additions & 20 deletions scripts/sqls/init_userrss_v3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,50 @@ INSERT OR REPLACE INTO "CONFIG_RSS_PARSER" ("ID", "NAME", "TYPE", "FORMAT", "PAR
}
}', 'api_key={TMDBKEY}&language=zh-CN', '', 'Y');
INSERT OR REPLACE INTO "CONFIG_RSS_PARSER" ("ID", "NAME", "TYPE", "FORMAT", "PARAMS", "NOTE", "SYSDEF") VALUES ('5', 'Nyaa', 'XML', '{
"list": "//channel/item",
"item": {
"title": {
"path": ".//title/text()"
},
"enclosure": {
"path": ".//link/text()"
},
"link": {
"path": ".//guid/text()"
},
"date": {
"path": ".//pubDate/text()"
},
"description": {
"path": ".//description/text()"
},
"size": {
"path": "size/text()",
"namespaces": "https://nyaa.si/xmlns/nyaa"
"list":"//channel/item",
"item":{
"title":{
"path":".//title/text()"
},
"enclosure":{
"path":".//link/text()"
},
"link":{
"path":".//guid/text()"
},
"date":{
"path":".//pubDate/text()"
},
"description":{
"path":".//description/text()"
},
"size":{
"path":"size/text()",
"namespaces":"https://nyaa.si/xmlns/nyaa"
}
}
}', '', '', 'Y');
INSERT OR REPLACE INTO "CONFIG_RSS_PARSER" ("ID", "NAME", "TYPE", "FORMAT", "PARAMS", "NOTE", "SYSDEF") VALUES ('6', 'FreshRSS', 'FRESH_XML', '{
"list":"//channel/item",
"item":{
"title":{
"path":".//title/text()"
},
"enclosure":{
"path":".//media:content[@type=''application/x-bittorrent'']/@url",
"namespaces": {"media": "http://search.yahoo.com/mrss/"}
},
"link":{
"path":".//link/text()"
},
"date":{
"path":".//pubDate/text()"
},
"description":{
"path":".//description/text()"
},
"size":{
"path":".//description/a[contains(@href, ''#'')]/following-sibling::text()[1]"
}
}
}', '', '', 'Y');
5 changes: 3 additions & 2 deletions web/templates/rss/rss_parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ <h5 class="modal-title" id="modal-filterrule-title">新增解析器</h5>
title="支持XML及JSON,需要分别按XPath及JsonPath的语法维护解析格式"
data-bs-toggle="tooltip">?</span></label>
<select class="form-select" id="rssparser_type">
<option value="XML" selected>XML</option>
<option value="JSON">JSON</option>
<option value="XML" selected>XML</option>
<option value="JSON">JSON</option>
<option value="FRESH_XML">FRESH_XML</option>
</select>
</div>
</div>
Expand Down

0 comments on commit 151b40c

Please sign in to comment.