100 lines
3.0 KiB
Python
Executable File
100 lines
3.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Phase 1: 获取故事索引
|
|
从 API 获取所有故事链接并保存
|
|
"""
|
|
|
|
import json
|
|
import requests
|
|
import os
|
|
from datetime import datetime
|
|
|
|
CONFIG_DIR = os.path.join(os.path.dirname(__file__), '..', 'config')
|
|
INDEX_FILE = os.path.join(CONFIG_DIR, 'stories_index.json')
|
|
|
|
|
|
def fetch_story_list():
|
|
"""从 API 获取故事列表"""
|
|
url = "https://www.libraryofshortstories.com/storylist-reduced"
|
|
headers = {
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
|
|
'Accept': 'application/json',
|
|
'Accept-Encoding': 'gzip',
|
|
}
|
|
|
|
print("🔍 正在获取故事列表...")
|
|
response = requests.get(url, headers=headers, timeout=30)
|
|
response.raise_for_status()
|
|
|
|
stories = response.json()
|
|
print(f"✅ 获取到 {len(stories)} 个故事")
|
|
return stories
|
|
|
|
|
|
def build_index(stories):
|
|
"""构建索引(最多1000个)"""
|
|
index = {
|
|
'fetch_date': datetime.now().isoformat(),
|
|
'source_url': 'https://www.libraryofshortstories.com/storylist-reduced',
|
|
'total_available': len(stories),
|
|
'max_limit': 1000,
|
|
'stories': []
|
|
}
|
|
|
|
for i, story in enumerate(stories[:1000], 1):
|
|
story_entry = {
|
|
'id': i,
|
|
'title': story.get('title', 'Unknown'),
|
|
'author': story.get('author', 'Unknown'),
|
|
'code': story.get('code'),
|
|
'url': f"https://www.libraryofshortstories.com/onlinereader/{story.get('code')}",
|
|
'words': story.get('words', 0),
|
|
'publish_year': story.get('publish_year'),
|
|
'genres': story.get('genres', []),
|
|
'description': story.get('description', ''),
|
|
'status': 'pending', # pending, downloaded, failed
|
|
'download_date': None,
|
|
'error': None
|
|
}
|
|
index['stories'].append(story_entry)
|
|
|
|
return index
|
|
|
|
|
|
def save_index(index):
|
|
"""保存索引到文件"""
|
|
os.makedirs(os.path.dirname(INDEX_FILE), exist_ok=True)
|
|
|
|
with open(INDEX_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(index, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"💾 索引已保存: {INDEX_FILE}")
|
|
print(f"📊 总计: {len(index['stories'])} 个故事")
|
|
|
|
|
|
def main():
|
|
# 检查是否已有索引
|
|
if os.path.exists(INDEX_FILE):
|
|
print(f"⚠️ 索引文件已存在: {INDEX_FILE}")
|
|
with open(INDEX_FILE, 'r', encoding='utf-8') as f:
|
|
existing = json.load(f)
|
|
print(f" 上次更新: {existing.get('fetch_date')}")
|
|
print(f" 故事数量: {len(existing.get('stories', []))}")
|
|
|
|
response = input("\n是否重新获取? (y/N): ")
|
|
if response.lower() != 'y':
|
|
print("👋 保持现有索引")
|
|
return
|
|
|
|
# 获取并保存
|
|
stories = fetch_story_list()
|
|
index = build_index(stories)
|
|
save_index(index)
|
|
|
|
print("\n✅ Phase 1 完成")
|
|
print("下一步: 运行 download_stories.py 开始下载")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|