iOS-Websockets-jDownloader/api/api.py

128 lines
3.7 KiB
Python

from hashlib import new
from typing import DefaultDict
import flask, werkzeug
from flask import request, jsonify
import myjdapi
import getpass
import configparser
app = flask.Flask(__name__)
app.config["DEBUG"] = True
jd = myjdapi.Myjdapi()
jd.set_app_key("flaskapiios")
config = configparser.RawConfigParser()
config.read('config.ini')
config.sections()
Email = config['Auth']['Email']
Password = config['Auth']['Password']
def get_id(links, uuid):
for i, obj in enumerate(links):
if obj["uuid"] == uuid:
return i
@app.route('/api/devices', methods=['GET'])
def api_devices():
jd.connect(Email,Password)
jd.update_devices()
devicelist = jd.list_devices()
devicesDict = dict()
for i, obj in enumerate(devicelist):
devicesDict[obj["name"]] = obj["name"]
return jsonify(devicesDict)
@app.route('/api/linkgrabber', methods=['GET'])
def api_url():
jd.connect(Email,Password)
# Check if a URL was provided as part of the URL.
# If URL is provided, assign it to a variable.
# If no URL is provided, display an error in the browser.
if 'url' in request.args:
url = request.args['url']
print(url)
else:
return "Error: No url field provided."
if 'device' in request.args:
print("THIS IS DEVICE: ")
print(str(request.args['device']))
device = jd.get_device(str(request.args['device']))
else:
return "Error: No device provided."
device.linkgrabber.add_links([{
"autostart" : False,
"links" : url}])
return ("Tue")
@app.route('/api/links', methods=['GET'])
def api_listlinks():
jd.connect(Email,Password)
if 'device' in request.args:
device = jd.get_device(str(request.args['device']))
else:
return "Error: No device provided."
links = device.linkgrabber.query_links()
linkDict = dict()
for i, obj in enumerate(links):
linkDict[obj["name"]] = obj["uuid"]
newList = list(linkDict.items())
return dict(newList)
return jsonify(device.linkgrabber.query_links())
@app.route('/api/variants', methods=['GET'])
def api_variants():
jd.connect(Email,Password)
if 'uuid' in request.args:
uuid = int(request.args['uuid'])
else:
return "Error: no file selected"
if 'device' in request.args:
device = jd.get_device(str(request.args['device']))
else:
return "Error: No device provided."
#links = device.linkgrabber.query_links()
uuid = str(uuid).split()
varid = "MKV_VP9_1080P_30FPS_AAC_128KBIT_DASH"
varid = varid
a = device.linkgrabber.get_variants(uuid)
s = {
"id" : "MP4_H264_1080P_30FPS_AAC_128KBIT_DASH",
"data" : "{\r\n \"aBitrate\" : -1,\r\n \"height\" : 1080,\r\n \"width\" : 1920,\r\n \"fps\" : 30,\r\n \"projection\" : \"NORMAL\"\r\n}"
}
print (s)
s = str(s).split()
device.linkgrabber.set_variant(uuid, s)
return jsonify(device.linkgrabber.get_variants(uuid))
@app.route('/api/download', methods=['GET'])
def api_addownload():
jd.connect(Email,Password)
if 'uuid' in request.args:
# file = []
uuid = int(request.args['uuid'])
else:
return "Error: No file selected"
if 'device' in request.args:
device = jd.get_device(str(request.args['device']))
else:
return "Error: No device provided."
links = device.linkgrabber.query_links()
id = get_id(links, uuid)
id = str(id).split()
uuid = str(uuid).split()
device.linkgrabber.move_to_downloadlist(id, uuid)
device.linkgrabber.clear_list()
return ("Tue")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)