def __init__(self, show, seasonnr, episodenr):
     self.showtitle = show.title
     self.seasonnr = int(seasonnr)
     self.episodenr = int(episodenr)
     self.code = "S%02dE%02d" % (seasonnr, episodenr)
     self.path = utils.uni_join(const.libpath, "%s shows" % const.provider,
                                utils.stringtofile(self.showtitle), "Season %s" % self.seasonnr)
     self.htmfilename = "%s %s.htm" % (utils.stringtofile(self.showtitle), self.code)
     self.nfofilename = "%s %s.nfo" % (utils.stringtofile(self.showtitle), self.code)
     self.jsonfilename = "%s %s.json" % (utils.stringtofile(self.showtitle), self.code)
 def __init__(self, title):
     self.title = title
     self.path = utils.uni_join(const.libpath, "%s movies" % const.provider)
     self.htmfilename = "%s.htm" % utils.stringtofile(self.title)
     self.nfofilename = "%s.nfo" % utils.stringtofile(self.title)
     self.jsonfilename = "%s.json" % utils.stringtofile(self.title)
    def get_stored_episodes(self):
        koala_stored_episodes = self.get_koala_stored_eps()
        if koala_stored_episodes:
            any_ep_kodiid = next(iter(koala_stored_episodes)).kodiid
        else:
            # no stored koala episode, get any stored episode
            episodes = kodi.rpc("VideoLibrary.GetEpisodes", limits={"start": 0, "end": 1},
                                filter={"field": "filename", "operator": "startswith", "value": "%s S" % utils.stringtofile(self.title)},
                                ).get('episodes', [])
            if not episodes:
                # no stored episodes detectable
                return set(), set()
            any_ep_kodiid = episodes[0]['episodeid']
        any_episode = kodi.rpc("VideoLibrary.GetEpisodeDetails", episodeid=any_ep_kodiid, properties=["showtitle"])
        scraped_title = any_episode['episodedetails']['showtitle']

        all_stored_episodes_dict = kodi.rpc("VideoLibrary.GetEpisodes",
                                            properties=["season", "episode"],
                                            filter={"field": "tvshow", "operator": "is", "value": scraped_title})
        all_stored_episodes_set = set(BaseEpisode(show=self, seasonnr=epdict['season'], episodenr=epdict['episode'])
                                      for epdict in all_stored_episodes_dict.get('episodes', []))
        return koala_stored_episodes, all_stored_episodes_set
def rpc(method, multifilter=False, **kwargs):
    if method == "VideoLibrary.GetMovies":
        filename = multifilter.values()[0][0][2]
        path = multifilter.values()[0][1][2]
        filepath = utils.os_join(path, filename)
        files = glob.glob(filepath)

        result = {"movies": []}
        for filepath in files:
            if filepath in nfo_needed and not os.path.isfile(filepath.replace(".htm", ".nfo")):
                continue
            result['movies'].append({"movieid": filepath,
                                     "file": filepath,
                                     'playcount': playcounts.get(filepath, 0)})
        return result

    elif method == "VideoLibrary.GetEpisodes":
        # get_koala_stored_eps
        if kwargs["filter"]["field"] == "path":
            showpath = kwargs["filter"]["value"]
            files = glob.glob(utils.os_join(showpath, "*", "*.htm"))

        # no stored koala episode, get any stored episode
        elif kwargs["filter"]["field"] == "filename":
            filename = kwargs["filter"]["value"]
            files = glob.glob(utils.os_join(mock_constants.libpath, "%s shows" % const.provider, "*", "*", filename))

        # all_stored_episodes
        elif kwargs["filter"]["field"] == "tvshow":
            showtitle = kwargs["filter"]["value"]
            files = glob.glob(utils.os_join(mock_constants.libpath, "%s shows" % const.provider, utils.stringtofile(showtitle), "*", "*.htm"))

        result = {"episodes": []}
        for filepath in files:
            seasonnr, episodenr, = re.match(r".*S(\d\d)E(\d\d).htm", filepath).groups()
            if filepath in nfo_needed and not os.path.isfile(filepath.replace(".htm", ".nfo")):
                continue
            result["episodes"].append({"episodeid": filepath,
                                       "file": filepath,
                                       'playcount': playcounts.get(filepath, 0),
                                       "season": int(seasonnr),
                                       'episode': int(episodenr)})
        return result

    # any_episode, episodeid=any_ep_kodiid
    elif method == "VideoLibrary.GetEpisodeDetails":
        filepath = kwargs["episodeid"]
        path, filename = os.path.split(filepath)
        showtitle = re.match(r"(.*) S\d\dE\d\d.htm", filename).groups()[0]
        return {'episodedetails': {"episodeid": filepath, "showtitle": showtitle}}
 def __init__(self, urlid, title):
     self.urlid = urlid
     self.title = title
     self.path = utils.uni_join(const.libpath, "%s shows" % const.provider, utils.stringtofile(self.title))
     self.nfofilename = "tvshow.nfo"
 def setUpClass(cls):
     library.scraper.getshowinfo = fake_getshowinfo
     library.scraper.getinfodict = fake_getepinfo
     library.scraper.getepisodes = fake_getepisodes_return_A
     cls.show = library.Show(urlid="faketvshowurlid", title="The Fake Koala %s Show" % const.provider)
     cls.episodepath = uni_join(const.libpath, "%s shows" % const.provider, "%s\\" % stringtofile(cls.show.title))
     library.execute(to_update_add=[cls.show])
 def setUpClass(cls):
     library.scraper.getepisodes = fake_getepisodes_return_A_B
     cls.show = library.Show(urlid="broadchurch", title="Broadchurch")
     library.execute(to_update_add=[cls.show])
     cls.episodepath = uni_join(const.libpath, "%s shows" % const.provider, "%s\\" % stringtofile(cls.show.title))
     libfiles = [show["file"] for show in rpc("VideoLibrary.GetTVShows", properties=["file"])['tvshows']]
     assert cls.episodepath in libfiles
     library.execute(to_remove=[cls.show])
 def test_add_show_to_kodi_lib(self):
     """Does show.add_update() cause the show to be added to kodi library?"""
     episodepath = uni_join(const.libpath, "%s shows" % const.provider, "%s\\" % stringtofile(self.show.title))
     libfiles = [show["file"] for show in rpc("VideoLibrary.GetTVShows", properties=["file"])['tvshows']]
     self.assertIn(episodepath, libfiles)
def check_episode(name, ext, season=None, episode=None):
    path = utils.os_join(mock_constants.libpath, "%s shows" % const.provider, utils.stringtofile(name), "Season %s" % season,
                         "%s S%02dE%02d.%s" % (utils.stringtofile(name), season, episode, ext))
    return path
def check_movie(name, ext, season=None, episode=None):
    path = utils.os_join(mock_constants.libpath, "%s movies" % const.provider, "%s.%s" % (utils.stringtofile(name), ext))
    return path