def delID(parameters): refreshToken() headers = { "Authorization": "Bearer " + str(xbmcaddon.Addon('script.realdebrid').getSetting('rd_access')) } if parameters['method'] == "torrent": if xbmcgui.Dialog().yesno( "Delete torrent?", "Do you want to delete the torrent" + '\n\n' + parameters['name']): r = requests.delete( "https://api.real-debrid.com/rest/1.0/torrents/delete/" + parameters['id'], headers=headers) try: isError(json.loads(r.text)) except: xbmc.executebuiltin('Container.Refresh') else: if xbmcgui.Dialog().yesno( "Delete link?", "Do you want to delete the link" + '\n\n' + parameters['name']): util.logError( "https://api.real-debrid.com/rest/1.0/downloads/delete/" + parameters['id']) r = requests.delete( "https://api.real-debrid.com/rest/1.0/downloads/delete/" + parameters['id'], headers=headers) try: isError(json.loads(r.text)) except: xbmc.executebuiltin('Container.Refresh')
def torrentsInfo(id): if "False" not in id[1]: try: refreshToken() cj_rd = cookielib.CookieJar() opener_rd = build_opener(HTTPCookieProcessor(cj_rd)) cont = 8 while cont == 8: opener_rd.addheaders = [("Authorization", "Bearer " + str( xbmcaddon.Addon('script.realdebrid').getSetting( 'rd_access')))] resp = opener_rd.open( "https://api.real-debrid.com/rest/1.0/torrents/info/" + str(id)) content = json.loads(resp.read()) cont = isError(content) if cont: return False else: return content except urllib.HTTPError as err: if err.code == 404: util.logError(str(err)) return False return False
def hostStatus(): from collections import OrderedDict cj_rd = cookielib.CookieJar() opener_rd = build_opener(HTTPCookieProcessor(cj_rd)) opener_rd.addheaders = [ ("Authorization", "Bearer " + str(xbmcaddon.Addon('script.realdebrid').getSetting('rd_access'))) ] error = True attempts = 0 while error: try: resp = opener_rd.open( 'https://api.real-debrid.com/rest/1.0/hosts/status') content = resp.read() credJSON = json.loads(content) #util.logError(str(credJSON)) return credJSON except Exception as e: e = str(e) util.logError("hoststaus error: " + e) attempts = attempts + 1 if attempts > 3: error = True return False elif "Unauthorized" in e: refreshToken()
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()
def isError(toCheck): try: if toCheck['error']: if toCheck['error_code'] == 8: # need to refresh token refreshToken() return 8 else: util.notify( "Error " + str(toCheck['error_code']) + ": " + string.capwords(toCheck['error'].replace("_", " "))) util.logError("Error " + str(toCheck['error_code']) + ": " + toCheck['error']) return True except: return False
def addTorrent(parameters, remove=False, all=False): refreshToken() if "torrent_file" not in parameters: dialog = xbmcgui.Dialog() link = dialog.browseSingle( 1, 'Select .torrent file', 'files', '.torrent', False, False, 'special://masterprofile/script_data/Kodi Lyrics') else: link = parameters['torrent_file'] try: file = open(link, 'rb') cont = 8 while cont == 8: headers = { "Authorization": "Bearer " + str( xbmcaddon.Addon('script.realdebrid').getSetting( 'rd_access')) } r = requests.put( "https://api.real-debrid.com/rest/1.0/torrents/addTorrent", data=file, headers=headers) content = json.loads(r.text) cont = isError(content) file.close() try: if remove: os.remove(link) except: util.logError("Unable to remove file '" + link + "'") if cont: return False else: return torrentSelect(content['id'], all) except: # no torrent selected probably pass
def unrestrict(parameters): cj_rd = cookielib.CookieJar() opener_rd = build_opener(HTTPCookieProcessor(cj_rd)) opener_rd.addheaders = [ ("Authorization", "Bearer " + str(xbmcaddon.Addon('script.realdebrid').getSetting('rd_access'))) ] if 'url' in parameters: link = parameters['url'] else: link = util.searchDialog("Enter link to unrestrict") if link: data_rd = urlencode({'link': link}).encode("utf-8") error = True attempts = 0 while error: try: resp = opener_rd.open( 'https://api.real-debrid.com/rest/1.0/unrestrict/link', data_rd) content = resp.read() credJSON = json.loads(content) error = True return credJSON except Exception as e: util.logError("realdebrid error: " + str(e)) attempts = attempts + 1 if attempts > 3: error = True util.notify("Unable to unrestrict link") break elif "Unauthorized" in e: refreshToken() return False
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
elif mode == 3: rd.addTorrent(parameters) elif mode == 4: rd.addMagent(parameters) elif mode == 5: link = rd.unrestrict(parameters) if link == False: pass 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(): if host[1]['supported'] == 1: if host[1]['status'] == "down": down.append({ "title": "[COLOR red]" + host[0] + "[/COLOR]", "url": "", "mode": "", "poster": host[1]['image_big'], "icon": host[1]['image_big'],