From fff16c3d3b30dcf5e321c2320a38add638172101 Mon Sep 17 00:00:00 2001 From: joe di castro Date: Tue, 19 Mar 2013 21:36:36 +0100 Subject: [PATCH] Support decimal part in seconds in the subtitles --- src/TEDSubs.py | 11 ++++++----- src/TEDTalks.py | 9 +++++---- 2 files changed, 11 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/TEDSubs.py diff --git a/src/TEDSubs.py b/src/TEDSubs.py old mode 100644 new mode 100755 index e06267b..f1b9969 --- a/src/TEDSubs.py +++ b/src/TEDSubs.py @@ -95,8 +95,9 @@ def get_sub(tt_id, tt_intro, sub): def srt_time(tst): """Format Time from TED Subtitles format to SRT time Format.""" secs, mins, hours = ((tst / 1000) % 60), (tst / 60000), (tst / 3600000) - right_srt_time = "{0:02d}:{1:02d}:{2:02d},000".format(hours, mins, - secs) + right_srt_time = ("{0:02d}:{1:02d}:{2:02d},{3:3.0f}". + format(int(hours), int(mins), int(secs), + divmod(secs, 1)[1] * 1000)) return right_srt_time srt_content = '' @@ -178,7 +179,7 @@ def main(): (opts, args) = options().parse_args() # regex expressions to search into the webpage - regex_intro = re.compile('introDuration%22%3A(\d+)%2C') + regex_intro = re.compile('introDuration%22%3A(\d+\.?\d+)%2C') regex_id = re.compile('talkId%22%3A(\d+)%2C') regex_url = re.compile('id="no-flash-video-download" href="(.+)"') regex_vid = re.compile('http://.+\/(.*\.mp4)') @@ -199,8 +200,8 @@ def main(): tedtalk_webpage).read() if ttalk_webpage: try: - ttalk_intro = ((int(regex_intro.findall(ttalk_webpage)[0]) + 1) - * 1000) + ttalk_intro = ((float(regex_intro.findall(ttalk_webpage)[0]) + + 1) * 1000) ttalk_id = int(regex_id.findall(ttalk_webpage)[0]) ttalk_url = regex_url.findall(ttalk_webpage)[0] ttalk_url = ttalk_url.replace('.mp4', '-480p.mp4') diff --git a/src/TEDTalks.py b/src/TEDTalks.py index 9de120f..8e3626d 100755 --- a/src/TEDTalks.py +++ b/src/TEDTalks.py @@ -325,8 +325,9 @@ def get_sub(tt_id, tt_intro, sub): def srt_time(tst): """Format Time from TED Subtitles format to SRT time Format.""" secs, mins, hours = ((tst / 1000) % 60), (tst / 60000), (tst / 3600000) - right_srt_time = "{0:02d}:{1:02d}:{2:02d},000".format(hours, mins, - secs) + right_srt_time = ("{0:02d}:{1:02d}:{2:02d},{3:3.0f}". + format(int(hours), int(mins), int(secs), + divmod(secs, 1)[1] * 1000)) return right_srt_time srt_content = '' @@ -397,8 +398,8 @@ def check_subs(ttalk, v_name): stdout=PIPE).stdout.read() else: tt_webpage = urllib2.urlopen(ttalk.feedburner_origlink).read() - regex = re.compile('introDuration%22%3A(\d+)%2C') - tt_intro = (int(regex.findall(tt_webpage)[0]) + 1) * 1000 + regex = re.compile('introDuration%22%3A(\d+\.?\d+)%2C') + tt_intro = (float(regex.findall(tt_webpage)[0]) + 1) * 1000 subtitle, get_log = get_sub(ttalk.id.split(':')[-1], tt_intro, sub) s_log += get_log if subtitle: