-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (40 loc) · 1.53 KB
/
app.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
import driver
import json
from multiprocessing.pool import Pool
if __name__ == '__main__':
# init ATB driver
atb = driver.AtbWebDriver()
n = 5 # number of proccess for multithreading
categories = atb.get_all_categories() # get all categories
chunks = [categories[i:i + n] for i in range(0, len(categories), n)] # cutting chunks for multithreading proccessing
data = []
l = len(chunks)
for ind, chunk in enumerate(chunks):
print(f"getting pagination. {ind+1}/{l}",end = " ")
p = Pool(processes=n)
m = p.map_async(atb.get_paginations, chunk) # run in async func for getting pagination
m.wait()
try:
res = m.get()
for i in res:
data.append(i)
print('succesfuly')
except Exception as e:
print('error: ', e)
atb.return_data(data)
chunks = [data[i:i + n] for i in range(0, len(data), n)]
l = len(chunks)
for ind, chunk in enumerate(chunks):
print(f"getting products. {ind+1}/{l}",end = " ")
p = Pool(processes=n)
m = p.map_async(atb.get_product, chunk) # run in async func for getting products by category
m.wait()
try:
res = m.get()
for i in res:
with open(f'{i["title"]}.json', 'w+', encoding='utf-8') as f:
json.dump(i, f, indent=6, ensure_ascii=False)
f.close()
print('succesfuly')
except Exception as e:
print('error: ', e)