forked from here200/CCTalk-Video-Download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
142 lines (118 loc) · 5.02 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import hashlib
import json
import os
from concurrent.futures import ThreadPoolExecutor
import requests
from tqdm import tqdm
def transform_password(password):
md5_hash = hashlib.md5()
md5_hash.update(password.encode("utf-8"))
hashed_password = md5_hash.hexdigest()
return hashed_password
def transform_phone(phone):
result = ""
for num in phone:
char = chr(ord("C") + int(num)) # C的ASCII值是67
result += f"01{char}"
return result
def get_cookie(phone, password):
phone = transform_phone(phone)
password = transform_password(password)
url = "https://pass.cctalk.com/Handler/UCenter"
params = {"action": "Login", "encrypt": 1, "hc": phone, "password": password}
response = requests.get(url, params=params, timeout=5)
return response.json()["Data"]["Cookie"]
def get_group_list(cookie):
headers = {
"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Cookie": f"ClubAuth={cookie}",
}
url = "https://m.cctalk.com/webapi/content/v1.1/user/my_group_list"
response = requests.get(url, headers=headers, timeout=5)
data = response.json()
group_list = []
for group in data["data"]["items"]:
group_id = group["groupId"]
group_name = group["groupName"]
group_dict = {"id": group_id, "name": group_name}
group_list.append(group_dict)
return group_list
def get_series_list(cookie, group_id):
headers = {
"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Cookie": f"ClubAuth={cookie}",
}
url = f"https://www.cctalk.com/webapi/content/v1.2/series/group/{group_id}/series"
params = {"limit": 10, "start": 0}
response = requests.get(url, headers=headers, params=params, timeout=5)
data = response.json()
series_list = []
for series in data["data"]["items"]:
series_id = series["seriesId"]
series_name = series["seriesName"]
series_dict = {"id": series_id, "name": series_name}
series_list.append(series_dict)
return series_list
def get_movie_list(cookie, series_id):
headers = {
"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Cookie": f"ClubAuth={cookie}",
}
url = "https://www.cctalk.com/webapi/content/v1.2/series/all_lesson_list"
params = {"seriesId": series_id}
response = requests.get(url, headers=headers, params=params, timeout=5)
data = response.json()
movie_list = []
for movie in data["data"]["items"]:
movie_id = movie["videoInfo"]["videoId"]
movie_name = movie["videoInfo"]["videoName"]
movie_dict = {"id": movie_id, "name": movie_name}
movie_list.append(movie_dict)
return movie_list
def get_download_url(cookie, movie_id):
params = {"videoId": str(movie_id)}
headers = {
"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Cookie": f"ClubAuth={cookie}",
}
url = "https://www.cctalk.com/webapi/content/v1.1/video/detail"
response = requests.get(url, headers=headers, params=params, timeout=5)
return response.json()["data"]["videoUrl"]
def download_movies(cookie, movie_list, series_name):
for index, movie in enumerate(movie_list):
download_url = get_download_url(cookie, movie["id"])
print(
f"Index: {index}, ID: {movie['id']}, Name: {movie['name']}, Link: {download_url}"
)
response = requests.get(download_url, stream=True, timeout=300)
total_size = int(response.headers.get("content-length", 0)) # 获取文件总大小
block_size = 1024 # 每次读取的块大小(1KB)
os.makedirs(f"./{series_name}/", exist_ok=True)
with open(f"./{series_name}/{index + 1}. {movie["name"]}.mp4", "wb") as f, tqdm(
desc=movie["name"],
total=total_size,
unit="iB",
unit_scale=True,
unit_divisor=1024,
) as progress_bar:
for data in response.iter_content(block_size):
f.write(data)
progress_bar.update(len(data)) # 更新进度条
print(f"{movie['name']}.mp4下载完成")
if __name__ == "__main__":
with open("config.json", "r", encoding="utf8") as file:
config = json.load(file)
cookie = get_cookie(config["phone"], config["password"])
group_list = get_group_list(cookie)
for index, group in enumerate(group_list):
print(f"Index: {index}, ID: {group['id']}, Name: {group['name']}")
lesson_index = int(input("选择课程: "))
group_id = group_list[lesson_index]["id"]
series_list = get_series_list(cookie, group_id)
for index, series in enumerate(series_list):
print(f"Index: {index}, ID: {series['id']}, Name: {series['name']}")
series_index = int(input("选择课程: "))
series_id = series_list[series_index]["id"]
series_name = series_list[series_index]["name"]
movie_list = get_movie_list(cookie, series_id)
download_movies(cookie, movie_list, series_name)