Exemple #1
0
def refreshToken():
    cj_rd = cookielib.CookieJar()
    opener_rd = build_opener(HTTPCookieProcessor(cj_rd))
    data_rd = urlencode({
        'client_id':
        xbmcaddon.Addon('script.realdebrid').getSetting('rd_id'),
        'client_secret':
        xbmcaddon.Addon('script.realdebrid').getSetting('rd_secret'),
        'code':
        xbmcaddon.Addon('script.realdebrid').getSetting('rd_refresh'),
        'grant_type':
        'http://oauth.net/grant_type/device/1.0'
    }).encode("utf-8")

    try:
        resp = opener_rd.open('https://api.real-debrid.com/oauth/v2/token',
                              data_rd)
        content = resp.read()

        credJSON = json.loads(content)

        xbmcaddon.Addon('script.realdebrid').setSetting(
            'rd_access', credJSON['access_token'])
        xbmcaddon.Addon('script.realdebrid').setSetting(
            'rd_refresh', credJSON['refresh_token'])

        #util.logError("write complete: "+str(credJSON))
        #util.logError("checking values"+xbmcaddon.Addon('script.realdebrid').getSetting('rd_access')+" "+xbmcaddon.Addon('script.realdebrid').getSetting('rd_refresh'))

        authorised = True
    except Exception as e:
        util.logError("Error Refreshing Token: " + str(e))
        util.alert("Your RealDebrid account needs re-authorising")
        auth()
Exemple #2
0
def download(parameters, dest=addon.getSetting('download_path')):
    if parameters['method'] == "downloads":
        lib.simpledownloader.download(parameters['name'], parameters['poster'],
                                      parameters['url'], dest)
    else:
        link = unrestrict({"url": parameters['url']})
        if not link:
            util.alert("There was an error downloading your file")
        else:
            lib.simpledownloader.download(parameters['name'],
                                          parameters['poster'],
                                          link['download'], dest)
Exemple #3
0
def torrentsDelete(id):
    refreshToken()
    headers = {
        "Authorization":
        "Bearer " +
        str(xbmcaddon.Addon('script.realdebrid').getSetting('rd_access'))
    }
    r = requests.delete(
        "https://api.real-debrid.com/rest/1.0/torrents/delete/" + str(id),
        headers=headers)

    if r.status_code == 404:
        util.alert("Unable to delete torrent, permission denied.")
        return False
    elif r.status_code == 403:
        util.alert("Unable to delete torrent, torrent not found.")
        return False
    elif r.status_code == 401:
        util.alert("Unable to delete torrent.")
        return False

    return True
Exemple #4
0
def verifyThread(authData):
    xbmc.executebuiltin('Dialog.Close(10138)')
    # convert string to JSON
    authJSON = json.loads(authData)

    # create dialog with progress to show information
    authMsg = "To authorise your RealDebrid account, use a browser to browse to [B]" + authJSON[
        'verification_url'] + "[/B] and enter the verification code [B]" + authJSON[
            'user_code'] + "[/B]"
    authDialog = util.progressStart("RealDebrid Authentication", authMsg)

    authorised = False
    timer = 0
    credJSON = ""
    while not authorised:
        time.sleep(2)
        timer = timer + 2

        util.progressUpdate(authDialog, timer, authMsg)
        # check if we need to exit
        if util.progressCancelled(authDialog) == True:
            util.progressStop(authDialog)
            break
        if timer == 100:
            util.progressStop(authDialog)
            util.alert(
                "RealDebrid aithentication has timed out. Please try again.")
            break

        # all good to carry on lets check auth
        credentials = util.getURL(
            "https://api.real-debrid.com/oauth/v2/device/credentials?client_id="
            + client_id + "&code=" + authJSON['device_code'])

        if credentials != False:
            try:
                if "error" in credentials:
                    util.logError(credentials)
                else:
                    credJSON = json.loads(credentials)
                    #store credentials in settings
                    xbmcaddon.Addon('script.realdebrid').setSetting(
                        'rd_id', credJSON['client_id'])
                    xbmcaddon.Addon('script.realdebrid').setSetting(
                        'rd_secret', credJSON['client_secret'])

                    cj_rd = cookielib.CookieJar()
                    opener_rd = build_opener(HTTPCookieProcessor(cj_rd))

                    data_rd = urlencode({
                        'client_id':
                        credJSON['client_id'],
                        'client_secret':
                        credJSON['client_secret'],
                        'code':
                        authJSON['device_code'],
                        'grant_type':
                        'http://oauth.net/grant_type/device/1.0'
                    }).encode("utf-8")

                    try:
                        #util.logError(str(data_rd))

                        resp = opener_rd.open(
                            'https://api.real-debrid.com/oauth/v2/token',
                            data_rd)
                        content = resp.read()

                        credJSON = json.loads(content)

                        xbmcaddon.Addon('script.realdebrid').setSetting(
                            'rd_access', credJSON['access_token'])
                        xbmcaddon.Addon('script.realdebrid').setSetting(
                            'rd_refresh', credJSON['refresh_token'])

                        authorised = True
                    except Exception as e:
                        util.logError(str(e))
            except Exception as e:
                util.logError(str(e))
    # check how we exited loop
    util.progressStop(authDialog)
    if authorised == True:
        util.alert("RealDebrid authenticated.")
        return True
    else:
        util.alert("There was an error authenticating with RealDebrid")
        return False
    pass

# check to see if realdebrid has been setup, if not we dont want to go any further
if rd.checkDetails():
    if mode == 1:
        rd.torrents(parameters)
    elif mode == 2:
        rd.downloads(parameters)
    elif mode == 3:
        rd.addTorrent(parameters)
    elif mode == 4:
        rd.addMagent(parameters)
    elif mode == 5:
        link = rd.unrestrict(parameters)
        if link == False:
            util.alert("Unable to unrestrict link")
        else:
            if link['streamable'] == 1:
                util.playMedia(link['filename'],
                               link['host_icon'],
                               link['download'],
                               force=True)
            else:
                util.logError(str(link))
    elif mode == 6:
        rd.torrentDisplay(parameters)
    elif mode == 7:
        hosts = rd.hostStatus()
        up = []
        down = []
        for host in hosts.iteritems():