def __pick_source(sources): log_utils.log('Sources found: {0}'.format(sources)) if len(sources) == 1: return sources[0] elif len(sources) > 1: if kodi.get_kodi_version().major > 16: listitem_sources = [] for source in sources: title = source['label'] if source['label'] else kodi.i18n('unknown') label2 = '%s [[COLOR=lightgray]%s[/COLOR]]' % (source['content_type'].capitalize(), source['resolver'] if source['resolver'] else 'Kodi') icon = '' if source['content_type'] == 'image': icon = source['url'] elif not source['resolver']: icon = ICONS.KODI elif source['resolver'] == 'youtube-dl': icon = ICONS.YOUTUBEDL elif source['resolver'] == 'URLResolver': icon = ICONS.URLRESOLVER l_item = kodi.ListItem(label=title, label2=label2) l_item.setArt({'icon': icon, 'thumb': icon}) listitem_sources.append(l_item) result = kodi.Dialog().select(kodi.i18n('choose_source'), list=listitem_sources, useDetails=True) else: result = kodi.Dialog().select(kodi.i18n('choose_source'), ['[%s] %s' % (source['content_type'].capitalize(), source['label']) if source['label'] else '[%s] %s' % (source['content_type'].capitalize(), kodi.i18n('unknown')) for source in sources]) if result == -1: return None else: return sources[result] else: return None
def get_next_rewatch_method(trakt_id): rewatch_method = get_rewatch_method(trakt_id) if rewatch_method == REWATCH_METHODS.LAST_WATCHED: return i18n('least_watched_method'), REWATCH_METHODS.LEAST_WATCHED elif rewatch_method == REWATCH_METHODS.LEAST_WATCHED: return i18n('most_watched_method'), REWATCH_METHODS.MOST_WATCHED else: return i18n('last_watched_method'), REWATCH_METHODS.LAST_WATCHED
def download_media(url, path, file_name): try: progress = int(kodi.get_setting('down_progress')) active = not progress == PROGRESS.OFF background = progress == PROGRESS.BACKGROUND with kodi.ProgressDialog('Premiumize Cloud', i18n('downloading') % (file_name), background=background, active=active) as pd: request = urllib2.Request(url) request.add_header('User-Agent', USER_AGENT) request.add_unredirected_header('Host', request.get_host()) response = urllib2.urlopen(request) content_length = 0 if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) file_name = file_name.replace('.strm', get_extension(url, response)) full_path = os.path.join(path, file_name) log_utils.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) path = xbmc.makeLegalFilename(path) try: try: xbmcvfs.mkdirs(path) except: os.makedirs(path) except Exception as e: log_utils.log('Dir Create Failed: %s' % (e), log_utils.LOGDEBUG) if not xbmcvfs.exists(path): raise Exception(i18n('failed_create_dir')) file_desc = xbmcvfs.File(full_path, 'w') total_len = 0 cancel = False while True: data = response.read(CHUNK_SIZE) if not data: break if pd.is_canceled(): cancel = True break total_len += len(data) if not file_desc.write(data): raise Exception(i18n('failed_write_file')) percent_progress = (total_len) * 100 / content_length if content_length > 0 else 0 log_utils.log('Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG) pd.update(percent_progress) file_desc.close() if not cancel: kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000) log_utils.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) except Exception as e: log_utils.log('Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR) kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)
def attempt_db_recovery(self): header = i18n('recovery_header') if xbmcgui.Dialog().yesno(header, i18n('rec_mig_1'), i18n('rec_mig_2')): try: self.init_database('Unknown') except Exception as e: log_utils.log('DB Migration Failed: %s' % (e), log_utils.LOGWARNING) if self.db_type == DB_TYPES.SQLITE: if xbmcgui.Dialog().yesno(header, i18n('rec_reset_1'), i18n('rec_reset_2'), i18n('rec_reset_3')): try: self.reset_db() except Exception as e: log_utils.log('Reset Failed: %s' % (e), log_utils.LOGWARNING) try: msg = i18n('reset_failed') % (e) except: msg = 'Reset Failed: %s' % (e) else: msg = i18n('db_reset_success') kodi.notify(msg=msg, duration=5000)
def remote_play(source): rpc_client = HttpJSONRPC() command = {'jsonrpc': '2.0', 'id': 1, 'method': 'Player.GetActivePlayers'} response = rpc_client.execute_rpc(command) if 'error' in response: kodi.notify(kodi.get_name(), response['error'], duration=7000) return try: player_id = response['result'][0]['playerid'] except IndexError: player_id = None if player_id == 2: # stop picture player if active, it will block command = {'jsonrpc': '2.0', 'id': 1, 'method': 'Player.Stop', 'params': {'playerid': player_id}} response = rpc_client.execute_rpc(command) if 'error' in response: kodi.notify(kodi.get_name(), response['error'], duration=7000) return if source['is_dash']: filename = kodi.get_plugin_url({'mode': MODES.PLAY, 'player': 'false', 'path': urllib2.quote(source['url']), 'thumb': urllib2.quote(source['art']['thumb']), 'title': urllib2.quote(source['info']['title'])}) else: filename = source['url'] command = {'jsonrpc': '2.0', 'id': 1, 'method': 'Player.Open', 'params': {'item': {'file': filename}}} response = rpc_client.execute_rpc(command) if 'error' in response: kodi.notify(kodi.get_name(), response['error'], duration=7000) else: if 'No Response' not in response['result']: kodi.notify(kodi.get_name(), kodi.i18n('send_success'))
def parallel_get_url(q, scraper, video): worker = threading.current_thread() log_utils.log('Worker: %s (%s) for %s url' % (worker.name, worker, scraper.get_name()), log_utils.LOGDEBUG) url = scraper.get_url(video) log_utils.log('%s returned url %s from %s' % (scraper.get_name(), url, worker), log_utils.LOGDEBUG) if not url: url = '' if url == FORCE_NO_MATCH: label = '[%s] [COLOR green]%s[/COLOR]' % (scraper.get_name(), i18n('force_no_match')) else: label = '[%s] %s' % (scraper.get_name(), url) related = {'class': scraper, 'url': url, 'name': scraper.get_name(), 'label': label} q.put(related)
def get_section_params(section): section_params = {} section_params['section'] = section if section == SECTIONS.TV: section_params['next_mode'] = MODES.SEASONS section_params['folder'] = True section_params['video_type'] = VIDEO_TYPES.TVSHOW section_params['content_type'] = CONTENT_TYPES.TVSHOWS section_params['search_img'] = 'television_search.png' section_params['label_plural'] = i18n('tv_shows') section_params['label_single'] = i18n('tv_show') else: section_params['next_mode'] = MODES.GET_SOURCES section_params['folder'] = kodi.get_setting('source-win') == 'Directory' and kodi.get_setting('auto-play') == 'false' section_params['video_type'] = VIDEO_TYPES.MOVIE section_params['content_type'] = CONTENT_TYPES.MOVIES section_params['search_img'] = 'movies_search.png' section_params['label_plural'] = i18n('movies') section_params['label_single'] = i18n('movie') return section_params
def get_section_params(section): section_params = {} section_params['section'] = section if section == SECTIONS.TV: section_params['next_mode'] = MODES.SEASONS section_params['folder'] = True section_params['video_type'] = VIDEO_TYPES.TVSHOW section_params['content_type'] = CONTENT_TYPES.TVSHOWS section_params['search_img'] = 'television_search.png' section_params['label_plural'] = i18n('tv_shows') section_params['label_single'] = i18n('tv_show') else: section_params['next_mode'] = MODES.GET_SOURCES section_params['folder'] = kodi.get_setting( 'source-win') == 'Directory' and kodi.get_setting( 'auto-play') == 'false' section_params['video_type'] = VIDEO_TYPES.MOVIE section_params['content_type'] = CONTENT_TYPES.MOVIES section_params['search_img'] = 'movies_search.png' section_params['label_plural'] = i18n('movies') section_params['label_single'] = i18n('movie') return section_params
def parallel_get_url(q, scraper, video): worker = threading.current_thread() log_utils.log( 'Worker: %s (%s) for %s url' % (worker.name, worker, scraper.get_name()), log_utils.LOGDEBUG) url = scraper.get_url(video) log_utils.log( '%s returned url %s from %s' % (scraper.get_name(), url, worker), log_utils.LOGDEBUG) if not url: url = '' if url == FORCE_NO_MATCH: label = '[%s] [COLOR green]%s[/COLOR]' % (scraper.get_name(), i18n('force_no_match')) else: label = '[%s] %s' % (scraper.get_name(), url) related = { 'class': scraper, 'url': url, 'name': scraper.get_name(), 'label': label } q.put(related)
def play_this(item, title='', thumbnail='', player=True, history=None): if history is None: history = kodi.get_setting('history-add-on-play') == "true" override_history = kodi.get_setting('history-add-on-play') == "true" stream_url = None headers = None content_type = 'video' override_content_type = None is_dash = False direct = [ 'rtmp:', 'rtmpe:', 'ftp:', 'ftps:', 'special:', 'plugin:', 'udp:', 'upnp:' ] unresolved_source = None label = title source_label = label source_thumbnail = thumbnail history_item = None if item.startswith('http'): with kodi.ProgressDialog( '%s...' % kodi.i18n('resolving'), '%s:' % kodi.i18n('attempting_determine_type'), item) as progress_dialog: while not progress_dialog.is_canceled(): url_override = __check_for_new_url(item) if item != url_override: log_utils.log( 'Source |{0}| has been replaced by |{1}|'.format( item, url_override), log_utils.LOGDEBUG) progress_dialog.update( 5, '%s: %s' % (kodi.i18n('source'), item), '%s: %s' % (kodi.i18n('replaced_with'), url_override), ' ') item = url_override result = __get_content_type_and_headers(item) content_type = result['content_type'] headers = result['headers'] url_override = result['url_override'] if url_override: log_utils.log( 'Source |{0}| has been replaced by |{1}|'.format( item, url_override), log_utils.LOGDEBUG) progress_dialog.update( 10, '%s: %s' % (kodi.i18n('source'), item), '%s: %s' % (kodi.i18n('replaced_with'), url_override), ' ') item = url_override log_utils.log( 'Source |{0}| has media type |{1}|'.format( item, content_type), log_utils.LOGDEBUG) progress_dialog.update( 20, '%s: %s' % (kodi.i18n('source'), item), '%s: %s' % (kodi.i18n('using_media_type'), content_type), ' ') if content_type == 'video' or content_type == 'audio' or content_type == 'image' \ or content_type == 'mpd' or content_type == 'smil': source = item if content_type == 'smil': content_type = 'video' smil_result = __get_html_and_headers(item, headers) source = pick_source( parse_smil_source_list(smil_result['contents'])) elif content_type == 'mpd': content_type = 'video' if not dash_supported: source = None else: is_dash = True if source: stream_url = source elif content_type == 'text': if progress_dialog.is_canceled(): sys.exit(0) progress_dialog.update( 40, '%s: %s' % (kodi.i18n('source'), item), '%s: URLResolver' % kodi.i18n('attempt_resolve_with'), ' ') content_type = 'video' headers.update({'Referer': item}) source = resolve(item, title=title) if source: log_utils.log( 'Source |{0}| was |URLResolver supported|'.format( source), log_utils.LOGDEBUG) sd_result = __check_smil_dash(source, headers) source = sd_result['url'] is_dash = sd_result['is_dash'] if source: progress_dialog.update( 98, '%s: %s' % (kodi.i18n('source'), item), '%s: URLResolver' % kodi.i18n('attempt_resolve_with'), '%s: %s' % (kodi.i18n('resolution_successful'), source)) stream_url = source if not stream_url: if progress_dialog.is_canceled(): sys.exit(0) progress_dialog.update( 60, '%s: %s' % (kodi.i18n('source'), item), '%s: youtube-dl' % kodi.i18n('attempt_resolve_with'), ' ') if ytdl_supported(item): ytdl_result = resolve_youtube_dl(item) if ytdl_result['resolved_url']: headers = ytdl_result['headers'] label = ytdl_result['label'] if ytdl_result[ 'label'] is not None else label source_thumbnail = ytdl_result[ 'thumbnail'] if ytdl_result[ 'thumbnail'] is not None else source_thumbnail log_utils.log( 'Source |{0}| found by |youtube-dl|'. format(ytdl_result['resolved_url']), log_utils.LOGDEBUG) sd_result = __check_smil_dash( ytdl_result['resolved_url'], headers) source = sd_result['url'] is_dash = sd_result['is_dash'] if source: progress_dialog.update( 98, '%s: %s' % (kodi.i18n('source'), item), '%s: youtube-dl' % kodi.i18n('attempt_resolve_with'), '%s: %s' % (kodi.i18n('resolution_successful'), source)) stream_url = source if not progress_dialog.is_canceled(): progress_dialog.update(100, ' ', kodi.i18n('resolution_completed'), ' ') break else: sys.exit(0) if not stream_url: content_type = 'executable' scrape_result = scrape(item) source = scrape_result['resolved_url'] override_content_type = scrape_result['content_type'] unresolved_source = scrape_result['unresolved_url'] source_label = scrape_result['label'] headers = scrape_result['headers'] if scrape_result['thumbnail']: source_thumbnail = scrape_result['thumbnail'] if scrape_result['title']: label = scrape_result['title'] if source: log_utils.log( 'Source |{0}| found by |Scraping for supported|'.format( source), log_utils.LOGDEBUG) if override_content_type == 'video': sd_result = __check_smil_dash(source, headers) source = sd_result['url'] is_dash = sd_result['is_dash'] if source: stream_url = source if stream_url: stream_url = stream_url.replace(r'\\', '') elif any(item.startswith(p) for p in direct): log_utils.log('Source |{0}| may be supported'.format(item), log_utils.LOGDEBUG) stream_url = item if is_dash and (not dash_supported or not kodi.addon_enabled('inputstream.adaptive')): stream_url = None if stream_url and (content_type == 'video' or content_type == 'audio' or content_type == 'image' or content_type == 'executable'): working_dialog = kodi.WorkingDialog() with working_dialog: play_history = utils.PlayHistory() working_dialog.update(20) if history or player == 'history': history_item = item.split('|')[0] if '%' not in history_item: history_item = urllib2.quote(history_item) log_utils.log( 'Adding source |{0}| to history with content_type |{1}|'. format(item, content_type), log_utils.LOGDEBUG) play_history.add(history_item, content_type, label if label else item, urllib2.quote(thumbnail)) working_dialog.update(40) if override_content_type and override_history: history_item = stream_url if history_item.startswith('plugin://') or unresolved_source: history_item = unresolved_source history_item = history_item.split('|')[0] if '%' not in history_item: history_item = urllib2.quote(history_item) log_utils.log( 'Adding source |{0}| to history with content_type |{1}|'. format(unresolved_source, override_content_type), log_utils.LOGDEBUG) play_history.add(history_item, override_content_type, source_label, urllib2.quote(source_thumbnail)) if player == 'history': return if history_item: kodi.refresh_container() working_dialog.update(60) if (not stream_url.startswith('plugin://')) and (headers is not None): stream_url = get_url_with_headers(stream_url, headers) working_dialog.update(80) if any(plugin_id in stream_url for plugin_id in RUNPLUGIN_EXCEPTIONS): log_utils.log('Running plugin: |{0!s}|'.format(stream_url), log_utils.LOGDEBUG) kodi.execute_builtin('RunPlugin(%s)' % stream_url) else: if override_content_type: content_type = override_content_type source = { 'content_type': content_type, 'url': stream_url, 'is_dash': is_dash, 'info': { 'title': source_label }, 'art': { 'icon': source_thumbnail, 'thumb': source_thumbnail } } play(source, player) else: log_utils.log('Found no potential sources: |{0!s}|'.format(item), log_utils.LOGDEBUG)
def download_media(url, path, file_name): try: progress = int(kodi.get_setting('down_progress')) active = not progress == PROGRESS.OFF background = progress == PROGRESS.BACKGROUND with kodi.ProgressDialog('Premiumize Cloud', i18n('downloading') % (file_name), background=background, active=active) as pd: request = urllib2.Request(url) request.add_header('User-Agent', USER_AGENT) request.add_unredirected_header('Host', request.get_host()) response = urllib2.urlopen(request) content_length = 0 if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) file_name = file_name.replace('.strm', get_extension(url, response)) full_path = os.path.join(path, file_name) log_utils.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) path = xbmc.makeLegalFilename(path) try: try: xbmcvfs.mkdirs(path) except: os.makedirs(path) except Exception as e: log_utils.log('Dir Create Failed: %s' % (e), log_utils.LOGDEBUG) if not xbmcvfs.exists(path): raise Exception(i18n('failed_create_dir')) file_desc = xbmcvfs.File(full_path, 'w') total_len = 0 cancel = False while True: data = response.read(CHUNK_SIZE) if not data: break if pd.is_canceled(): cancel = True break total_len += len(data) if not file_desc.write(data): raise Exception(i18n('failed_write_file')) percent_progress = ( total_len ) * 100 / content_length if content_length > 0 else 0 log_utils.log( 'Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG) pd.update(percent_progress) file_desc.close() if not cancel: kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000) log_utils.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) except Exception as e: log_utils.log( 'Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR) kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)
def download_media(url, path, file_name): try: progress = int(kodi.get_setting('down_progress')) request = urllib2.Request(url) request.add_header('User-Agent', USER_AGENT) request.add_unredirected_header('Host', request.get_host()) response = urllib2.urlopen(request) content_length = 0 if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) file_name = file_name.replace('.strm', get_extension(url, response)) full_path = os.path.join(path, file_name) log_utils.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) path = xbmc.makeLegalFilename(path) if not xbmcvfs.exists(path): try: try: xbmcvfs.mkdirs(path) except: os.mkdir(path) except Exception as e: raise Exception(i18n('failed_create_dir')) file_desc = xbmcvfs.File(full_path, 'w') total_len = 0 if progress: if progress == PROGRESS.WINDOW: dialog = xbmcgui.DialogProgress() else: dialog = xbmcgui.DialogProgressBG() dialog.create('Stream All The Sources', i18n('downloading') % (file_name)) dialog.update(0) while True: data = response.read(CHUNK_SIZE) if not data: break if progress == PROGRESS.WINDOW and dialog.iscanceled(): break total_len += len(data) if not file_desc.write(data): raise Exception('failed_write_file') percent_progress = ( total_len) * 100 / content_length if content_length > 0 else 0 log_utils.log( 'Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG) if progress == PROGRESS.WINDOW: dialog.update(percent_progress) elif progress == PROGRESS.BACKGROUND: dialog.update(percent_progress, 'Stream All The Sources') else: kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000) log_utils.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) file_desc.close() if progress: dialog.close() except Exception as e: log_utils.log( 'Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR) kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)
def download_media(url, path, file_name): try: progress = int(kodi.get_setting('down_progress')) request = urllib2.Request(url) request.add_header('User-Agent', USER_AGENT) request.add_unredirected_header('Host', request.get_host()) response = urllib2.urlopen(request) content_length = 0 if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) file_name = file_name.replace('.strm', get_extension(url, response)) full_path = os.path.join(path, file_name) log_utils.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) path = xbmc.makeLegalFilename(path) if not xbmcvfs.exists(path): try: try: xbmcvfs.mkdirs(path) except: os.mkdir(path) except Exception as e: raise Exception(i18n('failed_create_dir')) file_desc = xbmcvfs.File(full_path, 'w') total_len = 0 if progress: if progress == PROGRESS.WINDOW: dialog = xbmcgui.DialogProgress() else: dialog = xbmcgui.DialogProgressBG() dialog.create('Stream All The Sources', i18n('downloading') % (file_name)) dialog.update(0) while True: data = response.read(CHUNK_SIZE) if not data: break if progress == PROGRESS.WINDOW and dialog.iscanceled(): break total_len += len(data) if not file_desc.write(data): raise Exception('failed_write_file') percent_progress = (total_len) * 100 / content_length if content_length > 0 else 0 log_utils.log('Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG) if progress == PROGRESS.WINDOW: dialog.update(percent_progress) elif progress == PROGRESS.BACKGROUND: dialog.update(percent_progress, 'Stream All The Sources') else: kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000) log_utils.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) file_desc.close() if progress: dialog.close() except Exception as e: log_utils.log('Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR) kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)
def download_media(url, path, file_name): try: progress = int(kodi.get_setting('down_progress')) active = not progress == PROGRESS.OFF background = progress == PROGRESS.BACKGROUND with kodi.ProgressDialog(kodi.get_name(), i18n('downloading') % (file_name), background=background, active=active) as pd: try: headers = dict([item.split('=') for item in (url.split('|')[1]).split('&')]) for key in headers: headers[key] = urllib.unquote(headers[key]) except: headers = {} if 'User-Agent' not in headers: headers['User-Agent'] = USER_AGENT request = urllib2.Request(url.split('|')[0], headers=headers) response = urllib2.urlopen(request) if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) else: content_length = 0 file_name = file_name.replace('.strm', get_extension(url, response)) full_path = os.path.join(path, file_name) log_utils.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) path = xbmc.makeLegalFilename(path) try: try: xbmcvfs.mkdirs(path) except: os.makedirs(path) except Exception as e: log_utils.log('Path Create Failed: %s (%s)' % (e, path), log_utils.LOGDEBUG) if not path.endswith(os.sep): path += os.sep if not xbmcvfs.exists(path): raise Exception(i18n('failed_create_dir')) file_desc = xbmcvfs.File(full_path, 'w') total_len = 0 cancel = False while True: data = response.read(CHUNK_SIZE) if not data: break if pd.is_canceled(): cancel = True break total_len += len(data) if not file_desc.write(data): raise Exception(i18n('failed_write_file')) percent_progress = (total_len) * 100 / content_length if content_length > 0 else 0 log_utils.log('Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG) pd.update(percent_progress) file_desc.close() if not cancel: kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000) log_utils.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) except Exception as e: log_utils.log('Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR) kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)
def scrape_supported(url, html, regex): parsed_url = urlparse.urlparse(url) links = [] _filter = [ '.js', 'data:', 'blob:', 'tab=', 'usp=', '/pixel.', '/1x1.', 'javascript:', 'rss.', 'blank.', '.rss' ] sources = [] with kodi.ProgressDialog('%s...' % kodi.i18n('scraping_for_potential_urls'), '%s: %s' % (kodi.i18n('source'), url), ' ', timer=0.2) as progress_dialog: while not progress_dialog.is_canceled(): new_iter = re.findall(regex, html, re.DOTALL) len_iter = len(new_iter) for index, match in enumerate(new_iter): if progress_dialog.is_canceled(): sys.exit(0) percent = int((float(index) / float(len_iter)) * 100) stream_url = match[0] if stream_url == '#' or stream_url == '//' or '/' not in stream_url or not re.match('^[hruf:/].+', stream_url) or \ any(item in stream_url for item in _filter) or any(stream_url == t[1] for t in links): progress_dialog.update( percent, kodi.i18n('preparing_results'), '%s: %s' % (kodi.i18n('discarded'), '%s' % stream_url), ' ') continue stream_url = __check_for_new_url(stream_url).replace(r'\\', '') if stream_url.startswith('//'): stream_url = '%s:%s' % (parsed_url.scheme, stream_url) elif stream_url.startswith('/'): stream_url = '%s://%s%s' % ( parsed_url.scheme, parsed_url.hostname, stream_url) host = urlparse.urlparse(stream_url).hostname if host is None: continue label = host if (len(match) > 2) and (match[2] is not None) and ( match[2].strip()) and (host not in match[2]): label = match[2].strip() elif (len(match) > 1) and (match[1] is not None) and ( match[1].strip()) and (host not in match[1]): label = match[1].strip() if not isinstance(label, unicode): label = label.decode('utf-8', 'ignore') try: parser = HTMLParser() label = parser.unescape(label) try: label = parser.unescape(label) except: pass except: pass progress_dialog.update(percent, kodi.i18n('preparing_results'), '%s: %s' % (kodi.i18n('added'), label), stream_url) sources.append((label, stream_url)) if progress_dialog.is_canceled(): sys.exit(0) break if progress_dialog.is_canceled(): sys.exit(0) with kodi.ProgressDialog('%s...' % kodi.i18n('scraping_for_potential_urls'), '%s: %s' % (kodi.i18n('source'), url), ' ', timer=0.1) as progress_dialog: while not progress_dialog.is_canceled(): len_iter = len(sources) for index, source in enumerate(sources): if progress_dialog.is_canceled(): sys.exit(0) percent = int((float(index) / float(len_iter)) * 100) label = source[0] stream_url = source[1] hmf = HostedMediaFile(url=stream_url, include_disabled=False) potential_type = __get_potential_type(stream_url) is_valid = hmf.valid_url() is_valid_type = (potential_type != 'audio') and (potential_type != 'image') if is_valid and is_valid_type: progress_dialog.update( percent, kodi.i18n('check_for_support'), '%s [%s]: %s' % (kodi.i18n('support_potential'), 'video', 'URLResolver'), '[%s]: %s' % (label, stream_url)) links.append({ 'label': label, 'url': stream_url, 'resolver': 'URLResolver', 'content_type': 'video' }) continue else: if potential_type == 'text': if ytdl_supported(stream_url): progress_dialog.update( percent, kodi.i18n('check_for_support'), '%s [%s]: %s' % (kodi.i18n('support_potential'), 'video', 'youtube-dl'), '[%s]: %s' % (label, stream_url)) links.append({ 'label': label, 'url': stream_url, 'resolver': 'youtube-dl', 'content_type': 'video' }) continue progress_dialog.update( percent, kodi.i18n('check_for_support'), '%s [%s]: %s' % (kodi.i18n('support_potential'), potential_type, 'None'), '[%s]: %s' % (label, stream_url)) else: progress_dialog.update( percent, kodi.i18n('check_for_support'), '%s [%s]: %s' % (kodi.i18n('support_potential'), potential_type, 'Kodi'), '[%s]: %s' % (label, stream_url)) links.append({ 'label': label, 'url': stream_url, 'resolver': None, 'content_type': potential_type }) continue if progress_dialog.is_canceled(): sys.exit(0) break if progress_dialog.is_canceled(): sys.exit(0) return links
def download_media(url, path, file_name): try: progress = int(kodi.get_setting('down_progress')) active = not progress == PROGRESS.OFF background = progress == PROGRESS.BACKGROUND with kodi.ProgressDialog(kodi.get_name(), i18n('downloading') % (file_name), background=background, active=active) as pd: try: headers = dict([ item.split('=') for item in (url.split('|')[1]).split('&') ]) for key in headers: headers[key] = urllib.unquote(headers[key]) except: headers = {} if 'User-Agent' not in headers: headers['User-Agent'] = USER_AGENT request = urllib2.Request(url.split('|')[0], headers=headers) response = urllib2.urlopen(request) if 'Content-Length' in response.info(): content_length = int(response.info()['Content-Length']) else: content_length = 0 file_name = file_name.replace('.strm', get_extension(url, response)) full_path = os.path.join(path, file_name) log_utils.log('Downloading: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) path = xbmc.makeLegalFilename(path) try: try: xbmcvfs.mkdirs(path) except: os.makedirs(path) except Exception as e: log_utils.log('Path Create Failed: %s (%s)' % (e, path), log_utils.LOGDEBUG) if not path.endswith(os.sep): path += os.sep if not xbmcvfs.exists(path): raise Exception(i18n('failed_create_dir')) file_desc = xbmcvfs.File(full_path, 'w') total_len = 0 cancel = False while True: data = response.read(CHUNK_SIZE) if not data: break if pd.is_canceled(): cancel = True break total_len += len(data) if not file_desc.write(data): raise Exception(i18n('failed_write_file')) percent_progress = ( total_len ) * 100 / content_length if content_length > 0 else 0 log_utils.log( 'Position : %s / %s = %s%%' % (total_len, content_length, percent_progress), log_utils.LOGDEBUG) pd.update(percent_progress) file_desc.close() if not cancel: kodi.notify(msg=i18n('download_complete') % (file_name), duration=5000) log_utils.log('Download Complete: %s -> %s' % (url, full_path), log_utils.LOGDEBUG) except Exception as e: log_utils.log( 'Error (%s) during download: %s -> %s' % (str(e), url, file_name), log_utils.LOGERROR) kodi.notify(msg=i18n('download_error') % (str(e), file_name), duration=5000)