feat(reader): autonomous Wikipedia import and reading with media, tables and settings
This commit is contained in:
+126
-7
@@ -30,7 +30,11 @@ from app.auth import (
|
||||
from app.dictionary import lookup_term, USER_AGENT, close_http_client
|
||||
from app.export import export_csv, export_json, export_anki_tsv, export_markdown
|
||||
from app.srs import calculate_sm2, generate_quiz_options, build_activity_heatmap, calculate_cefr_level
|
||||
from app.reader import parse_epub, parse_text, parse_pdf, count_words, extract_rfc_number, fetch_and_parse_rfc, parse_rfc_text
|
||||
from app.reader import (
|
||||
parse_epub, parse_text, parse_pdf, count_words, extract_rfc_number,
|
||||
fetch_and_parse_rfc, parse_rfc_text, fetch_and_parse_wikipedia,
|
||||
extract_wikipedia_title_and_lang
|
||||
)
|
||||
from app.ai import translate_sentence_ai, get_ollama_status
|
||||
from app.phrasal import detect_phrasal_verb
|
||||
from app.cefr import (
|
||||
@@ -187,6 +191,14 @@ class RfcFetchRequest(BaseModel):
|
||||
force: Optional[bool] = False
|
||||
|
||||
|
||||
class WikipediaFetchRequest(BaseModel):
|
||||
query: Optional[str] = ""
|
||||
lang: Optional[str] = "simple"
|
||||
time_budget: Optional[str] = "10-15"
|
||||
topic: Optional[str] = "random"
|
||||
force: Optional[bool] = False
|
||||
|
||||
|
||||
class SentenceTranslateRequest(BaseModel):
|
||||
sentence: str = Field(..., min_length=1)
|
||||
target_word: Optional[str] = ""
|
||||
@@ -1194,7 +1206,7 @@ async def api_get_books(user: dict = Depends(require_user)):
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
total_words, current_chapter, current_position,
|
||||
total_words, current_chapter, current_position, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books
|
||||
WHERE user_id = ?
|
||||
@@ -1309,7 +1321,7 @@ async def api_create_book(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books WHERE id = ?
|
||||
""",
|
||||
@@ -1347,7 +1359,7 @@ async def api_fetch_rfc(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books
|
||||
WHERE user_id = ? AND source_type = 'rfc' AND (title LIKE ? OR title LIKE ?)
|
||||
@@ -1388,7 +1400,114 @@ async def api_fetch_rfc(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books WHERE id = ?
|
||||
""",
|
||||
(new_id,),
|
||||
).fetchone()
|
||||
|
||||
res = dict(row)
|
||||
res["chapters"] = chapters
|
||||
res["already_existed"] = False
|
||||
return res
|
||||
|
||||
|
||||
@app.post("/api/books/fetch-wikipedia")
|
||||
async def api_fetch_wikipedia(
|
||||
req: WikipediaFetchRequest,
|
||||
user: dict = Depends(require_user),
|
||||
):
|
||||
"""
|
||||
Fetches a Wikipedia article (by title/URL or random topic & duration),
|
||||
parses sections into readable chapters with media and tables,
|
||||
and stores autonomously in the user's SQLite bookshelf (source_type = 'wikipedia').
|
||||
"""
|
||||
raw_query = (req.query or "").strip()
|
||||
target_lang = (req.lang or "simple").strip()
|
||||
time_budget = (req.time_budget or "10-15").strip()
|
||||
topic = (req.topic or "random").strip()
|
||||
|
||||
# If specific title or URL provided and force is False, check for existing book
|
||||
if raw_query and not req.force:
|
||||
title_cand, lang_cand = extract_wikipedia_title_and_lang(raw_query, default_lang=target_lang)
|
||||
search_title = title_cand.replace("_", " ")
|
||||
with get_db() as conn:
|
||||
existing = conn.execute(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books
|
||||
WHERE user_id = ? AND source_type = 'wikipedia' AND lower(title) = lower(?)
|
||||
ORDER BY id DESC LIMIT 1
|
||||
""",
|
||||
(user["id"], search_title),
|
||||
).fetchone()
|
||||
if existing:
|
||||
res = dict(existing)
|
||||
try:
|
||||
res["chapters"] = json.loads(res["chapters_json"])
|
||||
except Exception:
|
||||
res["chapters"] = []
|
||||
res["already_existed"] = True
|
||||
return res
|
||||
|
||||
try:
|
||||
title, author, chapters, cover_url, total_words, wiki_url = fetch_and_parse_wikipedia(
|
||||
title_or_url=raw_query,
|
||||
lang=target_lang,
|
||||
time_budget=time_budget,
|
||||
topic=topic,
|
||||
)
|
||||
except ValueError as ve:
|
||||
raise HTTPException(status_code=400, detail=str(ve))
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Ошибка загрузки статьи Wikipedia: {e}")
|
||||
|
||||
# Check again by resolved title if not forcing
|
||||
if not req.force:
|
||||
with get_db() as conn:
|
||||
existing = conn.execute(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books
|
||||
WHERE user_id = ? AND source_type = 'wikipedia' AND lower(title) = lower(?)
|
||||
ORDER BY id DESC LIMIT 1
|
||||
""",
|
||||
(user["id"], title),
|
||||
).fetchone()
|
||||
if existing:
|
||||
res = dict(existing)
|
||||
try:
|
||||
res["chapters"] = json.loads(res["chapters_json"])
|
||||
except Exception:
|
||||
res["chapters"] = []
|
||||
res["already_existed"] = True
|
||||
return res
|
||||
|
||||
chapters_json = json.dumps(chapters, ensure_ascii=False)
|
||||
|
||||
with get_db() as conn:
|
||||
cursor = conn.execute(
|
||||
"""
|
||||
INSERT INTO books (
|
||||
user_id, title, author, source_type, chapters_json,
|
||||
current_chapter, current_position, total_words, cover_url
|
||||
) VALUES (?, ?, ?, 'wikipedia', ?, 0, 0.0, ?, ?)
|
||||
""",
|
||||
(user["id"], title, author, chapters_json, total_words, cover_url),
|
||||
)
|
||||
new_id = cursor.lastrowid
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books WHERE id = ?
|
||||
""",
|
||||
@@ -1411,7 +1530,7 @@ async def api_get_book(book_id: int, user: dict = Depends(require_user)):
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books
|
||||
WHERE id = ? AND user_id = ?
|
||||
@@ -1488,7 +1607,7 @@ async def api_update_book_progress(
|
||||
"""
|
||||
SELECT id, user_id, title, author, source_type, chapters_json,
|
||||
json_array_length(chapters_json) AS chapters_count,
|
||||
current_chapter, current_position, total_words,
|
||||
current_chapter, current_position, total_words, cover_url,
|
||||
created_at, updated_at
|
||||
FROM books
|
||||
WHERE id = ?
|
||||
|
||||
Reference in New Issue
Block a user