forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.py
More file actions
40 lines (31 loc) · 1.41 KB
/
stat.py
File metadata and controls
40 lines (31 loc) · 1.41 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
from typing import Any
from scrapy import Spider
from scrapy.http import Response
from locations.dict_parser import DictParser
from locations.hours import OpeningHours
class StatSpider(Spider):
"""
Unknown storefinder used by pet_valu_ca and paris_baguette. To use, set
start_urls to a list with the API endpoint stat/api/locations/search. Named
because of the "stat" in the API path.
"""
dataset_attributes = {"source": "api", "api": "stat"}
def parse(self, response: Response, **kwargs: Any) -> Any:
for store in response.json()["locations"]:
store.update(store.pop("businessAddress"))
item = DictParser.parse(store)
item["ref"] = store.get("clientLocationId") or store["locationId"]
item["lon"], item["lat"] = store["coordinates"]
item["phone"] = store["primaryPhone"]
item["website"] = response.urljoin(store["link"])
oh = OpeningHours()
for day, rule in store["hours"].items():
for times in rule["blocks"]:
time_to = times["to"]
if time_to == "2400":
time_to = "2359"
oh.add_range(day, times["from"], time_to, "%H%M")
item["opening_hours"] = oh
yield from self.post_process_item(item, response, store)
def post_process_item(self, item, response, store):
yield item