forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcdonalds_it.py
51 lines (40 loc) · 1.98 KB
/
mcdonalds_it.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
48
49
50
51
from typing import Any
from urllib.parse import urljoin
import scrapy
from scrapy.http import Response
from locations.categories import Categories, apply_category
from locations.dict_parser import DictParser
from locations.hours import OpeningHours
class McdonaldsITSpider(scrapy.Spider):
name = "mcdonalds_it"
item_attributes = {"brand": "McDonald's", "brand_wikidata": "Q38076"}
start_urls = ["https://www.mcdonalds.it/static/json/store_locator.json"]
requires_proxy = True
def parse(self, response: Response, **kwargs: Any) -> Any:
for store in response.json()["sites"]:
store["street_address"] = store.pop("address")
item = DictParser.parse(store)
item["branch"] = item.pop("name")
item["website"] = urljoin("https://www.mcdonalds.it/ristorante/", store["uri"])
for opening_hours in store["opening_hours"]:
if opening_hours["name"] == "mccafe":
mccafe = item.deepcopy()
item["ref"] = "{}-mccafe".format(item["ref"])
mccafe["brand"] = "McCafé"
mccafe["brand_wikidata"] = "Q3114287"
apply_category(Categories.CAFE, mccafe)
mccafe["opening_hours"] = self.parse_opening_hours(opening_hours)
yield mccafe
elif opening_hours["name"] == "mcdrive":
item["extras"]["opening_hours:drive_through"] = self.parse_opening_hours(opening_hours)
elif opening_hours["name"] == "restaurant":
item["opening_hours"] = self.parse_opening_hours(opening_hours)
apply_category(Categories.FAST_FOOD, item)
yield item
def parse_opening_hours(self, rules: dict) -> str:
if rules["all24h"] is True:
return "24/7"
oh = OpeningHours()
for rule in rules["days"]:
oh.add_range(rule["name"], *rule["times"].split(","))
return oh.as_opening_hours()