-
Notifications
You must be signed in to change notification settings - Fork 0
/
qjwgg.py
198 lines (162 loc) · 5 KB
/
qjwgg.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import web
import os
import cgi
import random
import time
import util
from database import dbhandler
web.config.debug = False
LINUX_URL = '/linux'
PYTHON_URL = '/python'
WEBPY_URL = '/webpy'
dbhandler.connect()
cgi.maxlen = 1 * 1024 * 1024 # 1MB
urls = (
'/?', 'index',
LINUX_URL + '(.*)', 'linux',
PYTHON_URL + '(.*)', 'python',
'/tools' + '(.*)', 'tools',
WEBPY_URL + '(.*)', 'webpy',
'/search' + '(.*)', 'search',
'/about' + '(.*)', 'about',
'/publish' + '(.*)', 'publish',
'/qrcode' + '(.*)', 'qrcode',
'(.+)', 'other',
)
render = web.template.render('templates', cache=False)
web.template.Template.globals['render'] = render
class index:
def GET(self):
html = util.md2html('./README.md')
return render.index(html)
class linux:
def GET(self, url):
if url == '' or url == '/':
url = '/index.html'
url = LINUX_URL + url
article = util.url2article(url)
if article['download'] is True:
return article['stream']
elif article['notfound'] is True:
return render.error(article)
else:
return render.article(article)
class python:
def GET(self, url):
if url == '' or url == '/':
url = '/index.html'
url = PYTHON_URL + url
article = util.url2article(url)
if article['download'] is True:
return article['stream']
elif article['notfound'] is True:
return render.error(article)
else:
return render.article(article)
class tools:
def GET(self, url):
if url == '' or url == '/':
url = '/index.html'
url = '/tools' + url
article = util.url2article(url)
if url == '/tools/index.html':
return render.tools_index(article)
elif article['notfound'] is True:
return render.error(article)
else:
return render.tools(article)
class webpy:
def GET(self, url):
if url == '' or url == '/':
url = '/index.html'
url = WEBPY_URL + url
article = util.url2article(url)
if article['download'] is True:
return article['stream']
elif article['notfound'] is True:
return render.error(article)
else:
article['catalog'] = util.md2html('Markdown/webpy/catalog.md')
return render.article_webpy(article)
class search:
def GET(self, url):
article = {}
article['title'] = 'search'
html = ''
key = ''
x = web.input()
key = x.get('key', '')
if key != '':
key = key.encode('utf-8')
html = util.traversal_path('./Markdown', key)
article['content'] = html
return render.search(article)
class about:
def GET(self, url):
article = {}
if url == '':
url = '/'
url = '/about' + url
article = util.url2article(url)
return render.about(article)
class publish:
def GET(self, url):
file = open('./Markdown/markdown_online.md', 'rb')
md = file.read()
file.close()
return render.publish(md)
class qrcode:
def GET(self, url):
article = {}
article['img'] = ''
article['text'] = ''
article['img_name'] = 0
return render.qrcode(article)
def POST(self, url):
article = {}
isicon = False
article['img_name'] = time.time()
try:
x = web.input(myfile={})
except ValueError:
return "File too large!"
text = x.get('text', '')
myfile = x.get('myfile', '')
if text == '':
raise web.seeother('')
else:
if myfile != '' and myfile.filename != '':
filename = myfile.filename
if filename.endswith('.ico') or filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.bmp'):
fout = open('./static/img/qrcode.ico','w')
fout.write(myfile.file.read())
fout.close()
isicon = True
else:
return 'Unsupported image formats!'
img_name = article['img_name']
util.text2qrcode(text, isicon, img_name)
article['text'] = text
article['img'] = '<img src="/static/img/qrcode/%s.png"/>'%(img_name)
return render.qrcode(article)
class other:
def GET(self, url):
if url == '/sitemap.xml':
fh = open('sitemap.xml')
sitemap = fh.read()
fh.close()
return sitemap
elif url == '/robots.txt':
fh = open('robots.txt')
robots = fh.read()
fh.close()
return robots
else:
article = {}
article['title'] = '404 Not Found'
return render.error(article)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()