lanbogao commited on
Commit
d8804e5
1 Parent(s): 860b5e0

Add proxy & update fetchSubtitleUrls.

Browse files
Files changed (2) hide show
  1. fetchYoutubeSubtitle.py +13 -11
  2. main.py +4 -4
fetchYoutubeSubtitle.py CHANGED
@@ -36,10 +36,10 @@ def getUrlFromSubtitles(item, lang='en', subType="vtt"):
36
  return subtitle.get("url")
37
  return None
38
 
39
- async def fetchSubtitle(url: str, lang: Optional[str] = 'en', subType: Optional[str] = "vtt") -> dict:
40
- return await fetchSubtitlebyType(url, lang, subType)
41
 
42
- async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType="vtt") -> dict:
43
  ydl_opts = {
44
  "noplaylist": True,
45
  "writesubtitles": False,
@@ -49,6 +49,9 @@ async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType="vtt
49
  "socket_timeout": 20
50
  }
51
 
 
 
 
52
  title = "unknow"
53
  duration = ""
54
  try:
@@ -113,13 +116,16 @@ def xml_caption_to_srt( xml_captions: str) -> str:
113
  segments.append(line)
114
  return "\n".join(segments).strip()
115
 
116
- async def fetchSubtitleUrls(url: str) -> json:
117
  ydl_opts = {
118
  "noplaylist": True,
119
  "writesubtitles": False,
120
  "allsubtitles": True,
121
  "skip_download": True,
122
  }
 
 
 
123
  title = "unknow"
124
  duration = ""
125
  try:
@@ -128,12 +134,8 @@ async def fetchSubtitleUrls(url: str) -> json:
128
  title = info_dict.get("title", "unknow")
129
  seconds = info_dict.get("duration")
130
  duration = str(seconds) if seconds else ""
131
- if info_dict.get("subtitles"):
132
- langs = info_dict.get("subtitles").keys()
133
- if not (len(langs) == 1 and "live_chat" in langs):
134
- return {"title": title, "duration": duration, "subtitles": info_dict.get("subtitles")}
135
- if info_dict.get("automatic_captions"):
136
- return {"title": title, "duration": duration, "subtitles": info_dict.get("automatic_captions")}
137
  except Exception as e:
138
  return {"error": str(e)}
139
- return {"title": title, "duration": duration, "error": "No subtitles"}
 
36
  return subtitle.get("url")
37
  return None
38
 
39
+ async def fetchSubtitle(url: str, lang: Optional[str] = 'en', subType: Optional[str] = "vtt", proxy: Optional[str] = None) -> dict:
40
+ return await fetchSubtitlebyType(url, lang, subType, proxy)
41
 
42
+ async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType: Optional[str] = "vtt", proxy: Optional[str] = None) -> dict:
43
  ydl_opts = {
44
  "noplaylist": True,
45
  "writesubtitles": False,
 
49
  "socket_timeout": 20
50
  }
51
 
52
+ if proxy:
53
+ ydl_opts.update({"proxy": proxy})
54
+
55
  title = "unknow"
56
  duration = ""
57
  try:
 
116
  segments.append(line)
117
  return "\n".join(segments).strip()
118
 
119
+ async def fetchSubtitleUrls(url: str, proxy: Optional[str] = None) -> json:
120
  ydl_opts = {
121
  "noplaylist": True,
122
  "writesubtitles": False,
123
  "allsubtitles": True,
124
  "skip_download": True,
125
  }
126
+ if proxy:
127
+ ydl_opts.update({"proxy": proxy})
128
+
129
  title = "unknow"
130
  duration = ""
131
  try:
 
134
  title = info_dict.get("title", "unknow")
135
  seconds = info_dict.get("duration")
136
  duration = str(seconds) if seconds else ""
137
+
138
+ return {"title": title, "duration": duration, "subtitles": info_dict.get("subtitles"),"automatic_captions": info_dict.get("automatic_captions")}
139
+
 
 
 
140
  except Exception as e:
141
  return {"error": str(e)}
 
main.py CHANGED
@@ -21,16 +21,16 @@ def read_json():
21
 
22
 
23
  @app.get("/subtitle/")
24
- async def get_subtitle(url: str, subtype: str="srt", x_token: Annotated[str | None, Header()] = None):
25
  if token != x_token:
26
  return JSONResponse({"error": "Invalid token"})
27
- subtitle = await fetchSubtitle(url,subType=subtype)
28
  return JSONResponse(content=subtitle)
29
 
30
 
31
  @app.get("/subtitle-urls/")
32
- async def get_subtitleUrls(url: str, x_token: Annotated[str | None, Header()] = None):
33
  if token != x_token:
34
  return JSONResponse({"error": "Invalid token"})
35
- subtitles = await fetchSubtitleUrls(url)
36
  return JSONResponse(content=subtitles)
 
21
 
22
 
23
  @app.get("/subtitle/")
24
+ async def get_subtitle(url: str, subtype: str="srt", lang:str='en', proxy: str=None, x_token: Annotated[str | None, Header()] = None):
25
  if token != x_token:
26
  return JSONResponse({"error": "Invalid token"})
27
+ subtitle = await fetchSubtitle(url, lang=lang, subType=subtype, proxy=proxy)
28
  return JSONResponse(content=subtitle)
29
 
30
 
31
  @app.get("/subtitle-urls/")
32
+ async def get_subtitleUrls(url: str, proxy: str=None, x_token: Annotated[str | None, Header()] = None):
33
  if token != x_token:
34
  return JSONResponse({"error": "Invalid token"})
35
+ subtitles = await fetchSubtitleUrls(url, proxy=proxy)
36
  return JSONResponse(content=subtitles)