def getTraktAsJson(url, post=None): try: r, res_headers = __getTrakt(url, post) r = utils.json_loads_as_str(r) if 'X-Sort-By' in res_headers and 'X-Sort-How' in res_headers: r = sort_list(res_headers['X-Sort-By'], res_headers['X-Sort-How'], r) return r except: pass
def authTrakt(): try: if getTraktCredentialsInfo() == True: if control.yesnoDialog(control.lang(32511) + '[CR]' + control.lang(32512), heading='Trakt'): control.addon('plugin.video.koditvr').setSetting(id='trakt.user', value='') control.addon('plugin.video.koditvr').setSetting(id='trakt.authed', value='') control.addon('plugin.video.koditvr').setSetting(id='trakt.token', value='') control.addon('plugin.video.koditvr').setSetting(id='trakt.refresh', value='') raise Exception() result = getTraktAsJson('/koditvr/device/code', {'client_id': V2_API_KEY}) verification_url = control.lang(32513) % result['verification_url'] user_code = six.ensure_text(control.lang(32514) % result['user_code']) expires_in = int(result['expires_in']) device_code = result['device_code'] interval = result['interval'] progressDialog = control.progressDialog progressDialog.create('Trakt') for i in list(range(0, expires_in)): try: percent = int(100 * float(i) / int(expires_in)) progressDialog.update(max(1, percent), verification_url + '[CR]' + user_code) if progressDialog.iscanceled(): break time.sleep(1) if not float(i) % interval == 0: raise Exception() r = getTraktAsJson('/koditvr/device/token', {'client_id': V2_API_KEY, 'client_secret': CLIENT_SECRET, 'code': device_code}) if 'access_token' in r: break except: pass try: progressDialog.close() except: pass token, refresh = r['access_token'], r['refresh_token'] headers = {'Content-Type': 'application/json', 'trakt-api-key': V2_API_KEY, 'trakt-api-version': 2, 'Authorization': 'Bearer %s' % token} result = client.request(urllib_parse.urljoin(BASE_URL, '/users/me'), headers=headers) result = utils.json_loads_as_str(result) user = result['username'] authed = '' if user == '' else str('yes') print('info - ' + token) control.addon('plugin.video.koditvr').setSetting(id='trakt.user', value=user) control.addon('plugin.video.koditvr').setSetting(id='trakt.authed', value=authed) control.addon('plugin.video.koditvr').setSetting(id='trakt.token', value=token) control.addon('plugin.video.koditvr').setSetting(id='trakt.refresh', value=refresh) raise Exception() except: control.openSettings('4.1')
def getAlternativTitle(title): try: t = cleantitle.get(title) r = _getAniList('/anime/search/%s' % title) r = [(i.get('title_romaji'), i.get('synonyms', [])) for i in utils.json_loads_as_str(r) if cleantitle.get(i.get('title_english', '')) == t] r = [i[1][0] if i[0] == title and len(i[1]) > 0 else i[0] for i in r] r = [i for i in r if i if i != title][0] return r except: pass
def __getTrakt(url, post=None): try: url = urllib_parse.urljoin(BASE_URL, url) post = json.dumps(post) if post else None headers = {'Content-Type': 'application/json', 'trakt-api-key': V2_API_KEY, 'trakt-api-version': 2} if getTraktCredentialsInfo(): headers.update({'Authorization': 'Bearer %s' % control.addon('plugin.video.koditvr').getSetting('trakt.token')}) result = client.request(url, post=post, headers=headers, output='extended', error=True) result = utils.byteify(result) resp_code = result[1] resp_header = result[2] result = result[0] if resp_code in ['423', '500', '502', '503', '504', '520', '521', '522', '524']: log_utils.log('Trakt Error: %s' % str(resp_code)) control.infoDialog('Trakt Error: ' + str(resp_code), sound=True) return elif resp_code in ['429']: log_utils.log('Trakt Rate Limit Reached: %s' % str(resp_code)) control.infoDialog('Trakt Rate Limit Reached: ' + str(resp_code), sound=True) return elif resp_code in ['404']: log_utils.log('Object Not Found : %s' % str(resp_code)) return if resp_code not in ['401', '405']: return result, resp_header koditvr = urllib_parse.urljoin(BASE_URL, '/koditvr/token') opost = {'client_id': V2_API_KEY, 'client_secret': CLIENT_SECRET, 'redirect_uri': REDIRECT_URI, 'grant_type': 'refresh_token', 'refresh_token': control.addon('plugin.video.koditvr').getSetting('trakt.refresh')} result = client.request(koditvr, post=json.dumps(opost), headers=headers) result = utils.json_loads_as_str(result) token, refresh = result['access_token'], result['refresh_token'] print('Info - ' + str(token)) control.addon('plugin.video.koditvr').setSetting(id='trakt.token', value=token) control.addon('plugin.video.koditvr').setSetting(id='trakt.refresh', value=refresh) headers['Authorization'] = 'Bearer %s' % token result = client.request(url, post=post, headers=headers, output='extended', error=True) result = utils.byteify(result) return result[0], result[2] except: log_utils.log('getTrakt Error', 1) pass
def _getToken(): result = urllib_parse.urlencode({ 'grant_type': 'client_credentials', 'client_id': 'kodiexodus-7erse', 'client_secret': 'XelwkDEccpHX2uO8NpqIjVf6zeg' }) result = client.request( 'https://anilist.co/api/auth/access_token', post=result, headers={'Content-Type': 'application/x-www-form-urlencoded'}, error=True) result = utils.json_loads_as_str(result) return result['token_type'], result['access_token']
def manager(name, imdb, tmdb, content): try: post = {"movies": [{"ids": {"imdb": imdb}}]} if content == 'movie' else {"shows": [{"ids": {"tmdb": tmdb}}]} items = [(control.lang(32516), '/sync/collection')] items += [(control.lang(32517), '/sync/collection/remove')] items += [(control.lang(32518), '/sync/watchlist')] items += [(control.lang(32519), '/sync/watchlist/remove')] items += [(control.lang(32520), '/users/me/lists/%s/items')] result = getTraktAsJson('/users/me/lists') lists = [(i['name'], i['ids']['slug']) for i in result] lists = [lists[i//2] for i in list(range(len(lists)*2))] for i in list(range(0, len(lists), 2)): lists[i] = ((six.ensure_str(control.lang(32521) % lists[i][0])), '/users/me/lists/%s/items' % lists[i][1]) for i in list(range(1, len(lists), 2)): lists[i] = ((six.ensure_str(control.lang(32522) % lists[i][0])), '/users/me/lists/%s/items/remove' % lists[i][1]) items += lists select = control.selectDialog([i[0] for i in items], control.lang(32515)) if select == -1: return elif select == 4: t = control.lang(32520) k = control.keyboard('', t) ; k.doModal() new = k.getText() if k.isConfirmed() else None if (new == None or new == ''): return result = __getTrakt('/users/me/lists', post={"name": new, "privacy": "private"})[0] try: slug = utils.json_loads_as_str(result)['ids']['slug'] except: return control.infoDialog(control.lang(32515), heading=str(name), sound=True, icon='ERROR') result = __getTrakt(items[select][1] % slug, post=post)[0] else: result = __getTrakt(items[select][1], post=post)[0] icon = control.infoLabel('ListItem.Icon') if not result == None else 'ERROR' control.infoDialog(control.lang(32515), heading=str(name), sound=True, icon=icon) except: return