Created
October 19, 2020 20:05
-
-
Save henryjfry/2c08cba86c872444abab94fc6aff8103 to your computer and use it in GitHub Desktop.
realdebrid api magnet + kodi integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
import json | |
import time | |
import sys | |
import base64 | |
kodi_credentials = b'USER:PASSWORD' | |
kodi_ip = '192.168.0.XX' | |
kodi_port = '8081' | |
kodi_encoded_credentials = base64.b64encode(kodi_credentials) | |
kodi_authorization = b'Basic ' + kodi_encoded_credentials | |
kodi_header = { 'Content-Type': 'application/json', 'Authorization': kodi_authorization } | |
kodi_url = 'http://' + kodi_ip + ':' + kodi_port + '/jsonrpc' | |
kodi_params = json.dumps({"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}) | |
kodi_response = requests.post(kodi_url, headers=kodi_header, data=kodi_params) | |
kodi_data = kodi_response.json() | |
video_play = True | |
if any('video' in str(s) for s in kodi_data.get('result')): | |
video_play = False | |
header = {'Authorization': 'Bearer ' + 'API_TOKEN'} | |
count_list = '' | |
start = 1 | |
flag_s = False | |
flag_l = False | |
flag_p = False | |
for x in sys.argv: | |
if ("-s" in x or "-l" in x) and "magnet" in sys.argv[sys.argv.index(x) + 1]: | |
print('Youve used a flag without specifying a number') | |
exit() | |
if "magnet" in x: | |
magnet_link = x | |
if "-p" in x: | |
flag_p = True | |
if "-l" in x: | |
count_list = sys.argv[sys.argv.index(x) + 1] | |
count_list = count_list.split(',') | |
#uncomment if you want to have it automatically play/playlist in kodi when you use -l switch with only one file | |
#if len(count_list) == 1: | |
#flag_p = True | |
flag_l = True | |
if "-s" in x: | |
start = int(sys.argv[sys.argv.index(x) + 1]) + 1 | |
flag_s = True | |
if flag_l == False and flag_s == False: | |
flag_s = True | |
params = {'magnet': magnet_link} | |
response = requests.post('https://api.real-debrid.com/rest/1.0/torrents/addMagnet?', headers=header, data=params) | |
data = response.json() | |
torr_id = data.get('id') | |
if torr_id == None: | |
print('No Torrent ID => API Token Missing?') | |
exit() | |
response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/info/' + torr_id, headers=header) | |
data = response.json() | |
ids = [element['id'] for element in data['files']] | |
files = [element['path'] for element in data['files']] | |
files = [x.encode('utf-8') for x in files] | |
tot_files = ids[-1] | |
response = requests.delete('https://api.real-debrid.com/rest/1.0/torrents/delete/' + torr_id, headers=header) | |
print(response) | |
files, ids = zip(*sorted(zip(files,ids))) | |
response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/activeCount', headers=header) | |
active_downloads = response.json().get('nb') | |
print('Total Active Downloads ' + str(active_downloads)) | |
if active_downloads >= 24 and count_list == '': | |
exit() | |
count = 1 | |
extensions = ['mkv', 'mp4', 'avi'] | |
for x in files: | |
if not any(s in str(x) for s in extensions): | |
#count = count + 1 | |
continue | |
if any(s in str(x) for s in extensions) and str(count) in count_list and flag_l == True: | |
params = {'magnet': magnet_link} | |
response = requests.post('https://api.real-debrid.com/rest/1.0/torrents/addMagnet?', headers=header, data=params) | |
data = response.json() | |
torr_id = data.get('id') | |
params = {'files': ids[files.index(x)]} | |
response = requests.post('https://api.real-debrid.com/rest/1.0/torrents/selectFiles/' + torr_id, headers=header, data=params) | |
print(response) | |
print(x) | |
print('file number: ' + str(count)) | |
response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/info/' + torr_id + '?', headers=header) | |
data = response.json() | |
print(data.get('links')) | |
print(data.get('progress')) | |
if data.get('status') == "downloaded": | |
link_url = data.get('links') | |
params = {'link': link_url } | |
response = requests.post('https://api.real-debrid.com/rest/1.0/unrestrict/link', headers=header, data=params) | |
#print(response.json()) | |
kodi_file = response.json().get('download') | |
if video_play == False and flag_p == True: | |
kodi_params = json.dumps({ "jsonrpc": "2.0","method":'Playlist.Add', "params": {"playlistid": 1, "item": {'file': kodi_file}}, "id": 1}) | |
video_play = False | |
if video_play == True and flag_p == True: | |
kodi_params = json.dumps({ "jsonrpc": "2.0","method":'Player.Open',"params":{"item":{'file': kodi_file}}, "id": 1}) | |
video_play = False | |
if flag_p == True: | |
kodi_response = requests.post(kodi_url, headers=kodi_header, data=kodi_params) | |
kodi_data = kodi_response.json() | |
print(kodi_data) | |
if data.get('progress') < 1: | |
response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/activeCount', headers=header) | |
active_downloads = response.json().get('nb') | |
print('Total Active Downloads ' + str(active_downloads)) | |
if active_downloads >= 24: | |
exit() | |
time.sleep(12) | |
count = count + 1 | |
continue | |
if any(s in str(x) for s in extensions) and count >= int(start) - 1 and flag_s == True: | |
params = {'magnet': magnet_link} | |
response = requests.post('https://api.real-debrid.com/rest/1.0/torrents/addMagnet?', headers=header, data=params) | |
data = response.json() | |
torr_id = data.get('id') | |
params = {'files': ids[files.index(x)]} | |
response = requests.post('https://api.real-debrid.com/rest/1.0/torrents/selectFiles/' + torr_id, headers=header, data=params) | |
print(response) | |
print(x) | |
print('file number: ' + str(count)) | |
response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/info/' + torr_id + '?', headers=header) | |
data = response.json() | |
print(data.get('links')) | |
print(data.get('progress')) | |
if data.get('status') == "downloaded": | |
link_url = data.get('links') | |
params = {'link': link_url } | |
response = requests.post('https://api.real-debrid.com/rest/1.0/unrestrict/link', headers=header, data=params) | |
print(response) | |
kodi_file = response.json().get('download') | |
print(flag_p) | |
if video_play == False and flag_p == True: | |
kodi_params = json.dumps({ "jsonrpc": "2.0","method":'Playlist.Add', "params": {"playlistid": 1, "item": {'file': kodi_file}}, "id": 1}) | |
video_play = False | |
if video_play == True and flag_p == True: | |
kodi_params = json.dumps({ "jsonrpc": "2.0","method":'Player.Open',"params":{"item":{'file': kodi_file}}, "id": 1}) | |
video_play = False | |
if flag_p == True: | |
kodi_response = requests.post(kodi_url, headers=kodi_header, data=kodi_params) | |
kodi_data = kodi_response.json() | |
print(kodi_data) | |
if data.get('progress') < 1: | |
response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/activeCount', headers=header) | |
active_downloads =response.json().get('nb') | |
print('Total Active Downloads ' + str(active_downloads)) | |
if active_downloads >= 24: | |
exit() | |
time.sleep(12) | |
count = count + 1 | |
continue | |
else: | |
count = count + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment