def authTrakt():
    try:
        if getTraktCredentialsInfo() == True:
            if control.yesnoDialog(control.lang(32251).encode('utf-8'), control.lang(32252).encode('utf-8'), '', 'Trakt'):
                control.setSetting(id='trakt.user', value='')
                control.setSetting(id='trakt.token', value='')
                control.setSetting(id='trakt.refresh', value='')
            raise Exception()

        result = getTraktAsJson('/oauth/device/code', {'client_id': V2_API_KEY})
        verification_url = (control.lang(32253) % result['verification_url']).encode('utf-8')
        user_code = (control.lang(32254) % result['user_code']).encode('utf-8')
        expires_in = int(result['expires_in'])
        device_code = result['device_code']
        interval = result['interval']
        
        progressDialog = control.progressDialog
        progressDialog.create('Trakt', verification_url, user_code)

        for i in range(0, expires_in):
            try:
                if progressDialog.iscanceled(): break
                time.sleep(1)
                if not float(i) % interval == 0: raise Exception()
                r = getTraktAsJson('/oauth/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(urlparse.urljoin(BASE_URL, '/users/me'), headers=headers)
        result = utils.json_loads_as_str(result)

        user = result['username']

        control.setSetting(id='trakt.user', value=user)
        control.setSetting(id='trakt.token', value=token)
        control.setSetting(id='trakt.refresh', value=refresh)
        raise Exception()
    except:
        control.openSettings('3.1')
Beispiel #2
0
def manager(name, tmdb, content):
    try:
        post = {
            "movies": [{
                "ids": {
                    "tmdb": tmdb
                }
            }]
        } if content == 'movie' else {
            "shows": [{
                "ids": {
                    "tmdb": tmdb
                }
            }]
        }

        items = [(control.lang(32256).encode('utf-8'), '/sync/collection')]
        items += [(control.lang(32257).encode('utf-8'),
                   '/sync/collection/remove')]
        items += [(control.lang(32258).encode('utf-8'), '/sync/watchlist')]
        items += [(control.lang(32259).encode('utf-8'),
                   '/sync/watchlist/remove')]
        items += [(control.lang(32260).encode('utf-8'),
                   '/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 range(len(lists) * 2)]
        for i in range(0, len(lists), 2):
            lists[i] = ((control.lang(32261) % lists[i][0]).encode('utf-8'),
                        '/users/me/lists/%s/items' % lists[i][1])
        for i in range(1, len(lists), 2):
            lists[i] = ((control.lang(32262) % lists[i][0]).encode('utf-8'),
                        '/users/me/lists/%s/items/remove' % lists[i][1])
        items += lists

        select = control.selectDialog([i[0] for i in items],
                                      control.lang(32255).encode('utf-8'))

        if select == -1:
            return
        elif select == 4:
            t = control.lang(32260).encode('utf-8')
            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(32255).encode('utf-8'),
                                          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(32255).encode('utf-8'),
                           heading=str(name),
                           sound=True,
                           icon=icon)
    except:
        return