1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-12-23 01:08:10 +00:00

[ceskatelevize] Simplify

This commit is contained in:
Sergey M․ 2015-01-07 05:03:14 +06:00
parent ecd1936695
commit 27a82a1b93

View file

@ -12,7 +12,6 @@ from ..compat import (
from ..utils import ( from ..utils import (
ExtractorError, ExtractorError,
float_or_none, float_or_none,
HEADRequest,
) )
@ -105,14 +104,16 @@ class CeskaTelevizeIE(SubtitlesInfoExtractor):
duration = float_or_none(item.get('duration')) duration = float_or_none(item.get('duration'))
thumbnail = item.get('previewImageUrl') thumbnail = item.get('previewImageUrl')
# subtitles subtitles = {}
subtitles = self.extract_subtitles(video_id, webpage) subs = item.get('subtitles')
if subs:
subtitles['cs'] = subs[0]['url']
if self._downloader.params.get('listsubtitles', False): if self._downloader.params.get('listsubtitles', False):
self._list_available_subtitles(video_id, webpage) self._list_available_subtitles(video_id, subtitles)
return return
subtitles = self._fix_subtitles(self.extract_subtitles(video_id, webpage)) subtitles = self._fix_subtitles(self.extract_subtitles(video_id, subtitles))
return { return {
'id': episode_id, 'id': episode_id,
@ -124,13 +125,14 @@ class CeskaTelevizeIE(SubtitlesInfoExtractor):
'subtitles': subtitles, 'subtitles': subtitles,
} }
def _fix_subtitles(self, subtitles): @staticmethod
""" Convert milisecond-based subtitles to SRT """ def _fix_subtitles(subtitles):
""" Convert millisecond-based subtitles to SRT """
if subtitles is None: if subtitles is None:
return subtitles # subtitles not requested return subtitles # subtitles not requested
def _msectotimecode(msec): def _msectotimecode(msec):
""" Helper utility to convert miliseconds to timecode """ """ Helper utility to convert milliseconds to timecode """
components = [] components = []
for divider in [1000, 60, 60, 100]: for divider in [1000, 60, 60, 100]:
components.append(msec % divider) components.append(msec % divider)
@ -139,7 +141,7 @@ class CeskaTelevizeIE(SubtitlesInfoExtractor):
def _fix_subtitle(subtitle): def _fix_subtitle(subtitle):
for line in subtitle.splitlines(): for line in subtitle.splitlines():
m = re.match(r"^ *([0-9]+); *([0-9]+) +([0-9]+) *$", line) m = re.match(r"^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$", line)
if m: if m:
yield m.group(1) yield m.group(1)
start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:]) start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:])
@ -151,15 +153,3 @@ class CeskaTelevizeIE(SubtitlesInfoExtractor):
for k, v in subtitles.items(): for k, v in subtitles.items():
fixed_subtitles[k] = "\r\n".join(_fix_subtitle(v)) fixed_subtitles[k] = "\r\n".join(_fix_subtitle(v))
return fixed_subtitles return fixed_subtitles
def _get_available_subtitles(self, video_id, webpage):
video_id = video_id.partition('-')[0]
url = 'http://imgct.ceskatelevize.cz/cache/data/ivysilani/' \
'subtitles/{}/{}/sub.txt'.format(video_id[:3], video_id)
req = HEADRequest(url)
sub = self._request_webpage(
req, video_id,
note="Checking subtitles",
errnote="No subtitles found",
fatal=False)
return {'cs': url} if sub else {}