-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetcher.py
More file actions
78 lines (65 loc) · 2.27 KB
/
fetcher.py
File metadata and controls
78 lines (65 loc) · 2.27 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
# fetcher.py
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from config import DIVAR_URL, HEADERS, CITY_ID, THREAD_COUNT
API_SEARCH = DIVAR_URL
API_POST = "https://api.divar.ir/v8/posts-v2/web/"
def fetch_page(page):
body = {
"city_ids": [CITY_ID],
"source_view": "CATEGORY",
"pagination_data": {
"@type": "type.googleapis.com/post_list.PaginationData",
"page": page,
"layer_page": 1
},
"disable_recommendation": False,
"search_data": {
"form_data": {
"data": {
"category": {
"str": {"value": ""}
}
}
}
},
"server_payload": {"@type": "type.googleapis.com/widgets.SearchData.ServerPayload"}
}
try:
r = requests.post(API_SEARCH, headers=HEADERS, json=body, timeout=15)
if r.status_code != 200:
return page, None
return page, r.json()
except Exception:
return page, None
def fetch_pages(page_numbers):
results = {}
with ThreadPoolExecutor(max_workers=THREAD_COUNT) as executor:
futures = {executor.submit(fetch_page, p): p for p in page_numbers}
for future in as_completed(futures):
page = futures[future]
try:
pg, data = future.result()
results[page] = data or {}
except Exception:
results[page] = {}
return results
def fetch_post_description(token):
"""Fetch Divar post details and extract all DESCRIPTION_ROW text."""
try:
r = requests.get(API_POST + token, headers=HEADERS, timeout=12)
if r.status_code != 200:
return ""
data = r.json()
descriptions = []
for section in data.get("sections", []):
if section.get("section_name") != "DESCRIPTION":
continue
for w in section.get("widgets", []):
if w.get("widget_type") == "DESCRIPTION_ROW":
text = w.get("data", {}).get("text", "").strip()
if text:
descriptions.append(text)
return "\n\n".join(descriptions).strip()
except Exception:
return ""