def get_token(self): """ Return a JWT that can be used to authenticate the user. :rtype str """ # Don't return a token when we have no password or username. if not self._kodi.get_setting( 'username') or not self._kodi.get_setting('password'): self._kodi.log('Skipping since we have no username or password', LOG_INFO) return None # Return if we already have the token in memory. if self._token: self._kodi.log('Returning token from memory', LOG_DEBUG) return self._token # Try to load from cache path = self._kodi.get_userdata_path() + 'token.json' if self._kodi.check_if_path_exists(path): self._kodi.log('Returning token from cache', LOG_DEBUG) with self._kodi.open_file(path) as f: self._token = f.read() if self._token: return self._token # Authenticate with VTM GO and store the token self._token = self._login() self._kodi.log('Returning token from VTM GO', LOG_DEBUG) with self._kodi.open_file(path, 'w') as f: f.write(from_unicode(self._token)) return self._token
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] """ import re temp_dir = self._kodi.get_userdata_path() + 'temp/' if not self._kodi.check_if_path_exists(temp_dir): self._kodi.mkdir(temp_dir) else: dirs, files = self._kodi.listdir(temp_dir) # pylint: disable=unused-variable if files: for item in files: if item.endswith('.vtt'): self._kodi.delete_file(temp_dir + item) 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.split('/')[-1].split('.')[0] + '.nl-NL.' + subtitle.split('.')[-1] webvtt_content = requests.get(subtitle).text webvtt_content = webvtt_timing_regex.sub(lambda match: self._delay_webvtt_timing(match, ad_breaks), webvtt_content) with self._kodi.open_file(output_file, 'w') as webvtt_output: webvtt_output.write(from_unicode(webvtt_content)) delayed_subtitles.append(output_file) return delayed_subtitles
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(self._kodi.get_userdata_path(), 'temp', '') _, files = self._kodi.listdir(temp_dir) if files: for item in files: if item.endswith('.vtt'): self._kodi.delete_file(temp_dir + item) # Return if there are no subtitles available if not subtitles: return None import re if not self._kodi.check_if_path_exists(temp_dir): self._kodi.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 = requests.get(subtitle.get('url')).text webvtt_content = webvtt_timing_regex.sub( lambda match: self._delay_webvtt_timing(match, ad_breaks), webvtt_content) with self._kodi.open_file(output_file, 'w') as webvtt_output: webvtt_output.write(from_unicode(webvtt_content)) delayed_subtitles.append(output_file) return delayed_subtitles
def get_token(self): """ Return a JWT that can be used to authenticate the user. :rtype str """ userdata_path = self._kodi.get_userdata_path() # Don't return a token when we have no password or username. if not self._kodi.get_setting('username') or not self._kodi.get_setting('password'): _LOGGER.debug('Skipping since we have no username or password') return None # Return if we already have the token in memory. if self._token: _LOGGER.debug('Returning token from memory') return self._token # Try to load from cache path = os.path.join(userdata_path, 'token.json') if self._kodi.check_if_path_exists(path): _LOGGER.debug('Returning token from cache') with self._kodi.open_file(path) as fdesc: self._token = fdesc.read() if self._token: return self._token # Authenticate with VTM GO and store the token self._token = self._login() _LOGGER.debug('Returning token from VTM GO') # Make sure the path exists if not self._kodi.check_if_path_exists(userdata_path): self._kodi.mkdirs(userdata_path) with self._kodi.open_file(path, 'w') as fdesc: fdesc.write(from_unicode(self._token)) return self._token