def _start_stream(self, channel, max_tries=2): resp = None for _ in range(max_tries): device_id = get_from_cache("device_id") oauth_tokens = get_from_cache("OAuthTokens") try: resp = self.session.post(BASE_URL + "/stream/start", headers={ "Content-Type": "application/json;charset=utf-8", "X-Yelo-DeviceId": device_id, "Authorization": authorization_payload(oauth_tokens["accessToken"]) }, data=stream_payload(device_id, channel)) if resp.status_code == 401: raise NotAuthorizedException("Unauthorized") break except NotAuthorizedException: self._refresh_oauth_token() if not resp: raise YeloException('Could not authenticate to play channel %s' % channel) response_data = resp.json() if response_data.get("errors"): title, message = YeloErrors.get_error_message(self.session, response_data["errors"][0]["code"]) KodiWrapper.dialog_ok(title, message) raise YeloException(message) return response_data
def cache_channel_epg(cls): if PluginCache.key_exists("entitlements"): _LOGGER.debug('Caching channels..') channels = YeloApi.get_channels() epg = YeloApi.get_epg(channels) EPG.to_cache(epg) KodiWrapper.set_setting('metadata_last_updated', str(int(time()))) sleep(1) KodiWrapper.container_refresh()
def refresh_epg(): KodiWrapper.dialog_ok( KodiWrapper.get_localized_string(40001), # Info KodiWrapper.get_localized_string(40003)) # This can take a while BackgroundService.cache_channel_epg() KodiWrapper.dialog_ok( KodiWrapper.get_localized_string(40001), # Info KodiWrapper.get_localized_string(40002)) # EPG has been renewed
def is_cached(self): import os from kodiwrapper import KodiWrapper path = KodiWrapper.get_addon_data_path() if not os.path.isfile(os.path.join(path, self.EPG_CACHE_FILE_NAME)): return False return True
def get_from_cache(key): import json import os from kodiwrapper import KodiWrapper path = KodiWrapper.get_addon_data_path() with open(os.path.join(path, CACHE_FILE_NAME), "r") as json_file: data = json.load(json_file) return data.get(key)
def run(self): _LOGGER.debug('Service started') while not self.abortRequested(): # Update every `update_interval` after the last update if EPG().is_enabled and int(KodiWrapper.get_setting('metadata_last_updated', 0)) \ + self.update_interval < time(): self.cache_channel_epg() # Stop when abort requested if self.waitForAbort(10): break _LOGGER.debug('Service stopped')
def is_in_cache(key): import json import os from kodiwrapper import KodiWrapper path = KodiWrapper.get_addon_data_path() if not os.path.isfile(os.path.join(path, CACHE_FILE_NAME)): return False with open(os.path.join(path, CACHE_FILE_NAME), "r") as json_file: data = json.load(json_file) return key in data
def list_channels(self, is_folder=False): from kodiwrapper import KodiWrapper import datetime import dateutil.parser from dateutil.tz import UTC listing = [] tv_channels = self.get_channels() epg = self.get_epg(tv_channels, full=False) for tv_channel in tv_channels: name = tv_channel.get('channelIdentification').get('name') square_logo = tv_channel.get('channelProperties').get('squareLogo') channel_id = tv_channel.get('channelIdentification').get( 'stbUniqueName') poster = "" guide = "" for index, item in enumerate(epg[name]): now = datetime.datetime.utcnow().replace(second=0, microsecond=0, tzinfo=UTC) end = dateutil.parser.parse(item.get('stop')) if end <= now: continue start = dateutil.parser.parse(item.get('start')) if start >= now: continue try: prev_title = epg.get(name, [])[index - 1].get('title', '') except IndexError: prev_title = '' try: next_title = epg.get(name, [])[index + 1].get('title', '') except IndexError: next_title = '' title = item.get('title', '') guide = self._create_guide_from_channel_info( prev_title, title, next_title) poster = item.get('image', '') break list_item = KodiWrapper.create_list_item(name, square_logo, poster, {"plot": guide}, True, True) url = KodiWrapper.url_for('play_id', channel_id=channel_id) listing.append((url, list_item, is_folder)) KodiWrapper.add_dir_items(listing) KodiWrapper.end_directory()
def get_from_cache(cls): import json import os from kodiwrapper import KodiWrapper path = KodiWrapper.get_addon_data_path() try: with open(os.path.join(path, cls.EPG_CACHE_FILE_NAME), "r") as json_file: data = json.load(json_file) return data except IOError: return {}
def get_channels_iptv(self): allowed_tv_channels = self.get_channels() channels = [] for channel in allowed_tv_channels: name = channel.get('channelIdentification', {}).get('name') epg_id = channel.get('channelIdentification', {}).get('name') channel_id = channel.get('channelIdentification', {}).get('stbUniqueName') logo = channel.get('channelProperties', {}).get('squareLogo') channels.append(dict( name=name, id=epg_id, logo=logo, stream=KodiWrapper.url_for('play_id', channel_id=channel_id), )) return channels
def cache_to_file(json_data): import json import os from kodiwrapper import KodiWrapper path = KodiWrapper.get_addon_data_path() if not os.path.exists(path): os.mkdir(path, 0o775) data = {} if os.path.isfile(os.path.join(path, CACHE_FILE_NAME)): with open(os.path.join(path, CACHE_FILE_NAME), "r") as json_file: data = json.load(json_file) data.update(json_data) with open(os.path.join(path, CACHE_FILE_NAME), "w") as json_file: json.dump(data, json_file)
def list_channels_without_epg(self, is_folder=False): from kodiwrapper import KodiWrapper listing = [] tv_channels = self.get_channels() for tv_channel in tv_channels: name = tv_channel.get('channelIdentification').get('name') square_logo = tv_channel.get('channelProperties').get('squareLogo') channel_id = tv_channel.get('channelIdentification').get('stbUniqueName') list_item = KodiWrapper.create_list_item(name, square_logo, "", {"plot": ""}, True, True) url = KodiWrapper.url_for('play_id', channel_id=channel_id) listing.append((url, list_item, is_folder)) KodiWrapper.add_dir_items(listing) KodiWrapper.end_directory()
def _display_error_message(response_data): title, message = YeloErrors.get_error_message( response_data["errors"][0]["code"]) KodiWrapper.dialog_ok(title, message)
def _login(self): creds = Credentials() if not creds.are_filled_in(): KodiWrapper.dialog_ok(KodiWrapper.get_localized_string(32014), KodiWrapper.get_localized_string(32015)) KodiWrapper.open_settings() creds.reload() resp = self.session.post( "https://login.prd.telenet.be/openid/login.do", data=login_payload(creds.username, creds.password)) last_response = resp.history[-1] try: self.auth_tries += 1 if "Location" in last_response.headers: token = self.extract_auth_token( last_response.headers.get('Location')) if not token: raise NotAuthorizedException() PluginCache.set_data({"auth_token": token}) return True except NotAuthorizedException: KodiWrapper.dialog_ok(KodiWrapper.get_localized_string(32006), KodiWrapper.get_localized_string(32007)) if self.auth_tries < 2: KodiWrapper.open_settings() self._execute_required_steps() return False
def refresh_epg(): KodiWrapper.dialog_ok(KodiWrapper.get_localized_string(40001), KodiWrapper.get_localized_string(40003)) BackgroundService.cache_channel_epg() KodiWrapper.dialog_ok(KodiWrapper.get_localized_string(40001), KodiWrapper.get_localized_string(40002))