forked from GaryNotGay/API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper.py
413 lines (368 loc) · 14.3 KB
/
paper.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import os
import oss2
import time
import fitz
import json
import pyDes
import base64
import PyPDF2
import random
import hashlib
import pikepdf
import binascii
import requests
import urllib.parse
from pyDes import *
class RMRB:
def getYMD(self):
return time.strftime("%Y-%m/%d", time.localtime())
def getLen(self, YMD):
u = "http://paper.people.com.cn/rmrb/html/{}/nbs.D110000renmrb_01.htm".format(YMD)
req = requests.get(u)
req_str = str(req.content.decode('utf-8'))
return req_str.count('版:')
def rmrb(self, path, mode):
if int(mode) == 0:
date = getDate()
YMD = self.getYMD()
else:
date = str(mode)
YMD = time.strftime("%Y-%m/%d", time.strptime(date, "%Y%m%d"))
print("Input Params: Path={path}, Date={date}".format(path=path, date=date))
length = self.getLen(YMD)
for index in range(length):
if int(date) <= 20200630:
u = "http://paper.people.com.cn/rmrb/page/{YMD}/{index:0>2d}/rmrb{date}{index:0>2d}.pdf".format(YMD=YMD, date=date, index=index+1)
else:
u = "http://paper.people.com.cn/rmrb/images/{YMD}/{index:0>2d}/rmrb{date}{index:0>2d}.pdf".format(YMD=YMD, date=date, index=index+1)
downFile(u, path+"rmrb_"+str(date)+"_"+str(index+1)+".pdf")
print("RMRB Down Complete!")
if mergePDF(length, "rmrb_", path, date):
print("RMRB Merge Complete!")
class BJYB:
def getYMD(self):
return time.strftime("%Y-%m/%d", time.localtime())
def getHtml(self, YMD):
u = "http://epaper.ynet.com/html/{}/node_1331.htm".format(YMD)
req = requests.get(u)
return str(req.content.decode('utf-8'))
def bjyb(self, path, mode):
if int(mode) == 0:
date = getDate()
YMD = self.getYMD()
else:
date = str(mode)
YMD = time.strftime("%Y-%m/%d", time.strptime(date, "%Y%m%d"))
print("Input Params: Path={path}, Date={date}".format(path=path, date=date))
URL_LEN = 39
req_str = self.getHtml(YMD)
pdfnum = req_str.count('.pdf')
start = 0
for index in range(pdfnum):
end = req_str.find('.pdf', start)
baseurl = req_str[end-URL_LEN:end]
start = end+4
url = "http://epaper.ynet.com" + baseurl + ".pdf"
downFile(url, path+"bjyb_"+str(date)+"_"+str(index+1)+".pdf")
print("BJYB Down Complete!")
if mergePDF(pdfnum, "bjyb_", path, date):
print("BJYB Merge Complete!")
for index in range(pdfnum):
os.remove(path+"bjyb_"+str(date)+"_"+str(index+1)+".pdf")
print("BJYB Delete Complete!")
class TTZB:
def init2cipher(self, text):
out_str = base64.b64decode(text)
return str(out_str.hex())
def unCompileCode(self, code):
c = ""
for index in range(len(code)):
c += chr(ord(code[index]) - 1 ^ index)
return c
def DesECB(self, cipher, key):
iv = ""
k = des(key[0:8], ECB, iv, pad=None, padmode=PAD_PKCS5)
de = k.decrypt(binascii.a2b_hex(cipher), padmode=PAD_PKCS5)
return str(de)
def getText(self, pdfID):
u = "http://www.ttplus.cn/reader.html?pdfId=" + str(pdfID)
c = {"SESSION": TTZB_COOKIE}
r = requests.get(url=u, cookies=c)
req = r.content.decode("utf-8")
s = req.find("webViewerLoad")
rearr = []
e = req.find(";", s)
st = req[s + 15:e - 2]
starr = st.split(",")
# print(starr[0][2:])
rearr.append(starr[0][:-1])
rearr.append(starr[1][2:])
return rearr
# return starr[1][2:]
def getPDF(self, path, url, name):
req = requests.get(url)
# print(req.content)
with open(path + name, "wb") as f:
f.write(req.content)
f.close()
return True
def dePDF(self, path, srcname, pwd):
pdf = pikepdf.open(path + srcname, password=pwd[2:-1])
pdf.save(path + "已解密 " + srcname)
def getSign(self, kvs):
VER = "_VER"
VER_CURRENT_VAL = "1.0"
SIGN_CURRENT_SECRET = "a7ff2586301b42eea962e9b9c8709689"
nkvs = ""
l = []
kvs[VER] = VER_CURRENT_VAL
for k in kvs:
v = kvs[k]
l.append(k + "=" + v + "&")
l.sort(key=str.lower)
nkvs += ''.join(l) + "key=" + SIGN_CURRENT_SECRET
result = hashlib.md5(nkvs.encode('utf-8'))
sign = str(result.hexdigest().upper())
return sign
def getParam(self, year, sign):
# ttzb type=11
# kvs = {"typeId": 11 ,"year": year, "pageSize": 30, "pagenum": 0, "_VER":1.0, "_SIGN":sign}
st = "typeId=11&year=" + str(year) + "&pageSize=130&pagenum=0&_VER=1.0&_SIGN=" + sign
return st
def getAPI(self, param):
# print(param)
name = {}
u = "https://api.ttplus.cn/h5/pdf/all"
h = {"content-type": "application/x-www-form-urlencoded; charset=UTF-8"}
r = requests.post(url=u, data=param, headers=h)
# req = r.content.decode('utf-8')
rj = r.json()
for index in range(len(rj["content"]["newsdatas"])):
id = rj["content"]["newsdatas"][index]["id"]
title = rj["content"]["newsdatas"][index]["title"] + ".pdf"
name[id] = title
# print(rj["content"]["newsdatas"][0])
return name
def getOne(self, id):
kvs = {"newspaperId": str(id), "userId": TTZB_USERID}
sign = self.getSign(kvs)
st = "newspaperId=" + str(id) + "&userId=" + str(TTZB_USERID) + "&_VER=1.0&_SIGN=" + sign
u = "https://api.ttplus.cn/h5/pdf/one"
h = {"content-type": "application/x-www-form-urlencoded; charset=UTF-8"}
r = requests.post(url=u, data=st, headers=h)
rj = r.json()
name = rj["content"]["newspapertype"] + " " + rj["content"]["updatetime"] + ".pdf"
# print(name)
return name
def ttzb(self, path, TTZB_PARAM):
pdfID = TTZB_PARAM
name = self.getOne(pdfID)
rearr = self.getText(pdfID)
text = rearr[1]
cipher = self.init2cipher(text)
key = self.unCompileCode(TTZB_CODE)
pdfkey = self.DesECB(cipher, key)
print("key:" + pdfkey)
url = rearr[0]
print("url:" + url)
self.getPDF(path, url, name)
print("TTZB Down Complete!")
self.dePDF(path, name, pdfkey)
#os.remove(path + name)
os.rename(path + "已解密 "+ name, path + "tmp.pdf")
print("TTZB Decrypt Complete!")
class ZQB:
def getInkey(self, pid):
u = "http://www.dooland.com/magazine/online_htm5.php?pid=" + str(pid)
r = requests.get(url=u)
res = r.content.decode("utf-8")
s = res.find("data-inkey=")
return res[s + 12:s + 44]
def getDest(self, pid, inkey):
u = "http://www.dooland.com/magazine/InterFace/s_EchoXml_Streamv2_htm5.php?pid=" + str(pid) + "&inkey=" + str(inkey) + "&uid=" + ZQB_UID
h = {"cookies": ZQB_COOKIE}
r = requests.get(url=u, headers=h)
res = r.content.decode("utf-8")
return str(res)
def bingo_decode(self, dest, key):
keyLength = len(key)
keyArray = []
for index in range(keyLength):
keyArray.append((ord(key[index]) % 6))
destTemp = "";
for index in range(len(dest)):
destTemp += (chr(ord(dest[index]) - keyArray[index % keyLength]))
source = urllib.parse.unquote(destTemp)
return source
def src2url(self, source):
urlArr = []
start = 0
num = int(int(source.count(".jpg")) / 2)
for index in range(num):
a = source.find('+src=', start)
b = source.find('+ssrc=', start)
start = b + 1
# print(source[a+6: b-1])
urlArr.append(source[a + 6: b - 1])
return urlArr
def getDate():
return time.strftime("%Y%m%d", time.localtime())
def getJPG(self, path, urlArr):
print("ZQB Total " + str(len(urlArr)))
for index in range(len(urlArr)):
name = str(path + getDate()) + "_" + str(index + 1) + ".jpg"
url = urlArr[index]
req = requests.get(url)
with open(name, "wb") as f:
f.write(req.content)
f.close()
#print(name + " success")
return True
def JPG2PDF(self, path, num):
for index in range(num):
doc = fitz.open()
img_file = path + getDate() + "_" + str(index+1) + ".jpg"
imgdoc = fitz.open(img_file)
pdfbytes = imgdoc.convert_to_pdf()
pdf_name = path + getDate() + "_" + str(index+1) + ".pdf"
imgpdf = fitz.open(pdf_name, pdfbytes)
doc.insert_pdf(imgpdf)
doc.save(path + "zqb_" + getDate() + "_" + str(index+1) + ".pdf")
doc.close()
os.remove(path + getDate() + "_" + str(index+1) + ".jpg")
def getName(self, pid):
u = "http://www.dooland.com/magazine/online_htm5.php?pid=" + str(pid)
r = requests.get(url=u)
res = r.content.decode("utf-8")
s = res.find("<title>")
e = res.find("电子杂志 - 读览天下")
return res[s+7: e-1]
def zqb(self, path, pid):
key = self.getInkey(pid)
dest = self.getDest(pid, key)
source = self.bingo_decode(dest, key)
urlArr = self.src2url(source)
self.getJPG(path, urlArr)
self.JPG2PDF(path, len(urlArr))
print("ZQB Down Complete!")
mergePDF(len(urlArr), "zqb_", path, getDate())
def getAuth(name, tm, rand, uid):
key = ""
m = hashlib.md5()
#print(f"/{name}-{tm}-{rand}-{uid}-{key}")
m.update(f"/{name}-{tm}-{rand}-{uid}-{key}".encode("utf-8"))
return m.hexdigest()
def getRandom():
s = ""
for i in range(32):
s += random.choice('abcdefghijklmnopqrstuvwxyz1234567890')
return s
def getDate():
return time.strftime("%Y%m%d", time.localtime())
def downFile(url, name):
req = requests.get(url)
#print(req.content)
with open(name, "wb") as f:
f.write(req.content)
f.close()
return True
def mergePDF(length, type, path, date):
file_merger = PyPDF2.PdfFileMerger(strict=False)
#if type == "rmrb":
for index in range(length):
filename = path+type+str(date)+"_"+str(index+1)+".pdf"
try:
file_merger.append(filename)
except:
continue
file_merger.write(path+"tmp"+".pdf")
file_merger.close()
return True
def up2OSS(name):
auth = oss2.Auth('', '')
bucket = oss2.Bucket(auth, '', '')
bucket.put_object_from_file(name, '/tmp/tmp.pdf')
return bucket.object_exists(name)
def getURL(name, ip):
url = "https://oss.telecom.ac.cn/"
tm = str(int(time.time()) + 3600)
rand = getRandom()
#ip = environ['REMOTE_ADDR']
uid = hashlib.md5((ip+tm).encode("utf-8")).hexdigest()
auth = getAuth(name, tm, rand, uid)
finalurl = f"{url}{name}?auth_key={tm}-{rand}-{uid}-{auth}"
#print(finalurl)
return finalurl
def querydata2json(data):
MUST = ["pid", "ptype"]
rj = {}
da = data.split('&')
for i in range(len(da)):
daa = da[i].split("=")
rj[daa[0]] = daa[1]
for key in MUST:
if key not in rj:
return "ERR"
return rj
def handler(environ, start_response):
global TTZB_CODE
global TTZB_USERID
global TTZB_COOKIE
global ZQB_UID
global ZQB_COOKIE
try:
query_data = querydata2json(environ['QUERY_STRING'])
if query_data == "ERR":
ERR_INFO = {"Status":"False", "Message": "缺少必要参数,请参考接口文档,确认必要参数", "Info": "https://doc.api.telecom.ac.cn/"}
start_response('200 OK', [('Content-type', 'application/json; charset=utf-8'), ('Access-Control-Allow-Origin', '*')])
return json.dumps(ERR_INFO, ensure_ascii=False)
pid = str(query_data["pid"])
ptype = str(query_data["ptype"])
PATH = '/tmp/'
url = ""
outname = ""
if ptype == "1":
outname = f"RMRB-{pid}"
rmrb = RMRB()
rmrb.rmrb(PATH, pid)
elif ptype == "2":
outname = f"BJYB-{pid}"
bjyb = BJYB()
bjyb.bjyb(PATH, pid)
elif ptype == "3":
ttzb = TTZB()
TTZB_CODE = "rayux~ddl|m~"
if "pid" in query_data:
TTZB_USERID = query_data["pid"]
else:
TTZB_USERID = ""
if "pcookie" in query_data:
TTZB_COOKIE = query_data["pcookie"]
else:
TTZB_COOKIE = ""
outname = ttzb.getOne(pid)[:-4]
ttzb.ttzb(PATH, pid)
elif ptype == "4":
zqb = ZQB()
if "pid" in query_data:
ZQB_UID = query_data["pid"]
else:
ZQB_UID = ""
if "pcookie" in query_data:
ZQB_COOKIE = query_data["pcookie"]
else:
ZQB_COOKIE = ""
ZQB_COOKIE = "auth="+ZQB_COOKIE
outname = zqb.getName(pid)
zqb.zqb(PATH, pid)
filetype = ".pdf"
name = getRandom().upper()[0:16] + filetype
if up2OSS(name):
url = getURL(name, environ['REMOTE_ADDR'])
outjson = {"Status":"True", "PaperName":outname, "PaperUrl":url}
start_response('200 OK', [('Content-type', 'application/json; charset=utf-8'), ('Access-Control-Allow-Origin', '*')])
return json.dumps(outjson, ensure_ascii=False)
except Exception as r:
outjson = {"Status": "False", "Message": "未知错误,请参考错误信息,定位原因,或联系作者", "Info": r}
start_response('200 OK', [('Content-type', 'application/json; charset=utf-8'), ('Access-Control-Allow-Origin', '*')])
return json.dumps(outjson, ensure_ascii=False)