添加按订阅导出文章的参数设置
This commit is contained in:
@@ -29,6 +29,7 @@ Usage:
|
||||
python3 blogwatcher-daily.py --export markdown # 把今天的文章导出为 Markdown
|
||||
python3 blogwatcher-daily.py --export html --date 2026-08-28
|
||||
python3 blogwatcher-daily.py --export txt --limit 20
|
||||
python3 blogwatcher-daily.py --export markdown --sub "异次元软件世界" --limit 20 # 单订阅最新 20 篇
|
||||
|
||||
订阅标识(SUB):
|
||||
可传 --list 输出中的整数序号(如 3),也可传订阅名(精确 → 忽略大小写精确 → 忽略大小写包含)。
|
||||
@@ -437,8 +438,9 @@ def delete_articles_for_subscription(sub, article_index=None):
|
||||
return True
|
||||
|
||||
# ========== 导出 ==========
|
||||
def fetch_articles(target_date=None, limit=None):
|
||||
"""从 articles 表读取;可选按本地日期过滤(date(fetched_at,'localtime'))+ 可选限制数量"""
|
||||
def fetch_articles(target_date=None, limit=None, feed_url=None):
|
||||
"""从 articles 表读取;可选按本地日期过滤(date(fetched_at,'localtime'))
|
||||
+ 可选按订阅 feed_url 过滤 + 可选限制数量。"""
|
||||
if not os.path.exists(DB_PATH):
|
||||
print(f"❌ 数据库不存在: {DB_PATH}", file=sys.stderr)
|
||||
return []
|
||||
@@ -446,11 +448,15 @@ def fetch_articles(target_date=None, limit=None):
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
where = ""
|
||||
conditions = []
|
||||
params = []
|
||||
if target_date:
|
||||
where = "WHERE date(fetched_at, 'localtime') = ?"
|
||||
conditions.append("date(fetched_at, 'localtime') = ?")
|
||||
params.append(target_date)
|
||||
if feed_url:
|
||||
conditions.append("feed_url = ?")
|
||||
params.append(feed_url)
|
||||
where = ("WHERE " + " AND ".join(conditions)) if conditions else ""
|
||||
|
||||
query = f"""
|
||||
SELECT channel_title, title, link, description, pub_date, fetched_at
|
||||
@@ -550,14 +556,21 @@ FORMATTERS = {
|
||||
"html": format_html,
|
||||
}
|
||||
|
||||
def export_articles(fmt, target_date, limit):
|
||||
articles = fetch_articles(target_date, limit)
|
||||
def export_articles(fmt, target_date, limit, sub=None):
|
||||
feed_url = sub['url'] if sub else None
|
||||
articles = fetch_articles(target_date, limit, feed_url=feed_url)
|
||||
|
||||
label_parts = []
|
||||
if sub:
|
||||
label_parts.append(sub['name'])
|
||||
if target_date:
|
||||
label = target_date
|
||||
elif limit:
|
||||
label = f"最新 {limit} 篇"
|
||||
else:
|
||||
label = "全部"
|
||||
label_parts.append(target_date)
|
||||
if limit:
|
||||
label_parts.append(f"最新 {limit} 篇")
|
||||
if not label_parts:
|
||||
label_parts.append("全部")
|
||||
label = " — ".join(label_parts)
|
||||
|
||||
print(f"📊 {len(articles)} 篇文章 → {fmt}", file=sys.stderr)
|
||||
sys.stdout.write(FORMATTERS[fmt](articles, label))
|
||||
|
||||
@@ -585,6 +598,8 @@ def main():
|
||||
parser.add_argument('--date', help='导出的目标日期 YYYY-MM-DD(默认今天,仅 --export 有效)')
|
||||
parser.add_argument('--limit', type=int,
|
||||
help='导出的文章数上限;单独使用时忽略日期取全库最新 N 篇(仅 --export 有效)')
|
||||
parser.add_argument('--sub', metavar='SUB',
|
||||
help='与 --export 搭配:只导出指定订阅的文章(SUB=--list 序号或名称)')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -592,6 +607,8 @@ def main():
|
||||
parser.error("--unread 必须与 --articles 一起使用")
|
||||
if args.article_id is not None and not args.delete:
|
||||
parser.error("--article-id 必须与 --delete 一起使用")
|
||||
if args.sub and not args.export:
|
||||
parser.error("--sub 必须与 --export 一起使用")
|
||||
|
||||
global RSSHUB_BASE
|
||||
if args.rsshub:
|
||||
@@ -642,13 +659,18 @@ def main():
|
||||
sys.exit(1)
|
||||
|
||||
elif args.export:
|
||||
sub = None
|
||||
if args.sub:
|
||||
sub = find_subscription(args.sub)
|
||||
if not sub:
|
||||
sys.exit(1)
|
||||
if args.date:
|
||||
target_date = args.date
|
||||
elif args.limit:
|
||||
elif args.limit or sub:
|
||||
target_date = None
|
||||
else:
|
||||
target_date = date.today().isoformat()
|
||||
export_articles(args.export, target_date, args.limit)
|
||||
export_articles(args.export, target_date, args.limit, sub=sub)
|
||||
|
||||
else:
|
||||
scan_all(force_all=args.all)
|
||||
|
||||
Reference in New Issue
Block a user