/
michael_kors.py
84 lines (70 loc) · 2.73 KB
/
michael_kors.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
# coding=utf-8
import json
import string
import re
import common as cm
import geosense as gs
__author__ = 'Zephyre'
db = None
def fetch(level=1, data=None, user='root', passwd=''):
# Walk from the root node, where level == 1.
if data is None:
data = {'url': 'http://cms.destinationkors.com/store/get',
'brand_id': 10259, 'brandname_e': u'Michael Kors', 'brandname_c': u'迈克.柯尔'}
type_desc = ['Collection Boutique', 'Lifestyle', 'Outlet']
global db
db = cm.StoresDb()
db.connect_db(user=user, passwd=passwd)
db.execute(u'DELETE FROM %s WHERE brand_id=%d' % ('stores', data['brand_id']))
store_list = []
url = data['url']
try:
html = cm.get_data(url).decode('unicode_escape')
start = html.find('[')
if start == -1:
return []
js = json.loads(html[start:])
except Exception:
print 'Error occured: %s' % url
dump_data = {'level': 0, 'time': cm.format_time(), 'data': {'url': url}, 'brand_id': data['brand_id']}
cm.dump(dump_data)
return []
for s in js:
entry = cm.init_store_entry(data['brand_id'], data['brandname_e'], data['brandname_c'])
entry[cm.store_type] = type_desc[string.atoi(s['store_type']) - 1]
name = s['name'].strip()
if s['name2'].strip() != '':
name += ', ' + s['name2'].strip()
entry[cm.name_e] = name
addr = []
for i in xrange(3):
tmp = s['address%d' % (i + 1)].strip()
if tmp != '':
addr.append(tmp)
entry[cm.addr_e] = ', '.join(addr)
entry[cm.city_e] = cm.extract_city(s['city'])[0]
country = s['country']
ret = gs.look_up(country, 1)
if ret is not None:
country=ret['name_e']
entry[cm.country_e] = country
state = s['state'].strip().upper()
if country=='UNITED STATES' and state != '':
ret = gs.look_up(state, 2)
if ret is not None:
entry[cm.province_e] = ret['name_e']
else:
entry[cm.province_e] = state
entry[cm.zip_code] = s['zip']
entry[cm.tel] = s['phone']
entry[cm.hours] = s['hours']
entry[cm.lat] = string.atof(s['latitude'])
entry[cm.lng] = string.atof(s['longitude'])
gs.field_sense(entry)
print '(%s / %d) Found store: %s, %s (%s, %s)' % (data['brandname_e'], data['brand_id'],
entry[cm.name_e], entry[cm.addr_e], entry[cm.country_e],
entry[cm.continent_e])
store_list.append(entry)
db.insert_record(entry, 'stores')
db.disconnect_db()
return store_list