|
@@ -1,6 +1,7 @@
|
|
|
"""
|
|
|
@author: luojunhui
|
|
|
"""
|
|
|
+
|
|
|
import os
|
|
|
import re
|
|
|
import html
|
|
@@ -14,10 +15,7 @@ from fake_useragent import FakeUserAgent
|
|
|
from applications.utils.common import str_to_md5
|
|
|
from config import decrypt_key_path
|
|
|
|
|
|
-headers = {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- 'User-Agent': FakeUserAgent().chrome
|
|
|
-}
|
|
|
+headers = {"Content-Type": "application/json", "User-Agent": FakeUserAgent().chrome}
|
|
|
|
|
|
|
|
|
def extract_video_url_from_article(article_url):
|
|
@@ -27,7 +25,7 @@ def extract_video_url_from_article(article_url):
|
|
|
"""
|
|
|
response = requests.get(
|
|
|
url=article_url,
|
|
|
- headers={'User-Agent': FakeUserAgent().random},
|
|
|
+ headers={"User-Agent": FakeUserAgent().random},
|
|
|
)
|
|
|
html_text = response.text
|
|
|
w = re.search(
|
|
@@ -53,18 +51,18 @@ def download_gzh_video(article_url):
|
|
|
return
|
|
|
save_path = "static/{}.mp4".format(str_to_md5(video_url))
|
|
|
headers = {
|
|
|
- 'Accept': '*/*',
|
|
|
- 'Accept-Language': 'zh,zh-CN;q=0.9',
|
|
|
- 'Connection': 'keep-alive',
|
|
|
- 'Origin': 'https://mp.weixin.qq.com',
|
|
|
- 'Referer': 'https://mp.weixin.qq.com/',
|
|
|
- 'Sec-Fetch-Dest': 'video',
|
|
|
- 'Sec-Fetch-Mode': 'cors',
|
|
|
- 'Sec-Fetch-Site': 'cross-site',
|
|
|
- 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
|
|
- 'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
|
|
- 'sec-ch-ua-mobile': '?0',
|
|
|
- 'sec-ch-ua-platform': '"macOS"'
|
|
|
+ "Accept": "*/*",
|
|
|
+ "Accept-Language": "zh,zh-CN;q=0.9",
|
|
|
+ "Connection": "keep-alive",
|
|
|
+ "Origin": "https://mp.weixin.qq.com",
|
|
|
+ "Referer": "https://mp.weixin.qq.com/",
|
|
|
+ "Sec-Fetch-Dest": "video",
|
|
|
+ "Sec-Fetch-Mode": "cors",
|
|
|
+ "Sec-Fetch-Site": "cross-site",
|
|
|
+ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
|
|
|
+ "sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
|
|
+ "sec-ch-ua-mobile": "?0",
|
|
|
+ "sec-ch-ua-platform": '"macOS"',
|
|
|
}
|
|
|
res = requests.get(video_url, headers=headers)
|
|
|
with open(save_path, "wb") as f:
|
|
@@ -89,7 +87,7 @@ def download_sph_video(download_url, key):
|
|
|
with requests.get(download_url, headers=headers, stream=True) as response:
|
|
|
response.raise_for_status()
|
|
|
|
|
|
- with open(encrypted_path, 'wb') as f:
|
|
|
+ with open(encrypted_path, "wb") as f:
|
|
|
for chunk in response.iter_content(chunk_size=8192):
|
|
|
if chunk: # filter out keep-alive chunks
|
|
|
f.write(chunk)
|
|
@@ -109,7 +107,7 @@ def download_sph_video(download_url, key):
|
|
|
raise RuntimeError(f"Video processing failed: {str(e)}") from e
|
|
|
|
|
|
|
|
|
-def decrypt_sph_video(video_path: str, key: int, save_path: str) -> None:
|
|
|
+def decrypt_sph_video(video_path: str, key: int, save_path: str) -> None:
|
|
|
"""
|
|
|
Decrypt video file using C library.
|
|
|
Args:
|
|
@@ -124,19 +122,20 @@ def decrypt_sph_video(video_path: str, key: int, save_path: str) -> None:
|
|
|
|
|
|
try:
|
|
|
lib = ffi.dlopen(decrypt_key_path)
|
|
|
- ffi.cdef('void decrypt(unsigned char *data, const size_t data_length, const uint32_t key);')
|
|
|
+ ffi.cdef(
|
|
|
+ "void decrypt(unsigned char *data, const size_t data_length, const uint32_t key);"
|
|
|
+ )
|
|
|
|
|
|
- with open(video_path, 'rb') as f:
|
|
|
+ with open(video_path, "rb") as f:
|
|
|
encrypted_data = f.read()
|
|
|
|
|
|
- c_data = ffi.new('unsigned char[]', list(encrypted_data))
|
|
|
- lib.decrypt(c_data, 2 ** 17, int(key))
|
|
|
+ c_data = ffi.new("unsigned char[]", list(encrypted_data))
|
|
|
+ lib.decrypt(c_data, 2**17, int(key))
|
|
|
decrypted_data = bytes(ffi.buffer(c_data, len(encrypted_data))[:])
|
|
|
|
|
|
- with open(save_path, 'wb') as f:
|
|
|
+ with open(save_path, "wb") as f:
|
|
|
f.write(decrypted_data)
|
|
|
|
|
|
except Exception as e:
|
|
|
print(traceback.format_exc())
|
|
|
raise RuntimeError(f"Decryption failed: {str(e)}") from e
|
|
|
-
|