-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.py
58 lines (48 loc) · 1.88 KB
/
categories.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
import requests
import json
import config
categories_ids = {}
def process_category(category_obj):
for subcategory in category_obj['subcategories']:
if 'products' in subcategory['layout'] or 'marketing' in subcategory['layout']:
process_category(subcategory)
else:
category_id = category_obj.get('redirectCategoryId', category_obj['id'])
category_name = formatting_category_name(category_obj['name'])
categories_ids[category_id] = category_name
def categories_by_market(market):
global categories_ids
categories_ids = {}
session = requests.session()
response = session.get(config.URL_CATEGORY, headers=config.HEADERS, params=config.PARAMS)
json_loads = json.loads(response.text)
sections = json_loads['categories']
for section in sections: # woman, man, kid, beauty, origins
section_name = section.get('sectionName', section['name'])
if market == 'zara_w' and section_name == 'WOMAN':
process_category(section)
if market == 'zara_m' and section_name == 'MAN':
process_category(section)
if market == 'zara_k' and section_name == 'KID':
process_category(section)
if market == 'zara_b' and section_name == 'BEAUTY':
process_category(section)
if market == 'zara_o' and section_name == 'ZARA ORIGINS':
process_category(section)
return categories_ids
def formatting_category_name(name: str):
name = name.lower()
if 'zara' in name:
return name.replace('zara', '').strip()
if '\xa0' in name:
name = name.replace('\xa0', ' ')
if ' | ' in name:
return ' '.join(name.split(' | '))
if ' ' in name:
return name.replace(' ', '_')
if '-' in name:
return name.replace('-', '_')
return name
if __name__ == '__main__':
res = categories_by_market('zara_w')
print(res)