Ejemplo n.º 1
0
def main():
    debug_info_label("ListItem.FileName")
    debug_info_label("ListItem.Path")
    debug_info_label("ListItem.FileExtension")
    debug_info_label("ListItem.FileNameAndPath")
    debug_info_label("ListItem.DBID")  # movie id)
    debug_info_label("ListItem.IMDBNumber")

    debug_info_label("Container.FolderPath")

    # import rpdb2
    # rpdb2.start_embedded_debugger('pw')

    import player

    settings = player.load_settings()

    path = xbmc.getInfoLabel("ListItem.FileNameAndPath")
    name = xbmc.getInfoLabel("ListItem.FileName")

    import xbmcvfs, os

    tempPath = xbmc.translatePath("special://temp")
    if xbmcvfs.exists(path + ".alternative"):
        debug("path exists")
        xbmcvfs.copy(path, os.path.join(tempPath, name))
        xbmcvfs.copy(path + ".alternative", os.path.join(tempPath, name + ".alternative"))
        path = os.path.join(tempPath, name)

    links = STRMWriterBase.get_links_with_ranks(path.decode("utf-8"), settings, use_scrape_info=True)

    window = MyWindow("Media Aggregator", settings=settings, links=links)
    window.doModal()

    debug(window.has_choice)
    debug(window.has_select_file)

    if not window.has_choice and not window.has_select_file:
        del window
        return

    cursel = window.list.getSelectedItem()
    debug(cursel.getLabel())
    link = cursel.getProperty("link")
    debug(link)

    if link == "plugin://script.media.aggregator/?action=settings":
        xbmc.executebuiltin("Addon.OpenSettings(script.media.aggregator)")
        del window
        return

    if link == "plugin://script.media.aggregator/?action=show_similar":
        imdb_id = xbmc.getInfoLabel("ListItem.IMDBNumber")
        type = "movie"

        if not imdb_id and xbmc.getInfoLabel("ListItem.DBTYPE") == "episode":
            from nforeader import NFOReader

            nfo_path = xbmc.getInfoLabel("ListItem.FileNameAndPath").replace(".strm", ".nfo").decode("utf-8")
            debug(nfo_path)
            rd = NFOReader(nfo_path, "")
            tvs_rd = rd.tvs_reader()
            imdb_id = tvs_rd.imdb_id()
            type = "tv"

        if imdb_id:
            from movieapi import MovieAPI

            res = MovieAPI.tmdb_by_imdb(imdb_id, type)
            debug(res)
            if res and len(res) > 0:
                tmdb_id = res[0].tmdb_id()
                xbmc.executebuiltin(
                    'Container.Update("plugin://script.media.aggregator/?action=show_similar&tmdb=%s")' % tmdb_id
                )
                del window
                return

    if link == "plugin://script.media.aggregator/?action=add_media":
        imdb_id = xbmc.getInfoLabel("ListItem.IMDBNumber")
        title = xbmc.getInfoLabel("ListItem.Title")

        debug(imdb_id)
        debug(title)

        from service import add_media

        add_media(title.decode("utf-8"), imdb_id)
        return

    selected_file = None
    if window.has_select_file:
        # selected_file = window.files.getSelectedItem().getLabel()
        selected_file = window.files.getSelectedItem().getProperty("index")

    del window

    with filesystem.fopen(path.decode("utf-8"), "r") as strm:
        src_link = strm.read()
        debug(src_link)
        pattern = "torrent=(.+?)&"
        match = re.search(pattern, str(link))
        if not match:
            pattern2 = "torrent=(.+)"
            match = re.search(pattern2, str(link))

        if match:
            dst_link = re.sub(pattern, "torrent=" + match.group(1) + "&", str(src_link)) + "&onlythis=true"
            debug(dst_link)

            if selected_file:
                # import urllib
                # from tvshowapi import cutStr
                # dst_link += '&cutName=' + urllib.quote(cutStr(selected_file))
                dst_link += "&index=" + str(selected_file)

            xbmc.executebuiltin("xbmc.PlayMedia(" + dst_link + ")")

    if tempPath in path:
        xbmcvfs.delete(path)
        xbmcvfs.delete(path + ".alternative")