-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.py
98 lines (96 loc) · 4.78 KB
/
book.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import json
import re
# import sys
# sys.path.append('..')
from cls import LocalFile, AmySQL
# from cls import LocalFile
# from cls.MySQL import MySQL
class Books():
def get_book_info(id):
jsontext = {'books':[]} # 默认返回空字典数据格式
# response_header = "HTTP/1.1 404 Not Found\r\nServer: PWS1.0"
# response_body = 'Sorry, File Not Found'
response_header = 'HTTP/1.1 200 OK\r\nServer: PWS1.0'
response_body = 'index'
try:
LocalFile.write_LogFile('ID:' + id)
zid = '0'
md5 = ''
md5_reported = ''
title = ''
author = ''
publisher = ''
description = ''
extension = ''
language = ''
year = ''
cid = ''
data = ''
# 通过MD5查询记录
if(len(id) == 32 and id.isalnum() == True):
data = AmySQL.get_table_one('select zlibrary_id, extension, md5, md5_reported, title, author, publisher, language, year, description from books where md5 = \'' + id + '\' or md5_reported = \'' + id + '\'')
elif(id.isdigit() == True):
if(int(id) <= 22433982):
zid = str(id)
data = AmySQL.get_table_one('select zlibrary_id, extension, md5, md5_reported, title, author, publisher, language, year, description from books where zlibrary_id = ' + str(zid))
else:
if(id.isalnum() == True):
data = AmySQL.get_table_one('select zlibrary_id from isbn where isbn = \'' + id + '\'')
if(data != ''):
zid = str(data[0])
if(zid != '0'):
data = AmySQL.get_table_one('select zlibrary_id, extension, md5, md5_reported, title, author, publisher, language, year, description from books where zlibrary_id = ' + zid)
else:
#id = 0
data = AmySQL.get_table_one('select zlibrary_id, extension, md5, md5_reported, title, author, publisher, language, year, description from books where title like \'%' + str(id) + '%\' limit 30')
# 查询图书
if(data != ''):
zid = data[0]
extension = data[1]
md5 = data[2]
if(md5 == None):
md5 = ''
md5_reported = data[3]
title = data[4]
author = data[5]
publisher = data[6]
language = data[7]
year = data[8]
description = data[9]
description = re.sub('<[^<]+?>', '', description).replace('\n', '').strip()
if(cid == ''):
data = AmySQL.get_cont('select cid from zlib12 where id = ' + str(zid))
if(data != ''):
cid = data[0]
if(cid == ''):
data = AmySQL.get_cont('select cid from libgen where md5 = \'' + md5 + '\' or md5 = \'' + md5_reported + '\'')
if(data != ''):
cid = data[0]
if(cid == ''):
data = AmySQL.get_cont('select cid from zlib122 where id = \'' + md5 + '\' or md5 = \'' + md5_reported + '\'')
if(data != ''):
cid = data[0]
# for j in data:
# print(j[0]) # 处理多条记录
response_data = ''
response_header = ''
response_body = ''
nodeurl = ''
if(len(cid) >= 46):
# 临时测试数据
jsontext['books'].append({'zid': str(zid), 'md5': md5, 'title': title, 'author': author, 'language': language, 'year': year, 'publisher': publisher, 'description': description, 'extension': extension, 'cid': cid})
LocalFile.write_LogFile('\n' + str(jsontext))
response_header = 'HTTP/1.1 200 OK\r\nServer: PWS1.0' # 响应头
# response_body = nodeurl # 响应体
response_body = json.dumps(jsontext, indent=2, ensure_ascii=False)
# response_header = 'HTTP/1.1 301 Moved Permanently\r\nServer: PWS1.0\r\nLocation: ' + nodeurl
# #response_header = "HTTP/1.1 302 Moved Temporarily\r\nServer: PWS1.0"
# response_body = ''
except Exception as ex:
ex = 'Main-Line-110-Exception:\n' + str(ex)
print(ex)
LocalFile.write_LogFile(ex)
response_data = (response_header + '\r\n\r\n' + response_body).encode('utf-8') # 将数据组装成HTTP响应报文数据发送给浏览器
return response_data