GetVideoInfo.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # @Author : ChenZhaoyuchen
  3. # @Time : 2024/9/13 15:02
  4. # @File : GetVideoInfo.py
  5. import json
  6. import re
  7. from GetResponse import GetResponse_video,GetResponse_audio
  8. from setting import url
  9. def GetVideoInfo() -> str:
  10. response_video = GetResponse_video(url = url)
  11. html_video = response_video.text
  12. info_video = re.findall('<script>window.__playinfo__=(.*?)</script>', html_video)[0]
  13. json_data_video = json.loads(info_video)
  14. video_url = json_data_video['data']['dash']['video'][0]['baseUrl']
  15. title = re.findall('<title data-vue-meta="true">(.*?)</title>', html_video)
  16. title = title[0]
  17. if title:
  18. illegal_chars = fr'<|>\/:"*?'
  19. def remove_illegal_chars(title_ill):
  20. for char in illegal_chars:
  21. title_ill = title_ill.replace(char, "")
  22. return title_ill
  23. else:
  24. return None
  25. title = remove_illegal_chars(title)
  26. return (title,video_url)
  27. def GetAudioInfo() -> str:
  28. response_audio = GetResponse_audio(url = url)
  29. html_audio = response_audio.text
  30. info_audio = re.findall('<script>window.__playinfo__=(.*?)</script>', html_audio)[0]
  31. json_data_audio = json.loads(info_audio)
  32. audio_url = json_data_audio['data']['dash']['audio'][0]['baseUrl']
  33. title = re.findall('<title data-vue-meta="true">(.*?)</title>', html_audio)
  34. title = title[0]
  35. if title:
  36. illegal_chars = fr'<|>\/:"*?'
  37. def remove_illegal_chars(title_ill):
  38. for char in illegal_chars:
  39. title_ill = title_ill.replace(char, "")
  40. return title_ill
  41. else:
  42. return None
  43. title = remove_illegal_chars(title)
  44. return (title,audio_url)
  45. # # 测试代码
  46. # if __name__ == '__main__':
  47. # A = GetAudioInfo()
  48. # B = GetVideoInfo()
  49. # print(A,B)