from django.shortcuts import render from django.http import HttpResponse,HttpResponseRedirect,FileResponse from django.core.files.storage import FileSystemStorage from getAudio.download import audio_download,get_urls import os import re import time from pathlib import Path from urllib.parse import quote BASE_DIR = Path(__file__).resolve().parent.parent def index(request): return render(request, "index.html") def getBV(request): bv = request.POST['bv'] mp3 = audio_download(bv) if mp3 == "FAIL": return render(request, "index.html") src = '//www.bilibili.com/blackboard/html5mobileplayer.html?bvid=' + bv + '&high_quality=1&danmaku=1' file = mp3 bvname = re.sub('_哔哩哔哩_bilibili.mp3','',mp3) # response = HttpResponse(FileSystemStorage().open(file)) # response['Content-Type'] = 'application/octet-stream' # response['Content-Disposition'] = 'attachment;filename="{0}"'.format(quote(file)) # response['Content-Length'] = os.path.getsize(file) return render(request,"getBV.html",{"src":src,"bvname":bvname,"file":file}) # ,{"src":src,"bvname":bvname} # return HttpResponseRedirect('/') def getBVs(request): bv = request.POST['bv'] ziplist = get_urls(bv) if ziplist == "FAIL": return render(request, "index.html") # for page,name in ziplist: # print(page) # print(name) return render(request,"getBVs.html",{"ziplist":ziplist}) def download(request): file = request.GET['file'] # print(file) os.chdir(os.path.join(BASE_DIR,'getAudio/download')) response = HttpResponse(FileSystemStorage().open(file)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="{0}"'.format(quote(file)) response['Content-Length'] = os.path.getsize(file) return response def downloads(request): postdict = request.POST for key in postdict.keys(): if key == 'csrfmiddlewaretoken': continue value = postdict[key] if value != 'on': continue flag = 0 for count in range(3): try: aid =re.sub('https://www.bilibili.com/video/','',key) print(aid) file = audio_download(aid) print(file) if file != 'FAIL': flag = 1 break except Exception as e: print(e) time.sleep(0.5) else: print('ERROR!!!') print('-------------------------------------') if flag == 1: os.chdir(os.path.join(BASE_DIR,'getAudio/download')) response = HttpResponse(FileSystemStorage().open(file)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="{0}"'.format(quote(file)) response['Content-Length'] = os.path.getsize(file) return response