-
Notifications
You must be signed in to change notification settings - Fork 0
/
getseal_background.py
executable file
·287 lines (244 loc) · 9.32 KB
/
getseal_background.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
# -*- coding: utf-8 -*-
__author__ = 'okazawa'
import os
import sys
import logging
from packages import bottle
from packages import pyservice
import datetime
import memcache
import urllib
import urllib2
#Setting this!
DEVELOP = 1
CASH= 1
HOME = ""
STATIC_ROOT = ""
LIST_URL2=""
RUN_HOST = ""
if DEVELOP == 1:
#local
HOME = "http://localhost:8080/"
STATIC_ROOT = "/Users/okazawa/src/get_seal_bottle/static"
LIST_URL2="/Users/okazawa/src/get_seal_bottle/test/input.csv"
LIST_URL3="/Users/okazawa/src/get_seal_bottle/test/input_dm.csv"
RUN_HOST = "localhost"
else:
#server
HOME = "https://hoge.jp/"
STATIC_ROOT = "/var/www/hoge.jp/bin/static"
LIST_URL2="/var/www/hoge/input.csv"
LIST_URL3="/var/www/hoge/input_dm.csv"
RUN_HOST = "127.0.0.1"
KEY1 = "URL_LIST1"
KEY2 = "URL_LIST2"
KEY3 = "URL_LIST3"
KEY_DAY = "DAY_KEY"
IMAGE_BASE1= "var objImg = document.createElement('img');var objLink = document.createElement('div');var f = new Function(\"location.href='https://trust.kyodo-d.jp/info/'\");objLink.setAttribute('onclick',\"window.open('https://trust.kyodo-d.jp/info/')\");objImg.src='"
IMAGE_BASE2= "'; objLink.appendChild(objImg);document.getElementById('"
IMAGE_BASE3= "').appendChild(objLink);"
#IMAGE_BASE1="alert('server:"
#IMAGE_BASE2="');"
#MyServer
IMAGE_S_K= HOME+"static/blue_seal_S.png"
IMAGE_M_K= HOME+"static/blue_seal_M.png"
IMAGE_L_K= HOME+"static/blue_seal_L.png"
#HostServer
IMAGE_S= HOME+"static/red_seal_S.png"
IMAGE_M= HOME+"static/red_seal_M.png"
IMAGE_L= HOME+"static/red_seal_L.png"
LIST_URL1="https://hoge/certifiedurl.txt"
USER_NAME="username"
BASIC_HOST="https://hoge"
PASSWORD="password"
CLIENT = memcache.Client(["localhost:11211"])
updateDateFlag1 = False
updateDateFlag2 = False
updateDateFlag3 = False
# monkey patching for BaseHTTPRequestHandler.log_message
def log_message(obj, format, *args):
logging.debug("def log_message")
logging.info("%s %s" % (obj.address_string(), format%args))
###
def cashBase(url,key):
returnFlag = False
day = ""
d = ""
global updateDateFlag1
global updateDateFlag2
global updateDateFlag3
if 0==CASH:
logging.debug("--Cash OFF--")
if KEY1 == key:
updateDateFlag1 = True
elif KEY2 == key:
updateDateFlag2 = True
elif KEY3 == key:
updateDateFlag3 = True
return returnFlag
if None != CLIENT.get(key+KEY_DAY):
day = CLIENT.get(key+KEY_DAY)
d = getDay()
else:
if KEY1 == key:
updateDateFlag1 = True
elif KEY2 == key:
updateDateFlag2 = True
elif KEY3 == key:
updateDateFlag3 = True
if day != d:
if KEY1 == key:
updateDateFlag1 = True
elif KEY2 == key:
updateDateFlag2 = True
elif KEY3 == key:
updateDateFlag3 = True
elif None != CLIENT.get(key):
urlList = CLIENT.get(key)
logging.debug("cash_"+key+"="+urlList)
if None != urlList and -1!=urlList.find(url):
returnFlag = True
return returnFlag
###
def findCash1(str):
logging.debug("def findCash1")
return cashBase(str,KEY1)
###
def findCash2(str):
logging.debug("def findCash2")
return cashBase(str,KEY2)
###
def findCash3(str):
logging.debug("def findCash3")
return cashBase(str,KEY3)
###
def getListBase(url,listUrl,code,key):
logging.debug("def getListBase")
src = ""
returnFlag = False
if LIST_URL1 == listUrl:
try:
#Basic Auth
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, BASIC_HOST, USER_NAME, PASSWORD)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(listUrl)
src = pagehandle.read()
except Exception as e:
logging.debug(e)
elif LIST_URL2 == listUrl:
try:
_src = urllib.urlopen(listUrl)
src = _src.read()
except Exception as e:
logging.debug(e)
finally:
_src.close()
elif LIST_URL3 == listUrl:
try:
_src = urllib.urlopen(listUrl)
src = _src.read()
except Exception as e:
logging.debug(e)
finally:
_src.close()
logging.debug("src= "+src)
_url = unicode(src, code)
CLIENT.set(key, _url)
CLIENT.set(key+KEY3, getDay())
if -1 != _url.find(url):
returnFlag = True
return returnFlag
###
def getList1(url):
logging.debug("def getList1")
return getListBase(url,LIST_URL1,'shift-jis',KEY1)
###
def getList2(url):
logging.debug("def getList2")
return getListBase(url,LIST_URL2,'utf_8',KEY2)
###
def getList3(url):
logging.debug("def getList3")
return getListBase(url,LIST_URL3,'utf_8',KEY3)
###
def getDay():
logging.debug("def getDay")
d = datetime.datetime.today()
return str(d).split()[0]
###
def getImageURL(imageSize, SealFlag,host_name):
logging.debug("def getImageURL")
if imageSize == "2" and SealFlag == 1:
return IMAGE_BASE1+IMAGE_S+IMAGE_BASE2+host_name+IMAGE_BASE3
elif imageSize == "1" and SealFlag == 1:
return IMAGE_BASE1+IMAGE_M+IMAGE_BASE2+host_name+IMAGE_BASE3
elif imageSize == "0" and SealFlag == 1:
return IMAGE_BASE1+IMAGE_L+IMAGE_BASE2+host_name+IMAGE_BASE3
elif imageSize == "2" and SealFlag == 2:
return IMAGE_BASE1+IMAGE_S_K+IMAGE_BASE2+host_name+IMAGE_BASE3
elif imageSize == "1" and SealFlag == 2:
return IMAGE_BASE1+IMAGE_M_K+IMAGE_BASE2+host_name+IMAGE_BASE3
elif imageSize == "0" and SealFlag == 2:
return IMAGE_BASE1+IMAGE_L_K+IMAGE_BASE2+host_name+IMAGE_BASE3
else:
return ""
###
@bottle.route('/getseal',method='GET',name="getseal")
def add():
logging.debug("@bottle.route('/getseal',method='GET',name='getseal')")
logging.debug(bottle.request.params.decode().get('dn'))
host_name = bottle.request.params.decode().get('dn')
imageSize = bottle.request.params.decode().get('sealid')
seal_type = bottle.request.params.decode().get('type')
imageURL = ""
if "dm"==seal_type:
if findCash3(host_name):#Find from MyServer Cash
logging.debug("From Cash of MyServer - Domain")
imageURL = getImageURL(imageSize,2,host_name)
elif updateDateFlag3 and getList3(host_name):#Find from MyServer List
logging.debug("From List of MyServer - Domain")
imageURL = getImageURL(imageSize,2,host_name)
return imageURL
else:
if findCash1(host_name):#Find from Red Cash
logging.debug("From Cash of Red - My page")
imageURL = getImageURL(imageSize,1,host_name)
elif findCash2(host_name):#Find from MyServer Cash
logging.debug("From Cash of MyServer - My page")
imageURL = getImageURL(imageSize,2,host_name)
elif updateDateFlag1 and getList1(host_name):#Find from Red List
logging.debug("From List of Red - My page")
imageURL = getImageURL(imageSize,1,host_name)
elif updateDateFlag2 and getList2(host_name):#Find from MyServer List
logging.debug("From List of MyServer - My page")
imageURL = getImageURL(imageSize,2,host_name)
return imageURL
###
@bottle.route('/static/<path:path>')
def static(path):
logging.debug("@bottle.route('/static/<path:path>')")
logging.info(path)
return bottle.static_file(path,root=STATIC_ROOT)
@bottle.error(404)
def error404(error):
logging.debug("@bottle.error(404)")
return 'Nothing here, sorry'
# Process to run
class BottleProcess(pyservice.Process):
pidfile = os.path.join(os.getcwd(), 'run/bottle.pid')
logfile = os.path.join(os.getcwd(), 'log/bottle.log')
def __init__(self):
super(BottleProcess, self).__init__()
from BaseHTTPServer import BaseHTTPRequestHandler
BaseHTTPRequestHandler.log_message = log_message
def run(self):
logging.info('Bottle {} server starting up'.format(bottle.__version__))
bottle.run(host='localhost', port=8080)
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] in 'start stop restart status'.split():
pyservice.service('getseal_background.BottleProcess', sys.argv[1])
else:
print 'usage: getseal_background <start,stop,restart,status>'