-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_filter.py
490 lines (417 loc) · 13.8 KB
/
custom_filter.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!-*- coding:utf-8 -*-
#!/usr/bin/env python
#---------------------------------------------------
#テンプレートエンジンから参照する関数
#copyright 2010-2012 ABARS all rights reserved.
#---------------------------------------------------
import datetime
import sys
import traceback
import re
import urllib
import hashlib
from google.appengine.api import users
from google.appengine.ext import db
from myapp.UTC import UTC
from myapp.JST import JST
from myapp.Bookmark import Bookmark
from myapp.ApiObject import ApiObject
from myapp.BbsConst import BbsConst
from myapp.TimeProgress import TimeProgress
#-----------------------------------------------------------------
#js host map
#-----------------------------------------------------------------
def host_for_js(host):
#iOS9で同じドメインからjsを読むと30secのレイテンシがかかる
#そのため、jsは別ドメインにする必要がある
#if(host=="http://illust-book.appspot.com/"):
# return "http://www.illustbook.net/"
#if(host=="http://www.illustbook.net/"):
# return "http://illust-book.appspot.com/"
return host
#-----------------------------------------------------------------
#Escape
#-----------------------------------------------------------------
def iriencode(value):
return urllib.quote(value.encode("utf-8"))
#-----------------------------------------------------------------
# 時間取得
#-----------------------------------------------------------------
def time_JST_format(value,mode):
tmp=value.replace(tzinfo=UTC()).astimezone(JST())
if(not mode):
return ""+str(tmp.year)+"年"+str(tmp.month)+"月"+str(tmp.day)+"日"+str(tmp.hour)+"時"+str(tmp.minute)+"分"
if(mode==1):
return ""+str(tmp.year)+"年"+str(tmp.month)+"月"+str(tmp.day)+"日"+str(tmp.hour)+"時"
if(mode==2):
return ""+str(tmp.year)+"年"+str(tmp.month)+"月"+str(tmp.day)+"日"
if(mode==10):
return ""+str(tmp.month)+"月"+str(tmp.day)+"日"+str(tmp.hour)+"時"
if(mode==11):
return ""+str(tmp.month)+"月"+str(tmp.day)+"日"
return "NO FORMAT"
def time_JST(value):
return time_JST_format(value,0)
def time_JST_short(value):
return time_JST_format(value,10)
def time_JST_progress(value):
return TimeProgress.get_date_diff_str(value,"前",False)
def time_UTC_progress(value):
return TimeProgress.get_date_diff_str(value," ago",True)
#-----------------------------------------------------------------
#絵で返信
#-----------------------------------------------------------------
def reply_with_except(entry,host):
try:
ret="<a href='"+host
if(entry.bbs_key.short and entry.bbs_key.short!="None") :
ret+=str(entry.bbs_key.short)
else:
ret+="usr/"+str(entry.bbs_key.key())
ret+="/"
if(entry.thread_key.short):
ret+=entry.thread_key.short
else:
ret+=str(entry.thread_key.key())
ret+=".html' class='imagelink'>"
ret+="<img src='"+host+"thumbnail/"+str(entry.illust_reply_image_key.key())+".jpg' border='0' "
ret+="alt='"+str(entry.thread_key.title)+"'/>"
ret+="</a>"
except:
return ""#+traceback.format_exc()
return ret
def image_key_with_except(thread):
try:
return str(thread.image_key.key())
except:
return ""
#-----------------------------------------------------------------
#新着コメント表記
#-----------------------------------------------------------------
def comment_with_except(entry,host,is_english):
return comment_with_except_core(entry,host,0,is_english)
def comment_with_except_no_hour(entry,host,is_english):
return comment_with_except_core(entry,host,1,is_english)
def comment_with_except_core(entry,host,no_hour,is_english):
try:
ret="<a href='"+host;
if(entry["short"] and entry["short"]!="None") :
ret+=str(entry["short"])
else:
ret+="usr/"+str(entry["bbs_key"])
ret+="/"
ret+=str(entry["thread_key"])
ret+=".html";
ret+="#"+str(entry["entry_key"])
ret+="' class='decnone'>"
title=str(entry["thread_title"])
ret+=title
ret+="("+entry["editor"]+")"
ret+="("
if(is_english):
ret+=time_UTC_progress(entry["date"])
else:
ret+=time_JST_progress(entry["date"])
ret+=")</a> "
except:
return "title get error"+traceback.format_exc()
return ret
def admin_comment_with_except(entry):
try:
ret="<a href='./usr/"
ret+=str(entry.bbs_key.key())+"/"
ret+=str(entry.thread_key.key())
ret+=".html' target='_BLANK'>"
ret+=entry.content
ret+="</a>"
except:
return "error "+traceback.format_exc()
return ret
def show_bookmark(thread_key):
try:
thread=db.get(thread_key)
except:
return "error "+traceback.format_exc()
if(not thread):
return "deleted ";
if(not thread.bbs_key):
return "deleted ";
text="<a href='"
if(thread.bbs_key.short):
text+="./"+thread.bbs_key.short+"/"
else:
text+="./usr/"+thread.bbs_key.key+"/"
if(thread.short):
text+=""+thread.short
else:
text+=""+thread.key
text+=".html"
text+="' class='imagelink'><IMG SRC='"
text+="./thumbnail/"
text+=""+thread.image
text+=".jpg' border='0'/></a>";
return text
def show_bookmark_bbs(bbs_key):
try:
bbs=db.get(bbs_key)
except:
return "error "+traceback.format_exc()
if(not bbs):
return "deleted";
text="<a href='"
if(bbs.short):
text+="./"+bbs.short+"/"
else:
text+="./usr/"+str(bbs.key())+"/"
text+="' >";
text+=bbs.bbs_name
text+="</a>"
text+="<a href='show_bookmark?bbs_key="+str(bbs.key())+"'</a> "
if(bbs.bookmark_count):
text+="<small>("+str(bbs.bookmark_count)
if(bbs.bookmark_count==1):
text+="user"
else:
text+="users"
text+=")</small>"
text+="</a>"
return text
def div(value):
return value/60
#-----------------------------------------------------------------
#ブックマーク
#-----------------------------------------------------------------
def add_bookmark_core(command,search,count,is_iphone,is_english,user,is_thread):
txt=""
if(is_iphone):
txt+='<a href="javascript:'+command+'">'
if(is_thread):
if(is_english):
txt+="Bookmark thread"
else:
txt+="イラストをブックマーク"
else:
if(is_english):
txt+="Bookmark bbs"
else:
txt+="ボードをブックマーク"
return txt
if(count):
txt+='<a href="'+search
txt+='">'
txt+='<i class="icon-star"></i>'+str(count)
txt+='</a>'
if(user):
txt+='<a href="javascript:'+command+'">'
txt+='<i class="icon-plus"></i>'
if(is_english):
txt+='Bookmark'
else:
if(is_iphone):
txt+='ブクマ'
else:
txt+='ブックマーク'
txt+='</a>'
return txt
def add_bookmark_thread(thread,host,is_iphone,is_english,user):
is_english_flag="false"
if(is_english):
is_english_flag="true"
command="AddBookmark('"+host+"','"+str(thread.key())+"',"+is_english_flag+")"
search=""+host+'show_bookmark?thread_key='+str(thread.key())
count=thread.bookmark_count
return add_bookmark_core(command,search,count,is_iphone,is_english,user,True)
def add_bookmark_bbs(bbs,host,is_iphone,is_english,user):
is_english_flag="false"
if(is_english):
is_english_flag="true"
command="bbs_add_bookmark('"+bbs.bbs_name+"','"+str(bbs.key())+"','"+host+"',"+is_english_flag+")";
search=""+host+'show_bookmark?bbs_key='+str(bbs.key())
count=bbs.bookmark_count
return add_bookmark_core(command,search,count,is_iphone,is_english,user,False)
#-----------------------------------------------------------------
#アプリ表示
#-----------------------------------------------------------------
def show_app_core(app,size,size2):
txt='<div style="background-color:#ffffff;width:'+str(size2)+'px;min-height:'+str(size2)+'px;padding:4px;float:left;">'
txt+='<a href="app?mode=play&app_id='+app.app_id+'"><img src="app?mode=icon&app_id='+app.app_id+'" width='+str(size)+'px height='+str(size)+'px class="app_icon"></a>'
style='margin-bottom: 2px;font-size: .85em;font-weight: bold;font-family: arial,helvetica,osaka,\'MS PGothic\',sans-serif;color: #004B91;'
txt+='<p style="margin-top:4px;"><a href="app?mode=play&app_id='+app.app_id+'" style="'+style+'">'+app.app_name+'</a><br/>'
txt+='<a href="show_bookmark?app_key='+str(app.key())+'">';
txt+=str(app.bookmark_count)
if(app.bookmark_count==1):
txt+='user'
else:
txt+='users'
txt+='</a>';
txt+='</p>'
txt+='</div>'
return txt
def show_app(app):
return show_app_core(app,80,100)
def show_app_iphone(app):
return show_app_core(app,70,70)
def show_app_mypage(app):
txt=""
try:
app=db.get(app)
except:
return "アプリ取得エラー";
txt+='<a href="app?mode=play&app_id='+app.app_id+'">';
txt+='<img src="app?mode=icon&app_id='+app.app_id+'" width="50px" height="50px" class="app_icon_small">'
#txt+=str(app.app_name)
txt+='</a>';
return txt
def author_name_thread(thread,host):
return author_name_core(thread.user_id,thread.homepage_addr,thread.author,host)
def author_name_comment(entry,host):
return author_name_core(entry.user_id,entry.homepage_addr,entry.editor,host)
def user_id_to_user_name(user_id):
bookmark=ApiObject.get_bookmark_of_user_id(user_id)
if(bookmark):
return bookmark.name
return "未登録"
def author_name_core(user_id,homepage_addr,author,host):
txt=""
style=""#"margin-left:0px;margin-bottom:2px;"
if user_id:
txt+='<a href="'
txt+=host
txt+='mypage?user_id='
txt+=user_id
txt+='" class="g-button mini" style="'+style+'">'
txt+=author
txt+='</a>'
return txt
if homepage_addr:
txt+='<a href="'
txt+=''+homepage_addr
txt+='"'
txt+=' class="g-button mini" style="'+style+'">'
txt+=author
txt+="</a>"
return txt
return author
def separate_nico(name):
return name.split(" ‐ ")[0]
#-----------------------------------------------------------------
#ブックマークバー
#-----------------------------------------------------------------
def login_url(redirect_url):
return users.create_login_url(redirect_url)
def logout_url(redirect_url):
return users.create_logout_url(redirect_url)
def new_feed_count(obj):
#return 1
if(not obj):
return 0
#if(type(obj)==Bookmark):
# bookmark=obj
#else:
bookmark=ApiObject.get_bookmark_of_user_id(str(obj.user_id()))
if(not bookmark):
return 0
if(not bookmark.new_feed_count):
return 0
return bookmark.new_feed_count
#-----------------------------------------------------------------
#スパムフィルタ
#-----------------------------------------------------------------
def set_seed(no):
return BbsConst.SUBMIT_SEED
#-----------------------------------------------------------------
#URLの自動リンク
#-----------------------------------------------------------------
def auto_link(summary):
#Chromeのiframeのeditableがbrではなくdivを生成する問題の対策
#フォントを変えた場合のdivも削ってしまってdivで閉じれなくなるので断念
#summary=summary.replace('<div>', '<br/>')
#summary=summary.replace('</div>', '')
#これならどうだろう?
summary=summary.replace('<div', '<p')
summary=summary.replace('</div', '</p')
#改行コードの修正
summary=summary.replace('<br>', '<br/>')
#自動リンク
#PlaneText以外に適用すると変な挙動になるので、
#aタグが含まれていない場合のみに適用する
if(not(re.search(r'<a',summary))):
compiled_line = re.compile("(http://[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)")
summary = compiled_line.sub(r'<a href=\1 target="_BLANK">\1</a>', summary)
return summary
#-----------------------------------------------------------------
#サムネイルのサイズ
#-----------------------------------------------------------------
def thumbnail2_width(thread,is_iphone,is_tablet):
if(is_iphone):
return 152
if(is_tablet):
return 180
return 200
def thumbnail2_height(thread,is_iphone,is_tablet):
width=thumbnail2_width(thread,is_iphone,is_tablet)
try:
return thread["height"]*width/thread["width"]
except:
return width
#-----------------------------------------------------------------
#レギュレーションの確認
#-----------------------------------------------------------------
def _get_adult(thread):
if(type(thread)==dict):
if(thread.has_key("adult")):
adult=thread["adult"]
else:
adult=0
else:
adult=thread.adult
return adult
def regulation_check(thread,user):
adult=_get_adult(thread)
if(not adult):
return True
if(not user):
return False
bookmark=ApiObject.get_bookmark_of_user_id(user.user_id())
if(not bookmark):
return False
if(not bookmark.regulation):
return False
if(adult & bookmark.regulation):
return True #表示
return False
def regulation_name(thread):
adult=_get_adult(thread)
if(adult):
return "[R15]"
return ""
#-----------------------------------------------------------------
#JSのエスケープ
#-----------------------------------------------------------------
def escape_single_quotation(text):
return text.replace("'","")
#-----------------------------------------------------------------
#IPハッシュ
#-----------------------------------------------------------------
def ip_hash(text):
if(not text):
return "-"
return hashlib.sha1(text).hexdigest()[0:8]
#-----------------------------------------------------------------
#カテゴリの縮退
#-----------------------------------------------------------------
def truncate_category(message):
split_length=20
if(len(message)>=split_length):
message=message[0:split_length]
message=""+message+"..."
return message
#-----------------------------------------------------------------
#ツイートの縮退
#-----------------------------------------------------------------
def truncate_tweet(message):
split_length=50
if(len(message)>=split_length):
message=message[0:split_length]
message=""+message+"..."
return message