Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ RAPIDAPI_HOST = "youtube-transcriptor.p.rapidapi.com"
|
|
10 |
|
11 |
# μ νλΈ URLμμ λΉλμ€ IDλ₯Ό μΆμΆνλ ν¨μ
|
12 |
def get_video_id(youtube_url):
|
13 |
-
# μ νλΈ URL
|
14 |
video_id_match = re.search(r"(?<=v=)[^#&?]*", youtube_url) or re.search(r"(?<=youtu.be/)[^#&?]*", youtube_url)
|
15 |
return video_id_match.group(0) if video_id_match else None
|
16 |
|
@@ -22,7 +22,6 @@ def get_youtube_transcript(youtube_url):
|
|
22 |
# λΉλμ€ ID μΆμΆ
|
23 |
video_id = get_video_id(youtube_url)
|
24 |
if video_id is None:
|
25 |
-
print("ERROR: μλͺ»λ μ νλΈ URLμ
λλ€. λΉλμ€ IDλ₯Ό μ°Ύμ μ μμ΅λλ€.")
|
26 |
return {"error": "μλͺ»λ μ νλΈ URLμ
λλ€. λΉλμ€ IDλ₯Ό μ°Ύμ μ μμ΅λλ€."}
|
27 |
|
28 |
url = "https://youtube-transcriptor.p.rapidapi.com/transcript"
|
@@ -35,39 +34,20 @@ def get_youtube_transcript(youtube_url):
|
|
35 |
# μΈμ΄ μ°μ μμμ λ°λΌ μμ°¨μ μΌλ‘ μμ²μ μλ
|
36 |
for lang in LANGUAGE_PRIORITY:
|
37 |
querystring = {"video_id": video_id, "lang": lang}
|
38 |
-
print(f"μμ² μ€: {lang} μΈμ΄μ λν μλ§ μμ²")
|
39 |
-
|
40 |
response = requests.get(url, headers=headers, params=querystring)
|
41 |
|
42 |
-
# μν μ½λ
|
43 |
-
print(f"Response status code: {response.status_code}")
|
44 |
-
|
45 |
-
# μμ μλ΅ λ°μ΄ν°λ₯Ό μΆλ ₯
|
46 |
-
print("Raw response content:", response.text)
|
47 |
-
|
48 |
-
# μν μ½λκ° 200μΌ λλ§ μ²λ¦¬
|
49 |
if response.status_code == 200:
|
50 |
try:
|
51 |
-
# JSONμΌλ‘ μλ΅μ νμ±
|
52 |
data = response.json()
|
53 |
-
|
54 |
-
# μλ΅ λ°μ΄ν° μΆλ ₯
|
55 |
-
print("Parsed JSON response:", json.dumps(data, indent=2, ensure_ascii=False))
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
# μλ§ λ°μ΄ν°λ₯Ό JSON 리μ€νΈλ‘ λ°ν
|
60 |
-
return {"language": lang, "transcription": data["transcription"]}
|
61 |
-
else:
|
62 |
-
print(f"ERROR: μλ§ λ°μ΄ν°λ₯Ό {lang} μΈμ΄λ‘ μ°Ύμ μ μμ΅λλ€.")
|
63 |
-
return {"error": f"{lang} μΈμ΄λ‘ μλ§ λ°μ΄ν°λ₯Ό μ°Ύμ μ μμ΅λλ€."}
|
64 |
|
65 |
except json.JSONDecodeError as e:
|
66 |
-
print(f"ERROR: JSON λμ½λ© μ€λ₯ λ°μ: {str(e)}")
|
67 |
return {"error": f"JSON λμ½λ© μ€λ₯ λ°μ: {str(e)}"}
|
68 |
|
69 |
-
# μλ§μ μ°Ύμ§ λͺ»ν κ²½μ°
|
70 |
-
print("ERROR: μ°μ μμ μΈμ΄λ‘ μλ§μ μ°Ύμ μ μμ΅λλ€.")
|
71 |
return {"error": "μ°μ μμ μΈμ΄λ‘ μλ§μ μ°Ύμ μ μμ΅λλ€."}
|
72 |
|
73 |
# Gradio μΈν°νμ΄μ€ μ μ
|
|
|
10 |
|
11 |
# μ νλΈ URLμμ λΉλμ€ IDλ₯Ό μΆμΆνλ ν¨μ
|
12 |
def get_video_id(youtube_url):
|
13 |
+
# μ νλΈ URL λλ youtu.be λ¨μΆ URLμμ video_id μΆμΆ
|
14 |
video_id_match = re.search(r"(?<=v=)[^#&?]*", youtube_url) or re.search(r"(?<=youtu.be/)[^#&?]*", youtube_url)
|
15 |
return video_id_match.group(0) if video_id_match else None
|
16 |
|
|
|
22 |
# λΉλμ€ ID μΆμΆ
|
23 |
video_id = get_video_id(youtube_url)
|
24 |
if video_id is None:
|
|
|
25 |
return {"error": "μλͺ»λ μ νλΈ URLμ
λλ€. λΉλμ€ IDλ₯Ό μ°Ύμ μ μμ΅λλ€."}
|
26 |
|
27 |
url = "https://youtube-transcriptor.p.rapidapi.com/transcript"
|
|
|
34 |
# μΈμ΄ μ°μ μμμ λ°λΌ μμ°¨μ μΌλ‘ μμ²μ μλ
|
35 |
for lang in LANGUAGE_PRIORITY:
|
36 |
querystring = {"video_id": video_id, "lang": lang}
|
|
|
|
|
37 |
response = requests.get(url, headers=headers, params=querystring)
|
38 |
|
39 |
+
# μν μ½λ νμΈ λ° μ 체 μλ΅ λ°ν
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if response.status_code == 200:
|
41 |
try:
|
|
|
42 |
data = response.json()
|
|
|
|
|
|
|
43 |
|
44 |
+
# μ 체 μλ΅ λ°μ΄ν°λ₯Ό κ·Έλλ‘ λ°ν
|
45 |
+
return {"language": lang, "data": data}
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
except json.JSONDecodeError as e:
|
|
|
48 |
return {"error": f"JSON λμ½λ© μ€λ₯ λ°μ: {str(e)}"}
|
49 |
|
50 |
+
# λͺ¨λ μΈμ΄μμ μλ§μ μ°Ύμ§ λͺ»ν κ²½μ°
|
|
|
51 |
return {"error": "μ°μ μμ μΈμ΄λ‘ μλ§μ μ°Ύμ μ μμ΅λλ€."}
|
52 |
|
53 |
# Gradio μΈν°νμ΄μ€ μ μ
|