-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (174 loc) · 7.03 KB
/
Copy pathmain.py
File metadata and controls
214 lines (174 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import asyncio
import os
from urllib.parse import urlparse
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import Optional
from app.database import (
init_db, create_episode, update_episode, get_episode, get_all_episodes, delete_episode,
save_extractions, get_extractions, update_extraction_anki_id, delete_extraction,
clear_extractions, record_listen, get_listens, delete_listen, get_due_relistens,
get_upcoming_relistens, complete_relisten,
)
from app.download import download_audio
from app.transcribe import transcribe_audio
from app.extract import extract_study_material
from app import anki
app = FastAPI(title="JLPT Podcast Study")
# --- Startup ---
@app.on_event("startup")
def startup():
init_db()
# --- Models ---
class EpisodeCreate(BaseModel):
url: str
jlpt_level: str = "N3"
class ListenCreate(BaseModel):
notes: Optional[str] = None
class ExtractionIds(BaseModel):
extraction_ids: list[int]
# --- Background processing pipeline ---
def process_episode(episode_id: int, url: str, jlpt_level: str):
"""Full pipeline: download → transcribe → extract."""
try:
update_episode(episode_id, status="downloading")
audio_path, title = download_audio(url, episode_id)
update_episode(episode_id, audio_path=audio_path, title=title, status="transcribing")
transcript = transcribe_audio(audio_path)
update_episode(episode_id, transcript=transcript, status="extracting")
items = extract_study_material(transcript, jlpt_level)
save_extractions(episode_id, items)
update_episode(episode_id, status="ready")
except Exception as e:
update_episode(episode_id, status=f"error: {str(e)[:200]}")
raise
# --- API Routes ---
@app.post("/api/episodes")
async def create_new_episode(data: EpisodeCreate, bg: BackgroundTasks):
"""Submit a new podcast/youtube URL for processing."""
parsed = urlparse(data.url)
if parsed.scheme not in ('http', 'https'):
raise HTTPException(400, "URL must start with http:// or https://")
eid = create_episode(data.url, jlpt_level=data.jlpt_level)
bg.add_task(process_episode, eid, data.url, data.jlpt_level)
return {"id": eid, "status": "processing"}
@app.get("/api/episodes")
async def list_episodes():
return get_all_episodes()
@app.get("/api/episodes/{eid}")
async def get_episode_detail(eid: int):
ep = get_episode(eid)
if not ep:
raise HTTPException(404, "Episode not found")
ep["extractions"] = get_extractions(eid)
ep["listens"] = get_listens(eid)
return ep
@app.get("/api/episodes/{eid}/extractions")
async def get_episode_extractions(eid: int):
return get_extractions(eid)
class EpisodeUpdate(BaseModel):
title: str
@app.patch("/api/episodes/{eid}")
async def rename_episode(eid: int, data: EpisodeUpdate):
"""Rename an episode."""
ep = get_episode(eid)
if not ep:
raise HTTPException(404, "Episode not found")
update_episode(eid, title=data.title.strip())
return {"ok": True}
@app.post("/api/episodes/{eid}/retry")
async def retry_episode(eid: int, bg: BackgroundTasks):
"""Re-run the full processing pipeline for a failed episode."""
ep = get_episode(eid)
if not ep:
raise HTTPException(404, "Episode not found")
if not ep["status"].startswith("error"):
raise HTTPException(400, "Episode is not in an error state")
clear_extractions(eid)
update_episode(eid, status="pending", audio_path=None, transcript=None)
bg.add_task(process_episode, eid, ep["url"], ep["jlpt_level"])
return {"id": eid, "status": "processing"}
@app.delete("/api/episodes/{eid}")
async def remove_episode(eid: int):
ep = get_episode(eid)
if not ep:
raise HTTPException(404, "Episode not found")
delete_episode(eid)
return {"ok": True}
@app.delete("/api/extractions/{extraction_id}")
async def remove_extraction(extraction_id: int):
delete_extraction(extraction_id)
return {"ok": True}
@app.post("/api/episodes/{eid}/push-to-anki")
async def push_to_anki(eid: int, data: ExtractionIds):
"""Push selected extractions to Anki."""
connected = await anki.check_connection()
if not connected:
raise HTTPException(503, "Anki not running or AnkiConnect not installed. Open Anki first.")
ep = get_episode(eid)
extractions = get_extractions(eid)
selected = [e for e in extractions if e["id"] in data.extraction_ids]
if not selected:
raise HTTPException(400, "No extractions selected")
try:
note_ids = await anki.add_notes_batch(selected, episode_title=ep.get("title", ""))
except Exception as e:
raise HTTPException(502, f"Anki error: {e}")
pushed = 0
for ext, note_id in zip(selected, note_ids or []):
if note_id:
update_extraction_anki_id(ext["id"], note_id)
pushed += 1
return {"pushed": pushed, "duplicates": len(selected) - pushed}
@app.post("/api/episodes/{eid}/push-all-to-anki")
async def push_all_to_anki(eid: int):
"""Push all extractions for an episode to Anki."""
connected = await anki.check_connection()
if not connected:
raise HTTPException(503, "Anki not running or AnkiConnect not installed.")
ep = get_episode(eid)
extractions = get_extractions(eid)
unpushed = [e for e in extractions if not e.get("anki_note_id")]
if not unpushed:
return {"pushed": 0, "message": "All already in Anki"}
try:
note_ids = await anki.add_notes_batch(unpushed, episode_title=ep.get("title", ""))
except Exception as e:
raise HTTPException(502, f"Anki error: {e}")
pushed = 0
for ext, note_id in zip(unpushed, note_ids or []):
if note_id:
update_extraction_anki_id(ext["id"], note_id)
pushed += 1
return {"pushed": pushed, "duplicates": len(unpushed) - pushed}
@app.post("/api/episodes/{eid}/listen")
async def mark_listen(eid: int, data: ListenCreate = ListenCreate()):
listen_id = record_listen(eid, data.notes)
return {"listen_id": listen_id}
@app.delete("/api/episodes/{eid}/listens/{listen_id}")
async def remove_listen(eid: int, listen_id: int):
delete_listen(listen_id, eid)
return {"ok": True}
@app.get("/api/relistens/due")
async def due_relistens():
return get_due_relistens()
@app.get("/api/relistens/upcoming")
async def upcoming_relistens(days: int = 7):
return get_upcoming_relistens(days)
@app.post("/api/relistens/{schedule_id}/complete")
async def mark_relisten_complete(schedule_id: int):
complete_relisten(schedule_id)
return {"ok": True}
@app.get("/api/anki/status")
async def anki_status():
connected = await anki.check_connection()
return {"connected": connected}
# --- Serve frontend ---
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
async def index():
return FileResponse("static/index.html")