diff --git a/README.md b/README.md index 844a2b1..25c256f 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,13 @@ - upload downloaded file via PodcastGenerator - https://podcasts.swaghausen.de/admin/episodes_upload.php - PHP SessId - - OR script to Edit Feed.xml and create xml assoicated with mp3 + - OR script to Edit Feed.xml and create xml associated with mp3 admin test123 +## youtube dl api +https://youtubedl-material.stoplight.io/docs/youtubedl-material/YXBpOjE2NDIyMjY-youtube-dl-material-api-docs-official + +1fea29eb-bc17-4101-9c7e-75a3d27f08e1 + diff --git a/rest_server.py b/rest_server.py index b8bc665..e4bded9 100644 --- a/rest_server.py +++ b/rest_server.py @@ -11,6 +11,7 @@ from werkzeug.security import check_password_hash # from werkzeug.utils import redirect import config_parser +from yt2podcast.ytdownloader import call_youtubedl app = flask.Flask(__name__) auth = HTTPBasicAuth() @@ -41,6 +42,7 @@ def handle_yt_url(): user_input = request.form['ytlink'] # return redirect(url_for("return_result", ytlink=user_input)) print(user_input) + call_youtubedl(user_input) return render_template("yt.html") else: return render_template("yt.html") diff --git a/yt2podcast/__init__.py b/yt2podcast/__init__.py new file mode 100644 index 0000000..a68927d --- /dev/null +++ b/yt2podcast/__init__.py @@ -0,0 +1 @@ +__version__ = "0.1.0" \ No newline at end of file diff --git a/yt2podcast/ytdownloader.py b/yt2podcast/ytdownloader.py new file mode 100644 index 0000000..524bd12 --- /dev/null +++ b/yt2podcast/ytdownloader.py @@ -0,0 +1,85 @@ +import requests +import json +import time +from threading import Thread + +# def threaded_function(arg): +# for i in range(arg): +# print("running") +# time.sleep(1) + +INTERVAL_TO_CHECK=10 # seconds + +def check_status_until_done(download_uid): + finished = False + while finished is False: + time.sleep(INTERVAL_TO_CHECK) + current_status = check_status(download_uid) + isFinished = current_status['download']['finished'] + percentFinished = current_status['download']['percent_complete'] + print("percentFinished: " + percentFinished) + if isFinished == "true": + finished = True + + +# https://youtubedl-material.stoplight.io/docs/youtubedl-material/b3A6MjI5MDQ2NDQ-download-video-file +def call_youtubedl(url): + # youtube_dl_url_template = "http://odroid.fritz.box:8998/api/downloadFile?apiKey=1fea29eb-bc17-4101-9c7e-75a3d27f08e1" + # requests.post(youtube_dl_url_template) + # pass + download_uid = start_download(url) + print("download_uid: " + download_uid) + # finished = False + thread = Thread(target=check_status_until_done, args=(download_uid, )) + thread.start() + thread.join() + print("thread finished...exiting") + + # current_status = check_status(download_uid) + # if current_status is "false": + # # do in thread + # # wait(1000) + # time.sleep(2) + # thread = Thread(target = threaded_function, args = (10, )) + + +def start_download(url_to_download): + youtube_dl_url = "http://odroid.fritz.box:8998/api/downloadFile?apiKey=1fea29eb-bc17-4101-9c7e-75a3d27f08e1" + + payload = json.dumps({ + # "url": "https://www.youtube.com/watch?v=hvPmRYAmbFU", + "url": url_to_download, + "type": "audio" + }) + headers = { + 'Content-Type': 'application/json' + } + + response = requests.request("POST", youtube_dl_url, headers=headers, data=payload) + + print(response.text) + print(response.status_code) + response_dict = json.loads(response.text) + return response_dict['download']['uid'] + + +def check_status(download_uid): + # check status + url = "http://odroid.fritz.box:8998/api/downloadFile?apiKey=1fea29eb-bc17-4101-9c7e-75a3d27f08e1" + + payload = json.dumps({ + # "download_uid": "eb76815c-5a07-412c-a67f-c84b058c0156" + "download_uid": download_uid + }) + headers = { + 'Content-Type': 'application/json' + } + + response = requests.request("POST", url, headers=headers, data=payload) + + print(response.text) + response_dict = json.loads(response.text) + + # check if done + # "finished": false, + return response_dict