yt2podcast/yt2podcast/ytdownloader.py
2022-04-19 00:09:40 +01:00

86 lines
2.6 KiB
Python

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