Prechádzať zdrojové kódy

用分离请求头方法重写中,还有部分错误

Cloudmistery 7 mesiacov pred
rodič
commit
ed9d3c4118

+ 0 - 1
.gitignore

@@ -4,5 +4,4 @@ Audio
 Files
 *.mp4
 Video
-
 down

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/

+ 8 - 0
.idea/bilibili_files.iml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 48 - 0
.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,48 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <Languages>
+        <language minSize="53" name="Python" />
+      </Languages>
+    </inspection_tool>
+    <inspection_tool class="PyPep8Inspection" enabled="false" level="WEAK WARNING" enabled_by_default="false">
+      <option name="ignoredErrors">
+        <list>
+          <option value="E101" />
+          <option value="E251" />
+          <option value="E501" />
+          <option value="W292" />
+          <option value="E303" />
+          <option value="E305" />
+          <option value="E302" />
+          <option value="E231" />
+        </list>
+      </option>
+    </inspection_tool>
+    <inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <option name="ignoredErrors">
+        <list>
+          <option value="N802" />
+        </list>
+      </option>
+    </inspection_tool>
+    <inspection_tool class="PyShadowingBuiltinsInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <option name="ignoredNames">
+        <list>
+          <option value="list" />
+        </list>
+      </option>
+    </inspection_tool>
+    <inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
+      <option name="ignoredIdentifiers">
+        <list>
+          <option value="keras.layers.convolutional" />
+          <option value="keras.layers.CuDNNLSTM" />
+          <option value="float.strftime" />
+          <option value="app.db" />
+        </list>
+      </option>
+    </inspection_tool>
+  </profile>
+</component>

+ 6 - 0
.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 7 - 0
.idea/misc.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="JavaScriptSettings">
+    <option name="languageLevel" value="ES6" />
+  </component>
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/bilibili_files.iml" filepath="$PROJECT_DIR$/.idea/bilibili_files.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 8 - 6
Bilibili.py

@@ -1,13 +1,15 @@
 # -*- coding: UTF-8 -*-
-
-from .utils.setting import Audio_path,Audio_format,Synth_path,Video_format,Video_path
+from bilibili_files.utils.setting import Audio_path,Audio_format,Synth_path,Video_format,Video_path
 from moviepy.editor import VideoFileClip, AudioFileClip
-from .utils.GetVideoInfo import GetVideoInfo
-from .utils.Save_bili import Save
+from bilibili_files.utils.GetVideoInfo import GetVideoInfo,GetAudioInfo
+from bilibili_files.utils.Save_bili import Saveaudio,Savevideo
+
 
 if __name__ == '__main__':
-    title,audio_url,video_url = GetVideoInfo()
-    Save(title, audio_url, video_url)
+    title,video_url = GetVideoInfo()
+    Savevideo(title,video_url)
+    audio_url = GetAudioInfo()
+    Saveaudio(title,audio_url)
 
     # 加载视频和音频文件
     audio_path = fr"{Audio_path}{title}{Audio_format}"

+ 36 - 7
utils/GetResponse.py

@@ -4,16 +4,45 @@
 # @File    : GetResponse.py
 
 import requests
+from setting import url,base_url
 
-headers_bili = {
+headers_bili_video = {
     'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / avif, image / webp, image / apng, * / *;q = 0.8, application / signed - exchange;v = b3;q = 0.7',
     'accept - encoding': 'gzip, deflate',
     'accept - language': 'zh - CN, zh;q = 0.9, en;q = 0.8, en - GB;q = 0.7, en - US;q = 0.6',
-    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
-    'Referer': 'https://www.bilibili.com/?spm_id_from=333.337.0.0'
+    'TE': 'Trailers',
+    'Range': 'bytes=0-',
+    'Origin': base_url,
+    'Connection': 'keep-alive',
+    'DNT': '1',
+    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'
 }
 
-def GetResponse(url):
-    headers = headers_bili
-    response = requests.get(url = url,headers = headers)
-    return response
+headers_bili_audio = {
+    'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / avif, image / webp, image / apng, * / *;q = 0.8, application / signed - exchange;v = b3;q = 0.7',
+    'accept - encoding': 'gzip, deflate',
+    'accept - language': 'zh - CN, zh;q = 0.9, en;q = 0.8, en - GB;q = 0.7, en - US;q = 0.6',
+    'TE': 'Trailers',
+    'Access-Control-Request-Method': 'GET',
+    'Access-Control-Request-Headers': 'range',
+    'Referer': url,
+    'Origin': base_url,
+    'DNT': '1',
+    'Connection': 'keep-alive',
+    'Cache-Control': 'max-age=0',
+    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'
+}
+
+def GetResponse_audio(url):
+    response_audio = requests.get(url = url,headers = headers_bili_audio)
+    return response_audio
+
+def GetResponse_video(url):
+    response_video = requests.get(url = url,headers = headers_bili_video)
+    return response_video
+
+# # 测试代码
+# if __name__ == '__main__':
+#     a = GetResponse_audio(url)
+#     print(a)
+#     print(headers_bili_audio)

+ 46 - 10
utils/GetVideoInfo.py

@@ -5,20 +5,50 @@
 
 import json
 import re
-from .GetResponse import GetResponse
-from .setting import url
+from GetResponse import GetResponse_video,GetResponse_audio
+from setting import url
 
 def GetVideoInfo() -> str:
-    response = GetResponse(url = url)
-    html_audio = response.text
+    response_video = GetResponse_video(url = url)
 
-    info = re.findall('<script>window.__playinfo__=(.*?)</script>', html_audio)[0]
-    json_data = json.loads(info)
+    html_video = response_video.text
 
-    audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
-    video_url = json_data['data']['dash']['video'][0]['baseUrl']
+    info_video = re.findall('<script>window.__playinfo__=(.*?)</script>', html_video)[0]
+
+    json_data_video = json.loads(info_video)
+
+    video_url = json_data_video['data']['dash']['video'][0]['baseUrl']
+
+    title = re.findall('<title data-vue-meta="true">(.*?)</title>', html_video)
+
+    title = title[0]
+
+    if title:
+        illegal_chars = fr'<|>\/:"*?'
+        def remove_illegal_chars(title_ill):
+            for char in illegal_chars:
+                title_ill = title_ill.replace(char, "")
+            return title_ill
+    else:
+        return None
+
+    title = remove_illegal_chars(title)
+
+    return (title,video_url)
+
+def GetAudioInfo() -> str:
+    response_audio = GetResponse_audio(url = url)
+
+    html_audio = response_audio.text
+
+    info_audio = re.findall('<script>window.__playinfo__=(.*?)</script>', html_audio)[0]
+
+    json_data_audio = json.loads(info_audio)
+
+    audio_url = json_data_audio['data']['dash']['audio'][0]['baseUrl']
 
     title = re.findall('<title data-vue-meta="true">(.*?)</title>', html_audio)
+
     title = title[0]
 
     if title:
@@ -31,6 +61,12 @@ def GetVideoInfo() -> str:
         return None
 
     title = remove_illegal_chars(title)
-    print("title:",title)
 
-    return (title, audio_url, video_url)
+    return (title,audio_url)
+
+
+# # 测试代码
+# if __name__ == '__main__':
+#     A = GetAudioInfo()
+#     B = GetVideoInfo()
+#     print(A,B)

+ 10 - 8
utils/Save_bili.py

@@ -3,15 +3,17 @@
 # @Time    : 2024/9/13 15:02
 # @File    : Save_bili.py
 
-from .setting import Audio_path,Audio_format,Video_format,Video_path
-from .GetResponse import GetResponse
+from setting import Audio_path,Audio_format,Video_format,Video_path
+from GetResponse import GetResponse_video,GetResponse_audio
 
-def Save(title,audio_url,video_url):
-    audio_content = GetResponse(url = audio_url).content
-    video_content = GetResponse(url = video_url).content
+def Savevideo(title,video_url):
+    video_content = GetResponse_video(url = video_url).content
+
+    with open(Video_path + title + Video_format, mode='wb') as video:
+        video.write(video_content)
+
+def Saveaudio(title,audio_url):
+    audio_content = GetResponse_audio(url = audio_url).content
 
     with open(Audio_path + title + Audio_format, mode='wb') as audio:
         audio.write(audio_content)
-
-    with open(Video_path + title + Video_format, mode='wb') as video:
-        video.write(video_content)

BIN
utils/__pycache__/GetResponse.cpython-39.pyc


BIN
utils/__pycache__/GetVideoInfo.cpython-39.pyc


BIN
utils/__pycache__/Save_bili.cpython-39.pyc


BIN
utils/__pycache__/setting.cpython-39.pyc


+ 2 - 1
utils/setting.py

@@ -10,4 +10,5 @@ Audio_path = 'D:/pyp/bilibili_pachong/bilibili_files/Audio/'
 Video_path = 'D:/pyp/bilibili_pachong/bilibili_files/Video/'
 Synth_path = 'D:/pyp/bilibili_pachong/bilibili_files/Files/'
 
-url = 'https://www.bilibili.com/video/BV1WT421Y7XQ/?spm_id_from=333.337.search-card.all.click&vd_source=f6247aa12dae1ff1bce74ef0af381757'
+url = 'https://www.bilibili.com/video/BV1Mt4y1i7LN'
+base_url = 'https://www.bilibili.com'