Ver código fonte

稳定版本,可用,最基本的功能开发完成

Cloudmistery 5 meses atrás
pai
commit
75b12e2df8

+ 3 - 2
Bilibili.py

@@ -1,4 +1,5 @@
 # -*- coding: UTF-8 -*-
-from bilibili_files.utils.Clip_bili import Clip_bilibili
+from bilibili_pachong.bilibili_files.utils.Clip_bili import downloads
+
 if __name__ == '__main__':
-    Clip_bilibili()
+    downloads()

BIN
__pycache__/Bilibili_Main_Setting.cpython-39.pyc


+ 14 - 9
utils/Clip_bili.py

@@ -2,17 +2,22 @@
 # @Author  : ChenZhaoyuchen
 # @Time    : 2024/9/20 8:59
 # @File    : Clip_bili.py
-from bilibili_files.utils.setting import Audio_path,Audio_format,Synth_path,Video_format,Video_path
-from moviepy.editor import VideoFileClip, AudioFileClip
-from bilibili_files.utils.GetVideoInfo import GetVideoInfo,GetAudioInfo,GetTitile
-from bilibili_files.utils.Save_bili import Saveaudio,Savevideo
 
-def Clip_bilibili():
+from .setting import *
+from .GetVideoInfo import *
+from .Save_bili import *
+from moviepy.editor import *
+import requests
+
+def downloads():
+    # 程序参数
+    session = requests.session()
     title = GetTitile()
-    video_url = GetVideoInfo()
-    Savevideo(title, video_url)
-    audio_url = GetAudioInfo()
-    Saveaudio(title, audio_url)
+    v_url = GetVideoInfo()
+    a_url = GetAudioInfo()
+
+    # 主程序操作
+    SaveAV(session,url,title,v_url,a_url)
 
     audio_path = fr"{Audio_path}{title}{Audio_format}"
     audio_clip = AudioFileClip(audio_path)

+ 36 - 26
utils/GetResponse.py

@@ -3,44 +3,54 @@
 # @Time    : 2024/9/13 15:03
 # @File    : GetResponse.py
 
+from .setting import *
 import requests
-from setting import url,base_url
 
-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'
-}
-
-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',
+# //视频音频请求头//
+headers_bili_av = {
+    'Accept': '*/*',
+    'Accept-Language': 'zh,en-US;q=0.7,en;q=0.3',
+    'Accept-Encoding': 'gzip, deflate, br',
     'TE': 'Trailers',
+    'Range': 'bytes=0-',
+    'Origin': 'https://www.bilibili.com',
+    'Connection': 'keep-alive',
+    'DNT': '1',
+    'referer': 'https://www.bilibili.com',
+    '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'
+}
+# //准许请求头//
+headers_bili_ref = {
+    'Host': 'cn-gdgz4-cmcc-v-10.bilivideo.com',
+    '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',
+    'Accept': '*/*',
+    'Accept-Language': 'zh,en-US;q=0.7,en;q=0.3',
+    'Accept-Encoding': 'gzip, deflate, br',
     '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',
+    'Cache-Control': 'max-age=0'
+}
+
+headers = {
+    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
+    'Accept-Language': 'zh,en-US;q=0.7,en;q=0.3',
+    'Accept-Encoding': 'gzip, deflate',
+    'Connection': 'keep-alive',
     '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
+video_json = {
 
-def GetResponse_video(url):
-    response_video = requests.get(url = url,headers = headers_bili_video)
-    return response_video
+}
 
-# 测试代码
-if __name__ == '__main__':
+def GetResponse_AV():
+    response_audio = requests.get(url = url,headers = headers_bili_av)
+    return response_audio
 
-    a = GetResponse_audio(url)
-    b = GetResponse_video(url)
-    print(a)
-    print(b)
+# 检查
+if __name__ == '__main__':
+    print(GetResponse_AV())

+ 46 - 28
utils/GetVideoInfo.py

@@ -5,11 +5,14 @@
 
 import json
 import re
-from GetResponse import GetResponse_video,GetResponse_audio
-from setting import url
+from bilibili_pachong.bilibili_files.utils.GetResponse import *
+from pprint import pprint
+
+video_json = {
+}
 
 def GetVideoInfo() -> str:
-    response_video = GetResponse_video(url = url)
+    response_video = GetResponse_AV()
 
     html_video = response_video.text
 
@@ -22,11 +25,11 @@ def GetVideoInfo() -> str:
     return video_url
 
 def GetAudioInfo() -> str:
-    response_audio = GetResponse_audio(url = url)
+    response_audio = GetResponse_AV()
 
     html_audio = response_audio.text
 
-    info_audio = re.findall(r'<script>window.__playinfo__=(.*?)</script>', html_audio)[0]
+    info_audio = re.findall('<script>window.__playinfo__=(.*?)</script>', html_audio)[0]
 
     json_data_audio = json.loads(info_audio)
 
@@ -35,30 +38,45 @@ def GetAudioInfo() -> str:
     return audio_url
 
 def GetTitile():
-    response_audio = GetResponse_audio(url = url)
-
-    html_audio = response_audio.text
-
-    title = re.findall('<title data-vue-meta="true">(.*?)</title>', html_audio)
-
-    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)
+    response_audio = GetResponse_AV()
+
+    html = response_audio.text
+
+    title = re.findall('<title data-vue-meta="true">(.*?)</title>', html)[0]
+
+    print("原名为:",title)
+
+    video_json[title] = title
+
+    if not title:
+        title = '未知'
+        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
+            title = remove_illegal_chars(title)
+        else:
+            return None
+    elif len(title) > 30:
+        # 如果名字过长,就取前20对反爬虫策略有一定的反制手段,如使用代理IP、设置随机访问时间、获取ajax等个字符
+        title = title[:30]
     return title
 
-
+# 仅用作测试
+def GetHTML():
+    response = GetResponse_AV()
+    html = response.text
+    return html
 
 # # 测试代码
-# if __name__ == '__main__':
-#     A = GetAudioInfo()
-#     B = GetVideoInfo()
-#     print(A,B)
+if __name__ == '__main__':
+    A = GetAudioInfo()
+    print(A)
+    B = GetVideoInfo()
+    print(B)
+    C = GetTitile()
+    print('修饰过后名字为:',C)
+    D = GetHTML()
+    pprint(D)

+ 16 - 9
utils/Save_bili.py

@@ -3,17 +3,24 @@
 # @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_video,GetResponse_audio
+import requests
+from bilibili_pachong.bilibili_files.utils.setting import *
+from bilibili_pachong.bilibili_files.utils.GetResponse import *
 
-def Savevideo(title,video_url):
-    video_content = GetResponse_video(url = video_url).content
+def SaveAV(session: requests.session(),url,title,v_url,a_url):
+    # 设置请求头
+    headers_bili_av['Referer'] = url
+    headers_bili_ref['Referer'] = url
 
-    with open(Video_path + title + Video_format, mode='wb') as video:
-        video.write(video_content)
+    # 获取准许
+    session.options(v_url, headers=headers_bili_ref)
+    session.options(a_url, headers=headers_bili_ref)
 
-def Saveaudio(title,audio_url):
-    audio_content = GetResponse_audio(url = audio_url).content
+    A_content = session.get(a_url, headers=headers_bili_av).content
+    V_content = session.get(v_url, headers=headers_bili_av).content
 
+    with open(Video_path + title + Video_format, mode='wb') as video:
+        video.write(V_content)
     with open(Audio_path + title + Audio_format, mode='wb') as audio:
-        audio.write(audio_content)
+        audio.write(A_content)
+

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


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 - 2
utils/setting.py

@@ -10,5 +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/BV1Mt4y1i7LN'
-base_url = 'https://www.bilibili.com'
+url = 'https://www.bilibili.com/video/BV1Mz15YkEKS'
+base_url = 'https://www.bilibili.com'