views.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from django.shortcuts import render
  2. from django.http import HttpResponse,HttpResponseRedirect,FileResponse
  3. from django.core.files.storage import FileSystemStorage
  4. from getAudio.download import audio_download,get_urls
  5. import os
  6. import re
  7. import time
  8. from pathlib import Path
  9. from urllib.parse import quote
  10. BASE_DIR = Path(__file__).resolve().parent.parent
  11. def index(request):
  12. return render(request, "index.html")
  13. def getBV(request):
  14. bv = request.POST['bv']
  15. mp3 = audio_download(bv)
  16. if mp3 == "FAIL":
  17. return render(request, "index.html")
  18. src = '//www.bilibili.com/blackboard/html5mobileplayer.html?bvid=' + bv + '&high_quality=1&danmaku=1'
  19. file = mp3
  20. bvname = re.sub('_哔哩哔哩_bilibili.mp3','',mp3)
  21. # response = HttpResponse(FileSystemStorage().open(file))
  22. # response['Content-Type'] = 'application/octet-stream'
  23. # response['Content-Disposition'] = 'attachment;filename="{0}"'.format(quote(file))
  24. # response['Content-Length'] = os.path.getsize(file)
  25. return render(request,"getBV.html",{"src":src,"bvname":bvname,"file":file})
  26. # ,{"src":src,"bvname":bvname}
  27. # return HttpResponseRedirect('/')
  28. def getBVs(request):
  29. bv = request.POST['bv']
  30. ziplist = get_urls(bv)
  31. if ziplist == "FAIL":
  32. return render(request, "index.html")
  33. # for page,name in ziplist:
  34. # print(page)
  35. # print(name)
  36. return render(request,"getBVs.html",{"ziplist":ziplist})
  37. def download(request):
  38. file = request.GET['file']
  39. # print(file)
  40. os.chdir(os.path.join(BASE_DIR,'getAudio/download'))
  41. response = HttpResponse(FileSystemStorage().open(file))
  42. response['Content-Type'] = 'application/octet-stream'
  43. response['Content-Disposition'] = 'attachment;filename="{0}"'.format(quote(file))
  44. response['Content-Length'] = os.path.getsize(file)
  45. return response
  46. def downloads(request):
  47. postdict = request.POST
  48. for key in postdict.keys():
  49. if key == 'csrfmiddlewaretoken':
  50. continue
  51. value = postdict[key]
  52. if value != 'on':
  53. continue
  54. flag = 0
  55. for count in range(3):
  56. try:
  57. aid =re.sub('https://www.bilibili.com/video/','',key)
  58. print(aid)
  59. file = audio_download(aid)
  60. print(file)
  61. if file != 'FAIL':
  62. flag = 1
  63. break
  64. except Exception as e:
  65. print(e)
  66. time.sleep(0.5)
  67. else:
  68. print('ERROR!!!')
  69. print('-------------------------------------')
  70. if flag == 1:
  71. os.chdir(os.path.join(BASE_DIR,'getAudio/download'))
  72. response = HttpResponse(FileSystemStorage().open(file))
  73. response['Content-Type'] = 'application/octet-stream'
  74. response['Content-Disposition'] = 'attachment;filename="{0}"'.format(quote(file))
  75. response['Content-Length'] = os.path.getsize(file)
  76. return response