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_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 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 refresh(media_id):
    details_cache = container.details_cache()
    folders_cache = container.folders_cache()
    meta_cache = container.meta_cache()
    if media_id in details_cache:
        del(details_cache[media_id])
    if media_id in folders_cache:
        del(folders_cache[media_id])
    if media_id in meta_cache:
        del(meta_cache[media_id])
    plugin.refresh()
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 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 refresh_all():
    container.details_cache().clear()
    container.folders_cache().clear()
    container.meta_cache().clear()
    container.search_cache().clear()
    plugin.refresh()