forked from alltheplaces/alltheplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyith.py
More file actions
53 lines (40 loc) · 1.84 KB
/
yith.py
File metadata and controls
53 lines (40 loc) · 1.84 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
from typing import Iterable
from scrapy import Request, Selector
from locations.hours import OpeningHours
from locations.json_blob_spider import JSONBlobSpider
from locations.pipelines.address_clean_up import merge_address_lines
class YithSpider(JSONBlobSpider):
"""
Yith
https://yithemes.com/themes/plugins/yith-store-locator-wordpress/
To use, specify `allowed_domains`
"""
locations_key = "markers"
detection_rules = [
# DetectionRequestRule(
# url=r"^https?:\/\/(?P<allowed_domains__list>[A-Za-z0-9\-.]+)\/wp-admin\/admin-ajax\.php\?action=yith_sl_get_results(.*)$"
# )
]
def start_requests(self) -> Iterable[Request]:
yield Request(
f"https://{self.allowed_domains[0]}/wp-admin/admin-ajax.php?action=yith_sl_get_results&context=frontend&filters[radius][]=500"
)
def extract_address(self, item, feature):
sel = Selector(text=feature["pin_modal"])
item["addr_full"] = merge_address_lines(sel.xpath('//p[@class="store-address"]/text()').getall())
def extract_phone(self, item, feature):
sel = Selector(text=feature["pin_modal"])
item["phone"] = sel.xpath('//li[@class="store-phone"]/a/text()').get()
def extract_hours(self, item, feature):
sel = Selector(text=feature["pin_modal"])
item["opening_hours"] = OpeningHours()
item["opening_hours"].add_ranges_from_string(
" ".join(sel.xpath('//div[@class="store-description"]/p/text()').getall())
)
def post_process_item(self, item, response, feature):
item["branch"] = item.pop("name")
item["website"] = f"https://{self.allowed_domains[0]}/store-locator/{feature['slug']}/"
self.extract_address(item, feature)
self.extract_phone(item, feature)
self.extract_hours(item, feature)
yield item