-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_parser.py
More file actions
119 lines (102 loc) · 3.58 KB
/
url_parser.py
File metadata and controls
119 lines (102 loc) · 3.58 KB
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
import urlparse
class UrlParser:
def __init__(self):
pass
def parse(self, url):
parsed_params = {}
params = urlparse.parse_qs(url)
parsed_params.update(self._get_category(params, urlparse.urlparse(url).path))
parsed_params.update(self._get_search(params))
parsed_params.update(self._get_offer_type(params))
parsed_params.update(self._get_price(params))
parsed_params.update(self._get_condition(params))
parsed_params.update(self._get_starting_time(params))
parsed_params.update(self._get_offer_options(params))
simple_params = ['city', 'state']
for simple_param in simple_params:
parsed_params.update(self._get_simple(params, simple_param))
return parsed_params
#TODO finish category
def _get_category(self, params, path):
out = {}
path_parts = path.split('-')
if 'id' in params:
out['category'] = params['id'][0]
elif len(path_parts) > 1:
out['category'] = path_parts[-1]
return out
def _get_search(self, params):
out = {}
if 'string' in params:
out['search'] = params['string'][0].decode('UTF-8')
return out
def _get_offer_type(self, params):
out = {}
if 'offerTypeBuyNow' in params or 'offerTypeAuction' in params:
out['offerType'] = []
if 'offerTypeBuyNow' in params:
out['offerType'].append('buyNow')
if 'offerTypeAuction' in params:
out['offerType'].append('auction')
return out
def _get_simple(self, params, param_name):
out = {}
if param_name in params:
out[param_name] = params[param_name][0].decode('UTF-8')
return out
def _get_price(self, params):
out = {}
if 'price_from' in params or 'price_to' in params:
out['price'] = {}
if 'price_from' in params:
out['price']['min'] = params['price_from'][0]
if 'price_to' in params:
out['price']['max'] = params['price_to'][0]
return out
def _get_condition(self, params):
out = {}
if 'buyNew' in params or 'buyUsed' in params:
out['condition'] = []
if 'buyNew' in params:
out['condition'].append('new')
if 'buyUsed' in params:
out['condition'].append('used')
return out
def _get_offer_options(self, params):
out = {}
options = []
if 'freeReturn' in params:
options.append('freeReturn')
if 'freeShipping' in params:
options.append('freeShipping')
if 'personal_rec' in params:
options.append('personalReceipt')
if 'vat_invoice' in params:
options.append('vatInvoice')
if 'generalDelivery_rec' in params:
options.append('generalDelivery')
if 'standard_allegro' in params:
options.append('standardAllegro')
if len(options):
out['offerOptions'] = options
return out
def _get_starting_time(self, params):
out = {}
starting_times = {
'1': '1h',
'2': '2h',
'3': '3h',
'4': '4h',
'5': '5h',
'6': '12h',
'7': '24h',
'8': '2d',
'9': '3d',
'10': '4d',
'11': '5d',
'12': '6d',
'13': '7d',
}
if 'startingTime' in params:
out['startingTime'] = starting_times[params['startingTime'][0]]
return out