def update_library(): import mediapoisk.container as container from plugin import plugin from mediapoisk.common import lang, batch, abort_requested from xbmcswift2 import xbmcgui from contextlib import closing log = logging.getLogger(__name__) library_manager = container.library_manager() scraper = container.scraper() media_ids = library_manager.stored_media_ids() if media_ids: log.info("Starting MediaPoisk.info library update...") progress = xbmcgui.DialogProgressBG() with closing(progress): progress.create(lang(30000), lang(40322)) processed = 0 for section, media_ids in media_ids.iteritems(): for ids in batch(media_ids): all_details = scraper.get_details_bulk(section, ids) all_folders = scraper.get_folders_bulk(section, ids) for media_id, details in all_details.items(): if media_id in all_folders: for folder in all_folders[media_id]: if library_manager.has_folder(folder.id): library_manager.update_folder(details, folder) processed += len(ids) progress.update(processed*100/len(media_ids)) if abort_requested(): break log.info("MediaPoisk.ru library update finished.") if plugin.get_setting('update-xbmc-library', bool): log.info("Starting XBMC library update...") plugin.update_library('video', library_manager.path)
def show_files(section, media_id, folder_id): scraper = container.scraper() section = Section.find(section) plugin.set_content("movies") files = scraper.get_files_cached(section, media_id, folder_id) plugin.add_items(itemify_file(f) for f in files) plugin.finish(sort_methods=["unsorted", "title", "duration", "size"])
def itemify_search_results(section, results): """ :type results: list[Media] """ ids = [result.id for result in results] scraper = container.scraper() meta_cache = container.meta_cache() all_details = scraper.get_details_bulk(section, ids) watched_items = container.watched_items() items = [] for media in results: details = all_details[media.id] is_series = details.section.is_series() watched = watched_items.is_watched(media.id, date_added=media.date if is_series else None) meta = meta_cache.setdefault(media.id, {}) meta.update({ 'date_added': media.date, 'is_series': is_series, }) item = itemify_details(details) item.update({ 'label': tf.media_title(media), 'label2': date_to_str(media.date), 'context_menu': search_result_context_menu(details, media.date), }) item['info'].update({ 'date': date_to_str(media.date), 'playcount': int(watched), }) items.append(item) return items
def play_file(section, media_id, url, title): stream = container.torrent_stream() scraper = container.scraper() history = container.history() meta_cache = container.meta_cache() meta = meta_cache.setdefault(media_id, {}) section = Section.find(section) details = scraper.get_details_cached(section, media_id) item = itemify_details(details) title = u"%s / %s" % (ensure_unicode(title), item["info"]["title"]) item["info"]["title"] = title history.add(media_id, details.section, title, plugin.request.url, url, details.poster) history.storage.sync() torrent = container.torrent(url=url) player = container.player() def check_and_mark_watched(event): log.info("Playback event: %s, current player progress: %d", event, player.get_percent()) if player.get_percent() >= 90 and plugin.request.arg("can_mark_watched"): watched_items = container.watched_items() watched_items.mark(media_id, date_added=meta.get("date_added"), total_size=meta.get("total_size")) player.attach([player.PLAYBACK_STOPPED, player.PLAYBACK_ENDED], check_and_mark_watched) temp_files = stream.play(player, torrent, item) if temp_files: save_files(temp_files, rename=not stream.saved_files_needed, on_finish=purge_temp_dir) else: purge_temp_dir()
def itemify_single_result(result, folders=None): """ :type result: Details """ media_id = result.media_id scraper = container.scraper() folders = folders or scraper.get_folders_cached(media_id) watched_items = container.watched_items() total_size = sum(f.size for f in folders) is_series = result.section.is_series() watched = watched_items.is_watched(media_id, total_size=total_size if is_series else None) meta_cache = container.meta_cache() meta = meta_cache.setdefault(media_id, {}) meta.update({ 'total_size': total_size, 'is_series': is_series, }) item = itemify_details(result) item.update({ 'label': tf.bookmark_title(result, folders), 'context_menu': search_result_context_menu(result, total_size=total_size), }) item['info'].update({ 'playcount': int(watched), }) return item
def turn_off_auto_refresh(section, media_id): section = Section.find(section) scraper = container.scraper() scraper.get_details_cached(section, media_id) scraper.get_folders_cached(section, media_id) container.details_cache().protect_item(media_id) container.folders_cache().protect_item(media_id) not_refreshing_items = container.not_refreshing_items() not_refreshing_items[media_id] = True plugin.refresh()
def add_to_library(section, media_id, folder_id): scraper = container.scraper() section = Section.find(section) details = scraper.get_details_cached(section, media_id) folder = scraper.get_folder_cached(section, media_id, folder_id) library_manager = container.library_manager() library_manager.update_folder(details, folder) plugin.refresh() if plugin.get_setting('update-xbmc-library', bool): plugin.update_library('video', library_manager.path)
def itemify_bookmarks(bookmarks): scraper = container.scraper() by_section = {} for b in bookmarks: by_section.setdefault(b.section, []).append(b.media_id) details = {} folders = {} for section, ids in by_section.iteritems(): details[section] = scraper.get_details_bulk(section, ids) folders[section] = scraper.get_folders_bulk(section, ids) return [itemify_single_result(details[b.section][b.media_id], folders[b.section][b.media_id]) for b in bookmarks]
def mark_watched(section, media_id): meta_cache = container.meta_cache() section = Section.find(section) watched_items = container.watched_items() meta = meta_cache.get(media_id, {}) total_size = meta.get('total_size') date_added = meta.get('date_added') if total_size is None: scraper = container.scraper() folders = scraper.get_folders_cached(section, media_id) total_size = sum(f.size for f in folders) meta['total_size'] = total_size meta_cache[media_id] = meta watched_items.mark(media_id, True, date_added=date_added, total_size=total_size) plugin.refresh()
def library_items(): scraper = container.scraper() library_manager = container.library_manager() for section, media_ids in library_manager.stored_media_ids().items(): for ids in batch(media_ids): if abort_requested(): break all_folders = scraper.get_folders_bulk(section, ids) all_details = scraper.get_details_bulk(section, ids) items = [ itemify_library_folder(all_details[media_id], f) for media_id, folders in all_folders.iteritems() for f in folders if library_manager.has_folder(f.id) ] plugin.add_items(items) plugin.finish(sort_methods=["title"], cache_to_disc=False)
def show_folders(section, media_id): section = Section.find(section) scraper = container.scraper() meta_cache = container.meta_cache() meta = meta_cache.setdefault(media_id, {}) plugin.set_content("movies") folders = scraper.get_folders_cached(section, media_id) total_size = sum(f.size for f in folders) meta["total_size"] = total_size for f in folders: if len(f.files) == 1 and not meta.get("is_series"): item = itemify_file(f.files[0], can_mark_watched=1) item["label"] = tf.folder_file_title(f, f.files[0]) item["context_menu"] += library_context_menu(section, media_id, f.id) else: item = itemify_folder(f) plugin.add_item(item) plugin.finish(sort_methods=["unsorted", "title", "duration", "size"])
def make_search(sf, header=None, cache_to_disc=False, update_listing=False): skip = plugin.request.arg('skip') scraper = container.scraper() results = scraper.search_cached(sf, skip) if not results: return False if isinstance(results, Details): if header: plugin.add_items(with_fanart(header)) item = itemify_single_result(results) plugin.finish(items=[item], cache_to_disc=cache_to_disc, update_listing=update_listing) return True total = len(results) items = [] if skip: skip_prev = max(skip - sf.page_size, 0) total += 1 items.append({ 'label': lang(34003), 'path': plugin.request.url_with_params(skip=skip_prev) }) elif header: items.extend(header) total += len(header) plugin.add_items(with_fanart(items), total) for batch_res in batch(results): if abort_requested(): break items = itemify_search_results(sf.section, batch_res) plugin.add_items(items, total) items = [] if scraper.has_more: skip_next = (skip or 0) + sf.page_size items.append({ 'label': lang(34004), 'path': plugin.request.url_with_params(skip=skip_next) }) plugin.finish(items=with_fanart(items), sort_methods=['unsorted', 'date', 'title', 'video_year', 'video_rating'], cache_to_disc=cache_to_disc, update_listing=update_listing or skip is not None) return True