forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyext_answers.py
More file actions
220 lines (196 loc) · 9.85 KB
/
yext_answers.py
File metadata and controls
220 lines (196 loc) · 9.85 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import json
from typing import Any, Iterable
from urllib.parse import urlencode
from scrapy import Request
from scrapy.http import JsonRequest, Response
from scrapy.spiders import Spider
from locations.categories import Drink, Extras, PaymentMethods, apply_yes_no
from locations.dict_parser import DictParser
from locations.hours import OpeningHours
from locations.items import Feature
from locations.structured_data_spider import clean_facebook
GOOGLE_ATTRIBUTES_MAP = {
"accepts_reservations": Extras.RESERVATION,
"has_bar_onsite": Extras.BAR,
"has_delivery": Extras.DELIVERY,
"has_drive_through": Extras.DRIVE_THROUGH,
"has_high_chairs": Extras.HIGH_CHAIR,
"has_live_music": Extras.LIVE_MUSIC,
"has_restroom": Extras.TOILETS,
"has_seating_outdoors": Extras.OUTDOOR_SEATING,
"has_takeout": Extras.TAKEAWAY,
"has_wheelchair_accessible_restroom": Extras.TOILETS_WHEELCHAIR,
"has_wheelchair_accessible_parking": Extras.PARKING_WHEELCHAIR,
"pay_check": PaymentMethods.CHEQUE,
"pay_debit_card": PaymentMethods.DEBIT_CARDS,
"pay_mobile_nfc": PaymentMethods.CONTACTLESS,
"pay_credit_card": PaymentMethods.CREDIT_CARDS,
"requires_cash_only": PaymentMethods.CASH_ONLY,
"requires_reservations": Extras.RESERVATION_REQUIRED,
"serves_vegetarian": Extras.VEGETARIAN,
"serves_halal_food": Extras.HALAL,
"serves_liquor": Drink.LIQUOR,
"serves_coffee": Drink.COFFEE,
"serves_cocktails": Drink.COCKTAIL,
"serves_beer": Drink.BEER,
"serves_wine": Drink.WINE,
"welcomes_dogs": Extras.DOG,
}
GOOGLE_WHEELCHAIR_KEYS = [
"has_wheelchair_accessible_restroom",
"has_wheelchair_accessible_entrance",
"has_wheelchair_accessible_seating",
]
class YextAnswersSpider(Spider):
dataset_attributes = {"source": "api", "api": "yext"}
endpoint: str = "https://liveapi.yext.com/v2/accounts/me/answers/vertical/query"
api_key: str = ""
experience_key: str = ""
api_version: str = "20220511"
page_limit: int = 50
locale: str = "en"
environment: str = "PRODUCTION" # "STAGING" also used
feature_type: str = "locations" # "restaurants" also used
facet_filters: dict = {}
def make_request(self, offset: int) -> JsonRequest:
return JsonRequest(
url="{}?{}".format(
self.endpoint,
urlencode(
{
"experienceKey": self.experience_key,
"api_key": self.api_key,
"v": self.api_version,
"version": self.environment,
"locale": self.locale,
"verticalKey": self.feature_type,
"filters": json.dumps(
{"builtin.location": {"$near": {"lat": 0, "lng": 0, "radius": 50000000}}}
),
"limit": str(self.page_limit),
"offset": str(offset),
"source": "STANDARD",
"facetFilters": json.dumps(self.facet_filters),
}
),
),
meta={"offset": offset},
)
def start_requests(self) -> Iterable[Request]:
yield self.make_request(0)
def parse(self, response: Response, **kwargs: Any) -> Any:
for location in response.json()["response"]["results"]:
location = location["data"]
item = DictParser.parse(location)
item["branch"] = location.get("geomodifier")
self.parse_socials(item, location)
item["opening_hours"] = self.parse_opening_hours(location.get("hours"))
if delivery_hours := location.get("deliveryHours"):
item["extras"]["opening_hours:delivery"] = self.parse_opening_hours(delivery_hours).as_opening_hours()
if happy_hours := location.get("happyHours"):
item["extras"]["happy_hours"] = self.parse_opening_hours(happy_hours).as_opening_hours()
if drive_through_hours := location.get("driveThroughHours"):
item["extras"]["opening_hours:drive_through"] = self.parse_opening_hours(
drive_through_hours
).as_opening_hours()
self.parse_payment_methods(location, item)
self.parse_google_attributes(location, item)
yield from self.parse_item(location, item) or []
if len(response.json()["response"]["results"]) == self.page_limit:
yield self.make_request(response.meta["offset"] + self.page_limit)
@staticmethod
def parse_socials(item: Feature, location: dict) -> None:
"""
Parse contact and social media fields of information from the
supplied dictionary and add to the supplied item (Feature).
:param item: item to add contact and social media information to.
:param location: dictionary to parse information from.
:returns: None
"""
phones = []
for phone_type in ["localPhone", "mainPhone", "mobilePhone"]:
if phone := location.get(phone_type):
if isinstance(phone, dict):
phones.append(phone.get("number"))
elif isinstance(phone, str):
phones.append(phone)
if len(phones) > 0:
item["phone"] = "; ".join(phones)
if emails := location.get("emails"):
item["email"] = ";".join(emails)
if google_place_id := location.get("googlePlaceId"):
item["extras"]["ref:google"] = google_place_id
if twitter_handle := location.get("tritterHandle"):
item["twitter"] = twitter_handle
if instagram_handle := location.get("instagramHandle"):
item["extras"]["contact:instagram"] = instagram_handle
if fax_number := location.get("fax"):
item["extras"]["fax"] = fax_number
if "facebookVanityUrl" in location.keys():
item["facebook"] = clean_facebook(location["facebookVanityUrl"])
elif "facebookPageUrl" in location.keys():
item["facebook"] = clean_facebook(location["facebookPageUrl"])
if website_url_dict := location.get("websiteUrl"):
if website_url_dict.get("preferDisplayUrl"):
item["website"] = website_url_dict.get("displayUrl")
else:
item["website"] = website_url_dict.get("url")
if menu_url_dict := location.get("menuUrl"):
if menu_url_dict.get("preferDisplayUrl"):
item["extras"]["website:menu"] = menu_url_dict.get("displayUrl")
else:
item["extras"]["website:menu"] = menu_url_dict.get("url")
@staticmethod
def parse_opening_hours(hours: dict) -> OpeningHours | None:
"""
Parse opening hours from the supplied dictionary and return an
OpeningHours object.
:param hours: dictionary provided by a Yext API.
:returns: OpeningHours object if hours could be parsed, or None if the
supplied hours dictionary cnanot be parsed.
"""
if not hours:
return None
oh = OpeningHours()
for day, rule in hours.items():
if not isinstance(rule, dict):
continue
if day == "holidayHours":
continue
if rule.get("isClosed") is True:
oh.set_closed(day)
continue
for time in rule["openIntervals"]:
oh.add_range(day, time["start"], time["end"])
return oh
def parse_payment_methods(self, location: dict, item: Feature) -> None:
if payment_methods := location.get("paymentOptions"):
payment_methods = [p.lower().replace(" ", "") for p in payment_methods]
apply_yes_no(PaymentMethods.ALIPAY, item, "alipay" in payment_methods)
apply_yes_no(PaymentMethods.AMERICAN_EXPRESS, item, "americanexpress" in payment_methods)
apply_yes_no(PaymentMethods.APPLE_PAY, item, "applepay" in payment_methods)
apply_yes_no(PaymentMethods.CASH, item, "cash" in payment_methods)
apply_yes_no(PaymentMethods.CHEQUE, item, "check" in payment_methods)
apply_yes_no(PaymentMethods.CONTACTLESS, item, "contactlesspayment" in payment_methods)
apply_yes_no(PaymentMethods.DINERS_CLUB, item, "dinersclub" in payment_methods)
apply_yes_no(PaymentMethods.DISCOVER_CARD, item, "discover" in payment_methods)
apply_yes_no(PaymentMethods.GOOGLE_PAY, item, "googlepay" in payment_methods)
apply_yes_no(PaymentMethods.MASTER_CARD, item, "mastercard" in payment_methods)
apply_yes_no(PaymentMethods.SAMSUNG_PAY, item, "samsungpay" in payment_methods)
apply_yes_no(PaymentMethods.UNIONPAY, item, "chinaunionpay" in payment_methods)
apply_yes_no(PaymentMethods.VISA, item, "visa" in payment_methods)
apply_yes_no(PaymentMethods.WECHAT, item, "wechatpay" in payment_methods)
def parse_google_attributes(self, location: dict, item: Feature) -> None:
if google_attributes := location.get("googleAttributes"):
for key, attribute in GOOGLE_ATTRIBUTES_MAP.items():
if key in google_attributes:
apply_yes_no(attribute, item, google_attributes[key][0] == "true", False)
wheelchair_keys_present = [key for key in GOOGLE_WHEELCHAIR_KEYS if key in google_attributes]
if all([google_attributes[key][0] == "true" for key in wheelchair_keys_present]):
apply_yes_no(Extras.WHEELCHAIR, item, True, False)
elif any([google_attributes[key][0] == "true" for key in wheelchair_keys_present]):
apply_yes_no(Extras.WHEELCHAIR_LIMITED, item, True, False)
else:
apply_yes_no(Extras.WHEELCHAIR, item, True, False)
def parse_item(self, location: dict, item: Feature) -> Iterable[Feature]:
yield item