-
Notifications
You must be signed in to change notification settings - Fork 0
/
christofle.py
172 lines (152 loc) · 5.93 KB
/
christofle.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
# coding=utf-8
import json
import string
import re
import common as cm
import geosense as gs
__author__ = 'Zephyre'
db = None
url = 'http://www.christofle.com/oc-en/10-store-locations'
brand_id = 10085
brandname_e = u'Christofle'
brandname_c = u'法国昆庭'
def get_countries(data):
"""
返回国家列表
:rtype : [{'country_code':**, 'country':**}, ...]
:param data:
:return:
"""
url = data['url']
try:
html = cm.get_data(url)
except Exception:
print 'Error occured: %s' % url
dump_data = {'level': 1, 'time': cm.format_time(), 'data': {'url': url}, 'brand_id': brand_id}
cm.dump(dump_data)
return []
pat = '<option value="0">Choose a country</option>'
splits = [m.start() for m in re.finditer(pat, html)]
splits.append(-1)
sub_html = []
for i in xrange(len(splits) - 1):
sub_html.append(html[splits[i]:splits[i + 1]])
# 1:州信息
# s_map = [{'state_code':m[0], 'state':m[1].strip}
state_list = []
for m in re.findall(ur'<option value="(.+?)"\s*?>(.+?)</option>', sub_html[0][len(pat):]):
code = m[0].strip().upper()
state = m[1].strip().upper()
ret = gs.look_up(state, 2)
if ret is not None:
# state_list.append({'state': ret[0]['province_e'], 'state_code': ret[0]['state_code']})
state_list.append({'state': ret['name_e'], 'state_code': ret['code']})
else:
# state其实是写成是代码
for key in gs.province_map['data']:
state = gs.province_map['data'][key]
if state['code'] == code:
state = state['name_e']
state_list.append({'state': state, 'state_code': code})
break
# flag = True
# for s in gs.province_map:
# if 'state_code' in gs.province_map[s] and gs.province_map[s]['state_code'] == code:
# state = gs.province_map[s]['province_e']
# state_list.append({'state': state, 'state_code': code})
# flag = False
# break
# if flag:
# state_list.append({'state': 'HZZZZ', 'state_code': code})
# 2:国家信息
c_list = [{'country_code': m[0], 'country': m[1].strip(), 'url': url}
for m in re.findall(ur'<option value="([A-Z]{2})"\s*?>(.+?)</option>', sub_html[1])]
return c_list
def get_stores(data):
# data[StoreLocator][pays]=BO
url = data['url']
try:
html = cm.post_data(url, {'data[StoreLocator][pays]': data['country_code'],
'data[StoreLocator][ville]': '',
'data[StoreLocator][etat]': 0})
except Exception, e:
print 'Error occured: %s, %s' % (url, str(e))
dump_data = {'level': 1, 'time': cm.format_time(), 'data': {'url': url}, 'brand_id': brand_id}
cm.dump(dump_data)
return []
start = html.find('var markers_affiches')
if start == -1:
return []
start = html.find('[', start)
if start == -1:
return []
end = html.find(']', start)
html = html[start:end]
store_list = []
while True:
entry = cm.init_store_entry(brand_id, brandname_e, brandname_c)
sub, start, end = cm.extract_closure(html, ur'\{', ur'\}')
if end == 0:
break
js = json.loads(sub)
start = end
html = html[start:]
raw = js['StoreLocator']
entry[cm.name_e] = raw['name']
addr1 = raw['adress1']
addr2 = raw['adress2']
entry[cm.addr_e] = cm.reformat_addr(', '.join([addr1, addr2]))
entry[cm.zip_code] = raw['postcode']
entry[cm.city_e] = cm.extract_city(raw['city'])[0]
entry[cm.province_e] = raw['region']
entry[cm.tel] = raw['phone']
entry[cm.fax] = raw['fax']
entry[cm.email] = raw['email']
entry[cm.hours] = cm.reformat_addr(raw['opening'])
entry[cm.lat] = string.atof(raw['latitude'])
entry[cm.lng] = string.atof(raw['longitude'])
entry[cm.url] = raw['link'].replace('\\', '')
raw = js['Country']
entry[cm.country_e] = raw['name']
raw = js['StoreLocatorType']
entry[cm.store_type] = raw['name']
gs.field_sense(entry)
print '(%s / %d) Found store: %s, %s (%s, %s)' % (
brandname_e, brand_id, entry[cm.name_e], entry[cm.addr_e], entry[cm.country_e],
entry[cm.continent_e])
db.insert_record(entry, 'stores')
store_list.append(entry)
return store_list
def fetch(level=1, data=None, user='root', passwd=''):
def func(data, level):
"""
:param data:
:param level: 0:国家;1:城市;2:商店列表
"""
if level == 0:
# 国家
return [{'func': lambda data: func(data, 1), 'data': c} for c in get_countries(data)]
elif level == 1:
store_list = get_stores(data)
return [{'func': None, 'data': s} for s in store_list]
# # 城市列表
# return [{'func': lambda data: func(data, 2), 'data': s} for s in get_cities(data)]
# elif level == 2:
# # 商店列表
# return [{'func': lambda data: func(data, 3), 'data': s} for s in get_store_list(data)]
# elif level == 3:
# # 商店的具体信息
# store = get_store_details(data)
# return [{'func': None, 'data': store}]
else:
return []
global db
db = cm.StoresDb()
db.connect_db(user=user, passwd=passwd)
db.execute(u'DELETE FROM %s WHERE brand_id=%d' % ('stores', brand_id))
# Walk from the root node, where level == 1.
if data is None:
data = {'url': url}
results = cm.walk_tree({'func': lambda data: func(data, 0), 'data': data})
db.disconnect_db()
return results