forked from iOSDevLog/GeekTime
-
Notifications
You must be signed in to change notification settings - Fork 7
/
geek_time.py
executable file
·215 lines (173 loc) · 6.22 KB
/
geek_time.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import os
import regex as re
import browsercookie
mobile_headers = {
'Cookie': '----you-cookie---',
'Accept-Language': "zh-cn",
'Accept-Encoding': "br, gzip, deflate",
'Content-Type': "application/json",
'Content-Length': "48",
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
'X-GEEK-OS-PLATFORM': "iOS",
'Referer': "http://www.geekbang.org/",
'X-GEEK-OS-NAME': "iOS",
'Connection': "keep-alive",
'Cache-Control': "no-cache",
}
# 专栏
my_column_url = "https://time.geekbang.org/serv/v1/my/columns"
# 视频专栏
video_url = "https://time.geekbang.org/serv/v1/column/articles"
# 音频专栏
audio_url = "https://time.geekbang.org/serv/v1/column/audios"
# 所有专栏
all_column_url = "https://time.geekbang.org/serv/v1/columns"
# 获取 Chrome cookie
cj = browsercookie.chrome()
JSON_PATH = os.path.join(os.getcwd(), 'json/')
AUDIO_PATH = os.path.join(os.getcwd(), 'audio/')
VIDEO_PATH = os.path.join(os.getcwd(), 'video/')
# mkdir
def make_dir(path):
try:
os.mkdir(path)
except:
pass
make_dir(JSON_PATH)
make_dir(VIDEO_PATH)
make_dir(AUDIO_PATH)
# 所有专栏
def fetch_all_column():
payload = "{}"
response = requests.request("POST", all_column_url, data=payload, cookies=cj, headers=mobile_headers)
json_data = json.loads(response.content.decode('utf-8'))
print(json_data)
data = json_data.get('code')
if data != 0:
return
list = json_data.get('data').get('list')
my_column = []
for item in list:
cid = item.get('id')
column_unit = item.get('column_unit')
column_title = item.get('column_title')
had_sub = item.get('had_sub')
is_include_audio = item.get('is_include_audio')
message = "id = %s has_audio = %s column_unit = %s column_title = %s" % (cid, is_include_audio, column_unit, column_title)
print(my_column)
if had_sub:
my_column.append(message)
print("-" * 40)
print("had sub:")
print(my_column)
def column_type_string(column_type):
return {
1 : "audio",
3 : "video",
}.get(column_type, "other")
# 获取我的专栏
def fetch_my_column():
payload = "{}"
#response = requests.request("POST", my_column_url, data=payload, cookies=cj, headers=mobile_headers)
response = requests.request("POST", my_column_url, data=payload, headers=mobile_headers)
json_data = json.loads(response.content.decode('utf-8'))
print(json_data)
data = json_data.get('code')
if data != 0:
return
list = json_data.get('data').get('list')
for item in list:
id = item.get('id')
column_type = item.get('column_type')
column_title = item.get('column_title')
print("id = %s type = %s column_title = %s" % (id, column_type_string(column_type), column_title))
# 根据专栏 id 获取视频
def download_video_by_cid(cid, size):
payload = " {\"cid\":%d,\"size\":%d,\"prev\":%d,\"order\":\"earliest\"}" % (cid, size, 0)
response = requests.request("POST", video_url, data=payload, cookies=cj, headers=mobile_headers)
print(response.content.decode('utf-8'))
download_videos(response)
def download_videos(response):
json_data = json.loads(response.content.decode('utf-8'))
print(json_data)
data = json_data.get('code')
if data != 0:
return
list = json_data.get('data').get('list')
for item in list:
video_media = item.get('video_media')
article_title = item.get('article_title')
pattern = ' |\|'
article_title = re.sub(pattern, '_', article_title)
print(article_title)
print(video_media)
if len(video_media) > 0:
video = json.loads(video_media)
video_path = VIDEO_PATH + article_title +'.mp4'
m3u8 = video.get('hd').get('url')
cmd = 'ffmpeg -y -i %s %s' % (m3u8, video_path)
os.system(cmd.encode('utf-8'))
# 根据专栏 id 获取音频
def download_audio_by_cid(cid, size):
payload = " {\"cid\":%d,\"size\":%d,\"prev\":%d,\"order\":\"newest\"}" % (cid, size, 0)
response = requests.request("POST", audio_url, data=payload, cookies=cj, headers=mobile_headers)
download_audio(response)
def download_json(json_data, file_name):
json_path = JSON_PATH + file_name + '.json'
# save json file
with open(json_path, 'wt') as f:
# unicode
json_string = json.dumps(json_data, sort_keys=False, ensure_ascii=False, indent=4)
f.write(json_string)
print("save : {}.json".format(file_name))
f.close()
def download_one_audio(audio_download_url, article_sharetitle):
if len(audio_download_url) > 0:
print(audio_download_url)
mp3_path = AUDIO_PATH + article_sharetitle +'.mp3'
cmd = "wget -c %s -O %s" % (audio_download_url, mp3_path)
os.system(cmd.encode('utf-8'))
def download_audio(response):
json_data = json.loads(response.content.decode('utf-8'))
print(json_data)
data = json_data.get('code')
if data != 0:
return
list = json_data.get('data').get('list')
for item in list:
audio_download_url = item.get('audio_download_url')
article_sharetitle = item.get('article_sharetitle')
pattern = ' |\|'
article_sharetitle = re.sub(pattern, '_', article_sharetitle)
print(article_sharetitle)
download_one_audio(audio_download_url, article_sharetitle)
def exec_audio():
cid = int(input("input audio column id:\n> "))
download_audio_by_cid(cid, default_size)
def exec_video():
cid = int(input("input video column id:\n> "))
download_video_by_cid(cid, default_size)
print("login 'https://geekbang.org' from chrome first")
print("put your Coookie to header: line 10")
if __name__ == "__main__":
""" 下载音频
cid = 50
size = 100
download_audio_by_cid(cid, size)
fetch_my_column()
"""
""" 下载视频
cid = 77
size = 100
download_video_by_cid(cid, size)
"""
""" 查看所有专栏
"""
fetch_all_column()
""" 查看我的专栏
fetch_my_column()
"""