def _delay_subtitles(self, subtitles, json_manifest): """ Modify the subtitles timings to account for ad breaks. :type subtitles: list[string] :type json_manifest: dict :rtype list[str] """ # Clean up old subtitles temp_dir = os.path.join(kodiutils.addon_profile(), 'temp', '') _, files = kodiutils.listdir(temp_dir) if files: for item in files: if kodiutils.to_unicode(item).endswith('.vtt'): kodiutils.delete(temp_dir + kodiutils.to_unicode(item)) # Return if there are no subtitles available if not subtitles: return None import re if not kodiutils.exists(temp_dir): kodiutils.mkdirs(temp_dir) ad_breaks = list() delayed_subtitles = list() webvtt_timing_regex = re.compile( r'\n(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\s') # Get advertising breaks info from json manifest cues = json_manifest.get('interstitials').get('cues') for cue in cues: ad_breaks.append( dict(start=cue.get('start'), duration=cue.get('break_duration'))) for subtitle in subtitles: output_file = temp_dir + subtitle.get('name') + '.' + subtitle.get( 'url').split('.')[-1] webvtt_content = util.http_get(subtitle.get('url')).text webvtt_content = webvtt_timing_regex.sub( lambda match: self._delay_webvtt_timing(match, ad_breaks), webvtt_content) with kodiutils.open_file(output_file, 'w') as webvtt_output: webvtt_output.write(kodiutils.from_unicode(webvtt_content)) delayed_subtitles.append(output_file) return delayed_subtitles
def _download_subtitles(subtitles): # Clean up old subtitles temp_dir = os.path.join(kodiutils.addon_profile(), 'temp', '') _, files = kodiutils.listdir(temp_dir) if files: for item in files: kodiutils.delete(temp_dir + kodiutils.to_unicode(item)) # Return if there are no subtitles available if not subtitles: return None if not kodiutils.exists(temp_dir): kodiutils.mkdirs(temp_dir) downloaded_subtitles = list() for subtitle in subtitles: output_file = temp_dir + subtitle.get('name') webvtt_content = util.http_get(subtitle.get('url')).text with kodiutils.open_file(output_file, 'w') as webvtt_output: webvtt_output.write(kodiutils.from_unicode(webvtt_content)) downloaded_subtitles.append(output_file) return downloaded_subtitles