When a button is clicked, I make an AJAX call to 'search/'. My goal is to display the details {"file_name":d['filename'],"percentage":d['_percent_str'],"speed":d['_eta_str']} in a progress bar on a webpage while downloading.
How can I retrieve the JSON response from the video_progress_hook function each time it's called by the 'progress_hooks' parameter in ydl_opts?
I need to capture this response in JavaScript.
Please provide assistance.
def search(request):
file_name="" + str(uuid.uuid1()).split('-')[0] + ".mp3"
query = request.GET.get("query")
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'}],
'outtmpl': 'media/' + file_name,
'progress_hooks':[video_progress_hook],
'quiet': False,
}
ydl = youtube_dl.YoutubeDL(ydl_opts)
ydl.download([query])
args = {'url_link': file_name}
return JsonResponse(args)
def video_progress_hook(d):
args = {}
if d['status'] == 'downloading':
args = {"file_name": d['filename'], "percentage": d['_percent_str'], "speed": d['_eta_str']}
return JsonResponse(args)