-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
102 lines (77 loc) · 3.57 KB
/
proxy.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
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import requests
import sqlite3 as lite
##Check for update.
try:
if int(requests.get("https://raw.githubusercontent.com/wfscans/kanpani-tl-project/master/version").content) > 1:
print "New version available! Check https://github.com/wfscans/kanpani-tl-project for more details."
except Exception as e:
print "Failed to check for new version."
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Cache-Control","max-age=604800")
self.end_headers()
try:
with open(self.path[self.path.index("/img/")+1:len(self.path)], 'rb') as f:
self.wfile.write(f.read())
return
except Exception as e:
pass
headers = []
for i in self.headers.headers:
headers.append(i.split(":", 1)[0].strip())
headers.append(i.split(":", 1)[1].strip())
headers = iter(headers)
headers = dict(zip(headers, headers))
self.wfile.write(requests.get(self.path, headers=headers, stream=True).raw.data)
def do_POST(self):
self.send_response(200)
self.end_headers()
headers = []
for i in self.headers.headers:
headers.append(i.split(":", 1)[0].strip())
headers.append(i.split(":", 1)[1].strip())
headers = iter(headers)
headers = dict(zip(headers, headers))
rbody = self.rfile.read(int(self.headers.getheader('content-length'))) #Request body length
res = requests.post(self.path, data=rbody, headers=headers, stream=True)
if ("c=Quest.next" in self.path):
conn = lite.connect('kp_trans.db')
cur = conn.cursor()
res = res.raw.data
conn.text_factory = str
cur.execute("SELECT jp, eng, id FROM story WHERE map = ?", (rbody[len(rbody)-10:len(rbody)],))
for row in cur:
res = res.replace(row[0], row[1])
self.wfile.write(res)
conn.close()
elif ("c=Quest.main_enter" in self.path) or ("c=Quest.special_enter" in self.path):
conn = lite.connect('kp_trans.db')
cur = conn.cursor()
res = res.raw.data
conn.text_factory = str
cur.execute("SELECT jp, eng, id FROM story WHERE map = ?", (rbody[len(rbody)-29:len(rbody)-12],))
for row in cur:
res = res.replace(row[0], row[1])
self.wfile.write(res)
conn.close()
elif ("c=Quest.main_stages" in self.path):
conn = lite.connect('kp_trans.db')
cur = conn.cursor()
res = res.raw.data
conn.text_factory = str
cur.execute("SELECT jp, eng FROM desc WHERE map = ?", (rbody[-8:],))
for row in cur:
res = res.replace(row[0], row[1])
self.wfile.write(res)
else:
self.wfile.write(res.raw.data)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
if __name__ == '__main__':
server = ThreadedHTTPServer(('localhost', 8899), Handler)
print 'Starting server, use <Ctrl-C> to stop'
server.serve_forever()