def api_add_magnet(_magnet): endpoint = torrents_url + "/addMagnet" available_hosts = api_get_hosts() if not available_hosts: return "Error checking available hosts. Check logs and/or retry." data = {magnet: _magnet, host: available_hosts[0][host]} user_credentials = get_credentials() if user_credentials is None: print( "No credentials were loaded, check if the user has gone through the authentication procedure" ) return None result = make_post(endpoint, data, access_token=user_credentials[access_token]) if result.status_code != 201: return prettify_json(result.json()) # starts the torrent with all the files add_result = api_select_files(result.json()["id"]) if add_result.status_code == 204: return "Magnet Successfully Added." else: return "Issue with magnet, http status code {}.".format( add_result.status_code)
def api_unrestrict_folder(link): if link is None or len(link) < 5: return "Command syntax is `/unrestrict www.your_link.com`, please retry" endpoint = unrestrict_url + "folder" data = {"link": link} user_credentials = get_credentials() if user_credentials is None: print( "No credentials were loaded, check if the user has gone through the authentication procedure" ) return None result = make_post(endpoint, data, access_token=user_credentials[access_token]) if "error_code" in result.json(): return result.json() if not result.json(): return "The folder was empty" return result.json()
def api_select_files(_id, select="all"): endpoint = torrents_url + "/selectFiles/{}".format(_id) data = {"files": select} user_credentials = get_credentials() result = make_post(endpoint, data, access_token=user_credentials[access_token]) return result
def api_unrestrict_link(link, password=None, remote=None): if link is None or len(link) < 5: return "Command syntax is `/unrestrict www.your_link.com`, please retry" endpoint = unrestrict_url + "link" data = {"link": link} if password is not None: data["password"] = password if remote is not None: data["remote"] = remote user_credentials = get_credentials() if user_credentials is None: print( "No credentials were loaded, check if the user has gone through the authentication procedure" ) return None result = make_post(endpoint, data, access_token=user_credentials[access_token]) download = result.json() if "error_code" in download: return prettify_json(download) markdown = "" if "filename" in download: markdown += "*" + escape_markdown(download["filename"], version=2) + "*\n" if "filesize" in download: markdown += "Size: " + escape_markdown( str('%.2f' % (download["filesize"] / (1024 * 1024))), version=2) + " MB\n" if "download" in download: markdown += escape_markdown("Download Link:\n" + download["download"] + "\n", version=2) if "streamable" in download: if download["streamable"] == 1: markdown += "Streamable: Yes\n" markdown += escape_markdown("Streaming ID: " + download["id"] + "\n", version=2) else: markdown += "Streamable: No\n" if len(markdown) > 5: return markdown else: return prettify_json(download)
def refresh_current_token(): credentials = get_credentials() result = make_post(token_url, data={ client_id: credentials[client_id], client_secret: credentials[client_secret], code: credentials[refresh_token], grant_type: grant_type_oauth, }, use_headers=False) if result.status_code != 200: print("Error refreshing token, status code ", result.status_code) return False result_json = result.json() update_token(result_json[access_token], result_json[refresh_token]) return True
def get_token(my_client_id, my_client_secret, device_code, my_grant_type=grant_type_url): # Using the value of device_code, your application makes a direct POST request to the token endpoint, # with the following parameters: # # client_id: the value of client_id provided by the call to the credentials endpoint # client_secret: the value of client_secret provided by the call to the credentials endpoint # code: the value of device_code # grant_type: use the value "http://oauth.net/grant_type/device/1.0" data = { client_id: my_client_id, client_secret: my_client_secret, code: device_code, grant_type: my_grant_type } result = make_post(token_url, data=data, use_headers=False) return result
def api_unrestrict_check(link, password=None): if link is None or len(link) < 5: return "Command syntax is `/unrestrict www.your_link.com, please retry" endpoint = unrestrict_url + "check" data = {"link": link} if password is not None: data["password"] = password result = make_post(endpoint, data, use_headers=False) check = result.json() # if the token has been updated I make the call again # todo: as an alternative I call this function again and I return that # that would need other checks of course # todo: this does not use the token so thereś an error with the call # todo: decide if we should send personalized messages for cases # if result.status_code == 503: # return "File_unavailable" # if result.status_code == 200: # return prettifyJSON(result.json()) # else: # return "Error : \n" + result.json() pretty_data = prettify_json(check) if "error_code" in check: return pretty_data if check["supported"] == 1: response = "File is available for download with command /unrestrict [{}]({})\n". \ format(check["link"], check["link"]) else: response = "File not available on hoster\n" return response + pretty_data