-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
254 lines (204 loc) · 7.71 KB
/
main.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
# -*- coding: utf-8 -*-
## /reg?username=<NAME>&identity=<CirrusID>
## usernameのidentityを更新
## 自分以外のidentityを返す
#pre import
execfile("pre_import.py")
#controllers
from controllers.room import *
from controllers.chat import *
from controllers.login import *
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.api import memcache
import urllib
from django.utils import simplejson
def fetchjson(response,key):
'''spread sheetからjsonを取得し、pythonオブジェクトに変換'''
sql = "" #"select * "
sql = urllib.quote(sql)
# url = "http://spreadsheets.google.com/tq?key=twenqTTEoUUigkPWhKdHhUA&tq=" + sql
json = None
json = memcache.get("cache_json/"+key)
if json:
return json #キャッシュを返して終了
url = "http://spreadsheets.google.com/tq?key=" + key
result = urlfetch.fetch(url)
res = result.content
# return
res = res.replace("google.visualization.Query.setResponse(","")
res = re.sub("\)\;$","",res)
res = res.replace("yyyy/MM/dd H:mm:ss","")
res = res.replace("#0.###############","")
res = re.sub("('style'\:\'[^\']+\')","",res)
res = re.sub("new Date\(([\d\,]+)\)","'\\1'",res)
res = res.replace('c:[,','c:[{v:\'\',f:\'\'},') #google spread sheetのバグ。日付型に文字がはいっているとオブジェクトが出力されない
res = res.replace("},,{","},{v:''},{") #spread sheetのバグ対応
res = re.sub("([{,])([a-z]+)\:","\\1'\\2':",res) #変数名をクオーテーションで囲む
res = re.sub(":([0-9]+)\.[0-9]+",":'\\1'",res) #小数点切り捨て
res = res.replace('"','')
res = res.replace("\\n",' ')
res = res.replace("},","},\n") #改行をいれてデバッグしやすく
res = res.replace("'",'"') #ダブルクオーテーションじゃないとパースしてくれない
# response.out.write(res)
# return
# self.response.out.write(res)
# logging.info(res)
# res = '{"name": "John Smith", "age": 33}'
try:
json = simplejson.loads(res)
except:
logging.error("simple json parse error!!")
json = memcache.get("backup/"+key)
if json:
try:
memcache.set("backup/"+key,json)
memcache.set("cache_json/"+key,json,60*3)
except:
logging.error("set memcache error!!")
# logging.info(json)
return json
def addlink(str):
cgi.escape(str)
# [[ ]] ブロック
def subblock(m):
ch = m.group(1) # マッチ・オブジェクトmのgroup(0)は、ヒットした文字列を取り出す。
lb = ch
if len(ch) > 50:
lb = ch[0:50] + "..."
ch = '<a href="'+ch+'" target="_blank">'+lb+'</a>'
return ch
block = re.compile(r'(https?:\/\/[a-zA-Z0-9_\.\/\~\%\:\#\?=&\;\-,]+)')
str = block.sub(subblock,str)
str = re.sub("@(\w+)",'<a target="_blank" href="http://twitter.com/\\1">@\\1</a>',str)
return str
def getjson(response,q,page,limit,version,status,link):
cachekey = q+"/"+str(page)+"/"+str(limit)+"/"+str(version)+"/"+status+"/"+link
cache = memcache.get("gj//" + cachekey)
if cache:
return cache
key = 'tgjMrXxdYuNdW0JEMBi36bA'
json = fetchjson(response,key)
newrows = []
rows = json["table"]["rows"]
for row in rows:
comment = row["c"][7]["v"]
if link == 'yes':
comment = addlink(comment)
newrows.append({
"time" : row["c"][0]["f"],#ここだけf
"pref" : row["c"][1]["v"],
"city" : row["c"][2]["v"],
"month" : row["c"][3]["v"],
"day" : row["c"][4]["v"],
"hour" : row["c"][5]["v"],
"status" : row["c"][6]["v"],
"comment" : comment,
})
#ここからフィルタリング
rows = newrows
if q:
newrows = []
for row in rows:
if re.search(q,row["pref"] + row["city"]):
newrows.append(row)
rows = newrows
if status:
newrows = []
for row in rows:
sq = status
if status == "yes":
sq = u"確認できました"
elif status == "no":
sq = u"確認できませんでした"
if re.search(sq,row["status"]):
newrows.append(row)
newrows.reverse()
count = len(newrows)
newrows = newrows[(page-1)*limit:page*limit]
result = None
if version == 1:
result = newrows
else:
result = {"count":count,
"page" : page,
"maxpage":int(count/limit)+1,
"rows":newrows}
memcache.set("gj//" + cachekey,result,60)
return result
class MainPage(BasePage):
def get(self):
'''無事.me情報'''
q = self.request.get("q")
format = self.request.get("format")
page = int(self.request.get("page",1)) or 1
limit = int(self.request.get("limit",60)) or 60
version = int(self.request.get("ver",1)) or 1
status = self.request.get("status")
link = self.request.get("link","")
newrows = getjson(self.response, q, page, limit,version,status,link)
if format == "json":
self.render_json(newrows)
else:
templates = {
"rows" : newrows
}
self.render("../views/top.html",templates)
class GenbaPage(BasePage):
def get(self):
'''震災現場情報'''
q = self.request.get("q")
id = self.request.get("id")
page = int(self.request.get("page",1)) or 1
limit = int(self.request.get("limit",60)) or 60
key = 't1aygyrFxv9VEjHcXUbfHxQ'
json = fetchjson(self.response,key)
newrows = []
# return
rows = json["table"]["rows"]
cols = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","aa","ab","ac","ad","ae","af"]
newrows = []
for row in rows:
newrow = {}
for x in xrange(len(row["c"])):
newrow[cols[x]] = row["c"][x]["v"]
newrows.append(newrow)
#ここからフィルタリング
rows = newrows
if q:
newrows = []
for row in rows:
if re.search(q,row["b"] + row["c"] + row["d"] + row["e"]):
newrows.append(row)
rows = newrows
if id:
newrows = []
for row in rows:
if id == row["a"]:
newrows.append(row)
newrows.reverse()
count = len(newrows)
newrows = newrows[(page-1)*limit:page*limit]
result = None
result = {"count":count,
"page" : page,
"maxpage":int(count/limit)+1,
"rows":newrows}
self.render_json(result)
def is_local():
domain = ''
if os.environ.has_key("SERVER_NAME"):
domain = os.environ["SERVER_NAME"]
isLocal = (not domain or not re.search("(buji\.me|appspot\.com)",domain))
return isLocal
webapp.template.register_template_library("customtags.xss")
webapp.template.register_template_library("customtags.link")
application = webapp.WSGIApplication([ ('/', MainPage),
('/genba', GenbaPage),
],
debug=is_local())
def main():
logging.getLogger().setLevel(logging.DEBUG)
run_wsgi_app(application)
if __name__ == "__main__":
main()