forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreshop.py
More file actions
48 lines (35 loc) · 1.78 KB
/
freshop.py
File metadata and controls
48 lines (35 loc) · 1.78 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
from scrapy import Spider
from scrapy.http import JsonRequest
from locations.categories import Extras, apply_yes_no
from locations.dict_parser import DictParser
from locations.hours import OpeningHours
# To use this store finder, specify the brand/application key using
# the "app_key" attribute of this class. You may need to define a
# parse_item function to extract additional location data and to
# make corrections to automatically extracted location data.
class FreshopSpider(Spider):
dataset_attributes = {"source": "api", "api": "freshop.com"}
app_key: str = ""
location_type_ids: list[str] = ["1567647"]
def start_requests(self):
yield JsonRequest(url=f"https://api.freshop.com/1/stores?app_key={self.app_key}")
def parse(self, response):
for location in response.json()["items"]:
if location.get("type_id") not in self.location_type_ids or not location.get("has_address"):
continue
if "COMING SOON" in location.get("hours_md", "").upper():
continue
item = DictParser.parse(location)
if location.get("store_number"):
item["ref"] = location["store_number"]
item["street_address"] = ", ".join(
filter(None, [location.get("address_1"), location.get("address_2"), location.get("address_3")])
)
if location.get("hours_md"):
item["opening_hours"] = OpeningHours()
item["opening_hours"].add_ranges_from_string(location["hours_md"])
if "has_delivery" in location.keys():
apply_yes_no(Extras.DELIVERY, item, location.get("has_delivery"), False)
yield from self.parse_item(item, location) or []
def parse_item(self, item, location):
yield item