def get_video_for_quality(self, videos, ask=True, quality=None, clip=False): if ask is True: return self.select_video_for_quality(videos) else: video_quality = kodi.get_setting('video_quality') source = video_quality == '0' ask = video_quality == '1' bandwidth = video_quality == '2' try: bandwidth_value = int(kodi.get_setting('bandwidth')) except: bandwidth_value = None if quality or len(videos) == 1: for video in videos: if (quality and (quality.lower() in video['name'].lower())) or len(videos) == 1: return video elif ask: return self.select_video_for_quality(videos) elif source: for video in videos: if 'chunked' in video['id']: return video elif bandwidth and bandwidth_value and not clip: bandwidths = [] for video in videos: bwidth = int(video['bandwidth']) if bwidth <= bandwidth_value: bandwidths.append(bwidth) best_match = max(bandwidths) try: index = next(idx for idx, video in enumerate(videos) if int(video['bandwidth']) == best_match) return videos[index] except: pass return self.select_video_for_quality(videos)
def refresh_previews(): if kodi.get_setting('live_previews_enable') != 'true': return if kodi.get_setting('refresh_previews') == 'true': refresh_interval = int(kodi.get_setting('refresh_interval')) * 60 if get_refresh_diff() >= refresh_interval: set_refresh_stamp() TextureCacheCleaner().remove_like(LIVE_PREVIEW_TEMPLATE, notify_refresh())
def clear_previews(): if kodi.get_setting('live_previews_enable') == 'true': return run_plugin(i18n('clear_live_preview'), { 'mode': MODES.CLEARLIVEPREVIEWS, 'notify': utils.notify_refresh() }) return []
def truncate_title(self, title): truncate_setting = kodi.get_setting('title_truncate') == 'true' if truncate_setting: short_title = title[:self.line_length] ending = (title[self.line_length:] and TitleBuilder.Templates.ELLIPSIS) return short_title + ending return title
def format_title(self, title_values): title_setting = int(kodi.get_setting('title_display')) template = self.get_title_template(title_setting) for key, value in title_values.iteritems(): title_values[key] = self.clean_title_value(value) title = template.format(**title_values) return self.truncate_title(title)
def get_redirect_uri(): settings_id = kodi.get_setting('oauth_redirecturi') stripped_id = settings_id.strip() if settings_id != stripped_id: settings_id = stripped_id kodi.set_setting('oauth_redirecturi', settings_id) if settings_id: return settings_id.decode('utf-8') else: return REDIRECT_URI.decode('utf-8')
def get_client_id(default=False): settings_id = kodi.get_setting('oauth_clientid') stripped_id = settings_id.strip() if settings_id != stripped_id: settings_id = stripped_id kodi.set_setting('oauth_clientid', settings_id) if settings_id and not default: return settings_id.decode('utf-8') else: return b64decode(CLIENT_ID).decode('utf-8')
def use_inputstream_adaptive(): if kodi.get_setting('video_quality_ia') == 'true' or kodi.get_setting('video_quality') == '3': if kodi.get_setting('video_support_ia_builtin') == 'true': return True elif kodi.get_setting('video_support_ia_addon') == 'true': use_ia = kodi.get_setting('video_quality_ia') == 'true' or kodi.get_setting('video_quality') == '3' if not use_ia: return False ia_enabled = kodi.addon_enabled('inputstream.adaptive') if ia_enabled is False: if kodi.Dialog().yesno(kodi.get_name(), i18n('adaptive_is_disabled')): ia_enabled = kodi.set_addon_enabled('inputstream.adaptive') if ia_enabled: ia_min_version = '2.0.10' ia_version = kodi.Addon('inputstream.adaptive').getAddonInfo('version') ia_enabled = loose_version(ia_version) >= loose_version(ia_min_version) if not ia_enabled: result = kodi.Dialog().ok(kodi.get_name(), i18n('adaptive_version_check') % ia_min_version) if not ia_enabled: kodi.set_setting('video_quality_ia', 'false') kodi.set_setting('video_quality', '0') return False else: return True else: kodi.set_setting('video_quality_ia', 'false') kodi.set_setting('video_quality', '0') return False else: return False
def get_oauth_token(token_only=True, required=False): oauth_token = kodi.get_setting('oauth_token') if not oauth_token or not oauth_token.strip(): if not required: return '' kodi.notify(kodi.get_name(), i18n('token_required'), sound=False) kodi.show_settings() oauth_token = kodi.get_setting('oauth_token') stripped_token = oauth_token.strip() if oauth_token != stripped_token: oauth_token = stripped_token kodi.set_setting('oauth_token', oauth_token) if oauth_token: if token_only: idx = oauth_token.find(':') if idx >= 0: oauth_token = oauth_token[idx + 1:] else: if not oauth_token.lower().startswith('oauth:'): idx = oauth_token.find(':') if idx >= 0: oauth_token = oauth_token[idx + 1:] oauth_token = 'oauth:{0}'.format(oauth_token) return oauth_token.decode('utf-8')
def notify_refresh(): if kodi.get_setting('notify_refresh') == 'false': return False return True
def onPlayBackEnded(self): is_playing = self.window.getProperty(key=self.player_keys['twitch_playing']) == 'True' log_utils.log('Player: |onPlayBackEnded| isTwitch |{0}|'.format(is_playing), log_utils.LOGDEBUG) need_reset = True if is_playing: reconnect = kodi.get_setting('live_reconnect') == 'true' if reconnect: live_channel = self.window.getProperty(self.reconnect_keys['stream']) if live_channel: channel_id, name, display_name, quality = live_channel.split(',') retries = 0 max_retries = 5 while not monitor.abortRequested(): if monitor.waitForAbort(5): break break if monitor.abortRequested(): dialog = None else: dialog = kodi.ProgressDialog(kodi.get_name(), line1=utils.i18n('attempt_reconnect') % display_name, line2=utils.i18n('attempt_number') % (retries + 1), line3=utils.i18n('retry_seconds') % 60) if dialog: need_reset = False with dialog: while (not monitor.abortRequested()) and (retries < max_retries) and (not dialog.is_canceled()): wait_time = 0.0 abort = False while wait_time <= 120.0: if monitor.waitForAbort(0.5) or dialog.is_canceled(): abort = True break wait_time += 1.0 if (wait_time % 2) == 0: percent = int(((wait_time / 120) * 100)) dialog.update(percent=percent, line3=utils.i18n('retry_seconds') % ((120.0 - wait_time) / 2)) if abort: break retries += 1 try: try: cache.reset_cache() except: pass twitch = api.Twitch() videos = twitch.get_live(name) result = twitch.get_channel_stream(channel_id)[Keys.STREAM] item_dict = converter.stream_to_playitem(result) video = converter.get_video_for_quality(videos, ask=False, quality=quality) if video: log_utils.log('Attempting playback using quality |%s| @ |%s|' % (video['name'], video['url']), log_utils.LOGDEBUG) item_dict['path'] = video['url'] playback_item = kodi.create_item(item_dict, add=False) stream_name = result[Keys.CHANNEL][Keys.DISPLAY_NAME] \ if result[Keys.CHANNEL][Keys.DISPLAY_NAME] else result[Keys.CHANNEL][Keys.NAME] self.window.setProperty(self.reconnect_keys['stream'], '{0},{1},{2}'.format(channel_id, name, stream_name)) self.play(item_dict['path'], playback_item) if utils.irc_enabled() and twitch.access_token: username = twitch.get_username() if username: utils.exec_irc_script(username, name) break except: log_utils.log('Player: |Reconnection| Failed attempt |{0}|'.format(retries), log_utils.LOGERROR) dialog.update(0, line2=utils.i18n('attempt_number') % (retries + 1), line3=utils.i18n('retry_seconds') % 60) if dialog.is_canceled(): self.reset() if need_reset: self.reset()
def get_items_per_page(): return int(kodi.get_setting('items_per_page'))
def show_menu(menu, parent=None): setting_id = 'menu' if parent: setting_id += '_%s' % parent setting_id += '_%s' % menu return kodi.get_setting(setting_id) == 'true'
def irc_enabled(): return (kodi.get_setting('irc_enable') == 'true') and kodi.has_addon('script.ircchat')
def clear_previews(): if kodi.get_setting('live_previews_enable') == 'true': return run_plugin(i18n('clear_live_preview'), {'mode': MODES.CLEARLIVEPREVIEWS, 'notify': utils.notify_refresh()}) return []
def get_search_history_size(): return int(kodi.get_setting('search_history_size'))
def get_vodcast_color(): color = int(kodi.get_setting('vodcast_highlight')) color = COLORS.split('|')[color] return color.decode('utf-8')
def onPlayBackEnded(self): is_playing = self.window.getProperty( key=self.player_keys['twitch_playing']) == 'True' log_utils.log( 'Player: |onPlayBackEnded| isTwitch |{0}|'.format(is_playing), log_utils.LOGDEBUG) need_reset = True if is_playing: reconnect = kodi.get_setting('live_reconnect') == 'true' if reconnect: live_channel = self.window.getProperty( self.reconnect_keys['stream']) if live_channel: channel_id, name, display_name = live_channel.split(',') retries = 0 max_retries = 5 while not monitor.abortRequested(): if monitor.waitForAbort(5): break break if monitor.abortRequested(): dialog = None else: dialog = kodi.ProgressDialog( kodi.get_name(), line1=utils.i18n('attempt_reconnect') % display_name, line2=utils.i18n('attempt_number') % (retries + 1), line3=utils.i18n('retry_seconds') % 60) if dialog: need_reset = False with dialog: while (not monitor.abortRequested()) and ( retries < max_retries) and ( not dialog.is_canceled()): wait_time = 0.0 abort = False while wait_time <= 120.0: if monitor.waitForAbort( 0.5) or dialog.is_canceled(): abort = True break wait_time += 1.0 if (wait_time % 2) == 0: percent = int( ((wait_time / 120) * 100)) dialog.update( percent=percent, line3=utils.i18n('retry_seconds') % ((120.0 - wait_time) / 2)) if abort: break retries += 1 try: try: cache.reset_cache() except: pass twitch = api.Twitch() quality = utils.get_default_quality( 'stream', channel_id) if quality: quality = quality[channel_id][ 'quality'] videos = twitch.get_live(name) result = twitch.get_channel_stream( channel_id)[Keys.STREAM] item_dict = converter.stream_to_playitem( result) video = converter.get_video_for_quality( videos, ask=False, quality=quality) if video: log_utils.log( 'Attempting playback using quality |%s| @ |%s|' % (video['name'], video['url']), log_utils.LOGDEBUG) item_dict['path'] = video['url'] playback_item = kodi.create_item( item_dict, add=False) stream_name = result[Keys.CHANNEL][Keys.DISPLAY_NAME] \ if result[Keys.CHANNEL][Keys.DISPLAY_NAME] else result[Keys.CHANNEL][Keys.NAME] self.window.setProperty( self.reconnect_keys['stream'], '{0},{1},{2}'.format( channel_id, name, stream_name)) self.play(item_dict['path'], playback_item) if utils.irc_enabled( ) and twitch.access_token: username = twitch.get_username() if username: utils.exec_irc_script( username, name) break except: log_utils.log( 'Player: |Reconnection| Failed attempt |{0}|' .format(retries), log_utils.LOGERROR) dialog.update( 0, line2=utils.i18n('attempt_number') % (retries + 1), line3=utils.i18n('retry_seconds') % 60) if dialog.is_canceled(): self.reset() if need_reset: self.reset()
# -*- coding: utf-8 -*- """ Copyright (C) 2016 Twitch-on-Kodi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from common import cache, kodi cache_function = cache.cache_function cache_method = cache.cache_method reset_cache = cache.reset_cache limit = float(kodi.get_setting('cache_expire_time')) / 60 cache.cache_enabled = limit > 0