Esempio n. 1
0
def get_external_ids(li, season=None, episode=None):
    trakt_api = TraktAPI()
    unique_id, trakt_type = None, None
    if li.infolabels.get('mediatype') == 'movie':
        unique_id = li.unique_ids.get('tmdb')
        trakt_type = 'movie'
    elif li.infolabels.get('mediatype') == 'tvshow':
        unique_id = li.unique_ids.get('tmdb')
        trakt_type = 'show'
    elif li.infolabels.get('mediatype') in ['season', 'episode']:
        unique_id = li.unique_ids.get('tvshow.tmdb')
        trakt_type = 'show'
    if not unique_id or not trakt_type:
        return
    trakt_slug = trakt_api.get_id(id_type='tmdb',
                                  unique_id=unique_id,
                                  trakt_type=trakt_type,
                                  output_type='slug')
    if not trakt_slug:
        return
    details = trakt_api.get_details(trakt_type, trakt_slug, extended=None)
    if not details:
        return
    if li.infolabels.get('mediatype') in ['movie', 'tvshow', 'season']:
        return {
            'unique_ids': {
                'tmdb': unique_id,
                'tvdb': details.get('ids', {}).get('tvdb'),
                'imdb': details.get('ids', {}).get('imdb'),
                'slug': details.get('ids', {}).get('slug'),
                'trakt': details.get('ids', {}).get('trakt')
            }
        }
    episode_details = trakt_api.get_details(trakt_type,
                                            trakt_slug,
                                            season=season
                                            or li.infolabels.get('season'),
                                            episode=episode
                                            or li.infolabels.get('episode'),
                                            extended=None)
    if episode_details:
        return {
            'unique_ids': {
                'tvshow.tmdb': unique_id,
                'tvshow.tvdb': details.get('ids', {}).get('tvdb'),
                'tvshow.imdb': details.get('ids', {}).get('imdb'),
                'tvshow.slug': details.get('ids', {}).get('slug'),
                'tvshow.trakt': details.get('ids', {}).get('trakt'),
                'tvdb': episode_details.get('ids', {}).get('tvdb'),
                'tmdb': episode_details.get('ids', {}).get('tmdb'),
                'imdb': episode_details.get('ids', {}).get('imdb'),
                'slug': episode_details.get('ids', {}).get('slug'),
                'trakt': episode_details.get('ids', {}).get('trakt')
            }
        }
class SyncItem():
    def __init__(self,
                 trakt_type,
                 unique_id,
                 season=None,
                 episode=None,
                 id_type=None):
        self.trakt_type = trakt_type
        self.unique_id = unique_id
        self.season = try_int(season) if season is not None else None
        self.episode = try_int(episode) if episode is not None else None
        self.id_type = id_type
        self.trakt_api = TraktAPI()

    def _build_choices(self):
        choices = self._user_list_check()
        choices += [
            j for j in (self._sync_item_check(**i)
                        for i in _sync_item_methods()) if j
        ]
        choices += [{
            'name': ADDON.getLocalizedString(32304),
            'method': 'comments'
        }]
        return choices

    def _user_list_check(self):
        if xbmc.getInfoLabel("ListItem.Property(param.owner)") == 'true':
            return [{
                'name': ADDON.getLocalizedString(32355),
                'method': 'userlist',
                'remove': True
            }]
        return [{
            'name': ADDON.getLocalizedString(32298),
            'method': 'userlist'
        }]

    def _sync_item_check(self,
                         sync_type=None,
                         method=None,
                         name_add=None,
                         name_remove=None,
                         allow_episodes=False):
        if self.season is not None and (not allow_episodes
                                        or not self.episode):
            return
        if self.trakt_api.is_sync(self.trakt_type, self.unique_id, self.season,
                                  self.episode, self.id_type, sync_type):
            return {'name': name_remove, 'method': '{}/remove'.format(method)}
        return {'name': name_add, 'method': method}

    def _sync_userlist_addlist(self):
        name = xbmcgui.Dialog().input(ADDON.getLocalizedString(32356))
        if not name:
            return
        response = self.trakt_api.post_response('users/me/lists',
                                                postdata={'name': name})
        if not response or not response.json():
            return
        return response.json().get('ids', {}).get('slug')

    def _sync_userlist_getlist(self):
        with busy_dialog():
            list_sync = self.trakt_api.get_list_of_lists(
                'users/me/lists') or []
            list_sync.append({'label': ADDON.getLocalizedString(32299)})
        x = xbmcgui.Dialog().contextmenu([i.get('label') for i in list_sync])
        if x == -1:
            return
        if list_sync[x].get('label') == ADDON.getLocalizedString(32299):
            return self._sync_userlist_addlist()
        return list_sync[x].get('params', {}).get('list_slug')

    def _sync_userlist(self, remove=False, **kwargs):
        list_slug = xbmc.getInfoLabel(
            "ListItem.Property(param.list_slug)"
        ) if remove else self._sync_userlist_getlist()
        if not list_slug:
            return
        with busy_dialog():
            return self.trakt_api.add_list_item(list_slug,
                                                self.trakt_type,
                                                self.unique_id,
                                                self.id_type,
                                                season=self.season,
                                                episode=self.episode,
                                                remove=remove)

    def _view_comments(self):
        trakt_type = 'show' if self.trakt_type in ['season', 'episode'
                                                   ] else self.trakt_type
        with busy_dialog():
            slug = self.trakt_api.get_id(self.unique_id, self.id_type,
                                         trakt_type, 'slug')
            comments = self.trakt_api.get_response_json(
                '{}s'.format(trakt_type), slug, 'comments', limit=50) or []
            itemlist = [
                i.get('comment', '').replace('\n', ' ') for i in comments
            ]
        return self._choose_comment(itemlist, comments)

    def _choose_comment(self, itemlist, comments):
        if not itemlist:
            xbmcgui.Dialog().ok(ADDON.getLocalizedString(32305),
                                ADDON.getLocalizedString(32306))
            return -1
        x = xbmcgui.Dialog().select(ADDON.getLocalizedString(32305), itemlist)
        if x == -1:
            return -1
        info = comments[x].get('comment')
        name = comments[x].get('user', {}).get('name')
        rate = comments[x].get('user_stats', {}).get('rating')
        info = u'{}\n\n{} {}/10'.format(info, xbmc.getLocalizedString(563),
                                        rate) if rate else u'{}'.format(info)
        xbmcgui.Dialog().textviewer(name, info)
        return self._choose_comment(itemlist, comments)

    def _sync_item(self, method, **kwargs):
        if method == 'userlist':
            return self._sync_userlist(**kwargs)
        if method == 'comments':
            return self._view_comments()
        with busy_dialog():
            return self.trakt_api.sync_item(method, self.trakt_type,
                                            self.unique_id, self.id_type,
                                            self.season, self.episode)

    def sync(self):
        with busy_dialog():
            choices = self._build_choices()
        x = xbmcgui.Dialog().contextmenu([i.get('name') for i in choices])
        if x == -1:
            return
        name = choices[x].get('name')
        item_sync = self._sync_item(**choices[x])
        if item_sync == -1:
            return
        if item_sync and item_sync.status_code in [200, 201, 204]:
            xbmcgui.Dialog().ok(
                ADDON.getLocalizedString(32295),
                ADDON.getLocalizedString(32297).format(name, self.trakt_type,
                                                       self.id_type.upper(),
                                                       self.unique_id))
            xbmc.executebuiltin('Container.Refresh')
            xbmc.executebuiltin(
                'UpdateLibrary(video,/fake/path/to/force/refresh/on/home)')
            return
        xbmcgui.Dialog().ok(
            ADDON.getLocalizedString(32295),
            ADDON.getLocalizedString(32296).format(name, self.trakt_type,
                                                   self.id_type.upper(),
                                                   self.unique_id))