forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwoosmap.py
More file actions
64 lines (52 loc) · 2.65 KB
/
woosmap.py
File metadata and controls
64 lines (52 loc) · 2.65 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
from typing import Iterable
from scrapy import Spider
from scrapy.http import JsonRequest, Response
from locations.dict_parser import DictParser
from locations.hours import DAYS, OpeningHours
from locations.items import Feature
from locations.pipelines.address_clean_up import clean_address
# Documentation available at https://developers.woosmap.com/products/search-api/get-started/
#
# To use this spider, supply the API 'key' which typically starts
# with 'woos-' followed by a UUID. Also supply a value for 'origin'
# which is the HTTP 'Origin' header value, typically similar to
# 'https://www.brandname.example'.
class WoosmapSpider(Spider):
dataset_attributes = {"source": "api", "api": "woosmap.com"}
key: str = ""
origin: str = ""
def start_requests(self) -> Iterable[JsonRequest]:
yield JsonRequest(
url=f"https://api.woosmap.com/stores?key={self.key}&stores_by_page=300&page=1",
headers={"Origin": self.origin},
meta={"referrer_policy": "no-referrer"},
)
def parse(self, response: Response) -> Iterable[Feature | JsonRequest]:
if features := response.json()["features"]:
for feature in features:
item = DictParser.parse(feature["properties"])
item["street_address"] = clean_address(feature["properties"]["address"]["lines"])
item["geometry"] = feature["geometry"]
item["opening_hours"] = OpeningHours()
for day, rules in feature["properties"].get("opening_hours", {}).get("usual", {}).items():
for hours in rules:
if hours.get("all-day"):
start = "00:00"
end = "24:00"
else:
start = hours["start"]
end = hours["end"]
if day == "default":
item["opening_hours"].add_days_range(DAYS, start, end)
else:
item["opening_hours"].add_range(DAYS[int(day) - 1], start, end)
yield from self.parse_item(item, feature) or []
if pagination := response.json()["pagination"]:
if pagination["page"] < pagination["pageCount"]:
yield JsonRequest(
url=f'https://api.woosmap.com/stores?key={self.key}&stores_by_page=300&page={pagination["page"] + 1}',
headers={"Origin": self.origin},
meta={"referrer_policy": "no-referrer"},
)
def parse_item(self, item: Feature, feature: dict) -> Iterable[Feature]:
yield item