forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreggs_gb.py
More file actions
52 lines (44 loc) · 1.96 KB
/
greggs_gb.py
File metadata and controls
52 lines (44 loc) · 1.96 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
from datetime import datetime
from typing import Any
from scrapy import Spider
from scrapy.http import Response
from locations.dict_parser import DictParser
from locations.hours import DAYS, OpeningHours
from locations.pipelines.address_clean_up import clean_address
class GreggsGBSpider(Spider):
name = "greggs_gb"
item_attributes = {"brand": "Greggs", "brand_wikidata": "Q3403981"}
start_urls = ["https://production-digital.greggs.co.uk/api/v1.0/shops"]
def parse(self, response: Response, **kwargs: Any) -> Any:
for store in response.json():
store["address"]["street_address"] = clean_address(
[
store["address"].pop("houseNumberOrName"),
store["address"].pop("streetName"),
]
)
item = DictParser.parse(store["address"])
if store["shopName"].startswith("Outlet: "):
item["name"] = "Greggs Outlet"
item["branch"] = store["shopName"].removeprefix("Outlet: ")
else:
item["name"] = "Greggs"
item["branch"] = store["shopName"]
item["phone"] = store["address"]["phoneNumber"]
item["ref"] = store["shopCode"]
item["website"] = f'https://www.greggs.com/shop-finder?shop-code={store["shopCode"]}'
item["opening_hours"] = self.decode_hours(store)
yield item
@staticmethod
def decode_hours(store):
oh = OpeningHours()
for r in store["tradingPeriods"]:
# Each day has a record like:
# {'openingTime': '2022-08-30T06:00:00Z', 'closingTime': '2022-08-30T18:00:00Z'}
# Do not believe their Zulu time modifier, it is local time.
oh.add_range(
DAYS[datetime.fromisoformat(r["openingTime"][0:10]).weekday()],
r["openingTime"][-9:-4],
r["closingTime"][-9:-4],
)
return oh.as_opening_hours()