Skip to content

Commit 415350c

Browse files
author
farfarfun
committed
Bump version to 1.1.71, refactor source loaders to use iter_source_download_data
1 parent 23884dd commit 415350c

4 files changed

Lines changed: 61 additions & 48 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "funread"
3-
version = "1.1.70"
3+
version = "1.1.71"
44
description = "一个用于管理和处理阅读源(Legado 阅读 APP 的书源和 RSS 源)的 Python 工具库"
55
readme = "README.md"
66
requires-python = ">=3.8"

src/funread/legado/manage/download/book.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""书源下载和管理模块"""
22

3-
from typing import Any, Dict, List
3+
from typing import Any, Dict
44

55
from funread.legado.manage.download.base import DownloadSource
6+
from funread.legado.manage.source_download import iter_source_download_data
67
from funread.legado.manage.utils import retain_zh_ch_dig
78

89

@@ -130,11 +131,6 @@ def format_toc(self):
130131
self.__format_base("ruleToc", map)
131132

132133

133-
BOOK_SOURCE_URLS: List[str] = [
134-
"https://bitbucket.org/xiu2/yuedu/raw/master/shuyuan",
135-
]
136-
137-
138134
class BookSourceDownload(DownloadSource):
139135
"""书源下载器,继承自 DownloadSource"""
140136

@@ -143,9 +139,9 @@ def get_source_url_key(self) -> str:
143139
return "bookSourceUrl"
144140

145141
def loader(self) -> None:
146-
"""加载公开书源列表"""
147-
for url in BOOK_SOURCE_URLS:
148-
self.add_sources(url)
142+
"""从 URL 记录迭代器中加载书源数据"""
143+
for _, data in iter_source_download_data(source_type=self.cate1):
144+
self.add_sources(data)
149145

150146
def source_format(self, source: Dict[str, Any]) -> Dict[str, Any]:
151147
"""

src/funread/legado/manage/download/rss.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22

33
from typing import Any, Dict
44

5-
import requests
6-
from funfake.headers import Headers
7-
from nltlog import getLogger
8-
from funutil.cache import disk_cache
9-
from tqdm import tqdm
10-
115
from funread.legado.manage.download.base import DownloadSource
6+
from funread.legado.manage.source_download import iter_source_download_data
127
from funread.legado.manage.utils import retain_zh_ch_dig
138

14-
logger = getLogger("funread")
15-
faker = Headers()
16-
179

1810
class RSSSourceFormat:
1911
"""RSS 源格式化类,用于统一 RSS 源格式"""
@@ -85,32 +77,7 @@ def source_format(self, source: Dict[str, Any]) -> Dict[str, Any]:
8577
"""
8678
return RSSSourceFormat(source).run()
8779

88-
def loader(self):
89-
urls = [
90-
"https://agit.ai/butterfly/yd/raw/branch/yd/迷迭订阅源.json",
91-
]
92-
urls.extend(
93-
[f"https://www.yckceo.com/yuedu/rsss/json/id/{_id}.json" for _id in range(0, 250)]
94-
)
95-
urls.extend(
96-
[f"https://www.yckceo.com/yuedu/rss/json/id/{_id}.json" for _id in range(0, 500)]
97-
)
98-
99-
cache_path = f"{self.path_rot}/../cache"
100-
logger.info(f"cache_path:{cache_path}")
101-
102-
@disk_cache(cache_key="url", cache_dir=cache_path, expire=3600 * 24 * 7)
103-
def load_data(url: str) -> dict:
104-
try:
105-
return requests.get(url, headers=faker.generate()).json()
106-
except Exception as e:
107-
# logger.error(f"error: {e},traceback: {traceback.format_exc()}")
108-
return {}
109-
110-
for _url in tqdm(urls):
111-
try:
112-
data = load_data(_url)
113-
self.persist_download_record(_url, data)
114-
self.add_sources(data)
115-
except Exception as e:
116-
logger.info(f"error:{e}")
80+
def loader(self) -> None:
81+
"""从 URL 记录迭代器中加载 RSS 源数据"""
82+
for _, data in iter_source_download_data(source_type=self.cate1):
83+
self.add_sources(data)

tests/test_download_base.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from pathlib import Path
22

3+
import funread.legado.manage.download.book as book_module
4+
import funread.legado.manage.download.rss as rss_module
5+
36
from funread.legado.manage.download.base import DownloadSource
47
from funread.legado.manage.download.book import BookSourceDownload
58

@@ -52,3 +55,50 @@ def test_book_source_download_accepts_book_source_url(tmp_path: Path) -> None:
5255
assert added is True
5356
exported = next(source.export_sources(size=10))
5457
assert exported[0]["bookSourceUrl"].startswith("https://books.example.com/api#")
58+
59+
60+
def test_book_loader_reads_source_download_iterator(monkeypatch, tmp_path: Path) -> None:
61+
source = book_module.BookSourceDownload(path=str(tmp_path), cate1="book")
62+
63+
monkeypatch.setattr(
64+
book_module,
65+
"iter_source_download_data",
66+
lambda source_type: iter(
67+
[
68+
(
69+
object(),
70+
{
71+
"bookSourceUrl": "https://books.example.com/api/",
72+
"bookSourceName": "Example Book",
73+
},
74+
)
75+
]
76+
),
77+
)
78+
79+
source.loader()
80+
81+
exported = next(source.export_sources(size=10))
82+
assert exported[0]["bookSourceUrl"].startswith("https://books.example.com/api#")
83+
84+
85+
def test_rss_loader_reads_source_download_iterator(monkeypatch, tmp_path: Path) -> None:
86+
source = rss_module.RSSSourceDownload(path=str(tmp_path), cate1="rss")
87+
88+
monkeypatch.setattr(
89+
rss_module,
90+
"iter_source_download_data",
91+
lambda source_type: iter(
92+
[
93+
(
94+
object(),
95+
{"sourceUrl": "https://rss.example.com/feed/", "sourceName": "Example RSS"},
96+
)
97+
]
98+
),
99+
)
100+
101+
source.loader()
102+
103+
exported = next(source.export_sources(size=10))
104+
assert exported[0]["sourceUrl"].startswith("https://rss.example.com/feed#")

0 commit comments

Comments
 (0)