lanbogao commited on
Commit
de8dee7
1 Parent(s): 4797dae

Remove unused and check subtitle_url.

Browse files
Files changed (1) hide show
  1. fetchYoutubeSubtitle.py +6 -32
fetchYoutubeSubtitle.py CHANGED
@@ -24,12 +24,14 @@ async def fetchSubtitle(url: str, lang: Optional[str] = 'en', vttType="vtt") ->
24
  if info_dict.get("subtitles"):
25
  # get first available subtitle
26
  subtitle_url = getVttUrlFromSubtitles(info_dict.get("subtitles"), lang, vttType)
27
- with ydl.urlopen(subtitle_url) as subtitle:
28
- return subtitle.read().decode()
 
29
  if info_dict.get("automatic_captions"):
30
  subtitle_url = getVttUrlFromSubtitles(info_dict.get("automatic_captions"), lang, vttType)
31
- with ydl.urlopen(subtitle_url) as subtitle:
32
- return subtitle.read().decode()
 
33
  return None
34
 
35
  async def fetchSubtitleUrls(url: str) -> json:
@@ -48,31 +50,3 @@ async def fetchSubtitleUrls(url: str) -> json:
48
  if info_dict.get("automatic_captions"):
49
  return info_dict.get("automatic_captions")
50
  return None
51
-
52
- def get_subtitle(url, lang='en'):
53
- if lang is None:
54
- lang = 'en'
55
- # Download subtitles if available
56
- ydl_opts = {
57
- 'writesubtitles': True,
58
- 'outtmpl': '%(id)s.%(ext)s',
59
- 'subtitleslangs': [lang],
60
- 'skip_download': True,
61
- }
62
- try:
63
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
64
- info_dict = ydl.extract_info(url, download=True)
65
- video_id = info_dict.get("id", None)
66
- if video_id is None:
67
- return None
68
-
69
- subtitle_file = f"{video_id}.{lang}.vtt"
70
- with open(subtitle_file, 'r') as f:
71
- subtitle_content = f.read()
72
- subtitle_content = re.sub(r"<[^>]+>", "", subtitle_content)
73
- return subtitle_content
74
- except error:
75
- print(error)
76
- return None
77
-
78
- return None
 
24
  if info_dict.get("subtitles"):
25
  # get first available subtitle
26
  subtitle_url = getVttUrlFromSubtitles(info_dict.get("subtitles"), lang, vttType)
27
+ if subtitle_url:
28
+ with ydl.urlopen(subtitle_url) as subtitle:
29
+ return subtitle.read().decode()
30
  if info_dict.get("automatic_captions"):
31
  subtitle_url = getVttUrlFromSubtitles(info_dict.get("automatic_captions"), lang, vttType)
32
+ if subtitle_url:
33
+ with ydl.urlopen(subtitle_url) as subtitle:
34
+ return subtitle.read().decode()
35
  return None
36
 
37
  async def fetchSubtitleUrls(url: str) -> json:
 
50
  if info_dict.get("automatic_captions"):
51
  return info_dict.get("automatic_captions")
52
  return None