Repository: alltheplaces/alltheplaces

Issue:
Dennys
https://www.dennys.com/locations/


Discussion:

Resolving patch:
diff --git a/locations/spiders/dennys.py b/locations/spiders/dennys.py
new file mode 100644
--- /dev/null
+++ b/locations/spiders/dennys.py
@@ -0,0 +1,114 @@
+# -*- coding: utf-8 -*-
+import scrapy
+import json
+from scrapy.utils.url import urljoin_rfc
+from scrapy.utils.response import get_base_url
+
+from locations.items import GeojsonPointItem
+
+class DennysSpider(scrapy.Spider):
+    name = "dennys"
+    allowed_domains = ["locations.dennys.com"]
+    start_urls = (
+        'https://locations.dennys.com/',
+    )
+
+    def store_hours(self, store_hours):
+        day_groups = []
+        this_day_group = None
+        for day_info in store_hours:
+            day = day_info['day'][:2].title()
+
+            hour_intervals = []
+            for interval in day_info['intervals']:
+                f_time = str(interval['start']).zfill(4)
+                t_time = str(interval['end']).zfill(4)
+                hour_intervals.append('{}:{}-{}:{}'.format(
+                    f_time[0:2],
+                    f_time[2:4],
+                    t_time[0:2],
+                    t_time[2:4],
+                ))
+            hours = ','.join(hour_intervals)
+
+            if not this_day_group:
+                this_day_group = {
+                    'from_day': day,
+                    'to_day': day,
+                    'hours': hours
+                }
+            elif this_day_group['hours'] != hours:
+                day_groups.append(this_day_group)
+                this_day_group = {
+                    'from_day': day,
+                    'to_day': day,
+                    'hours': hours
+                }
+            elif this_day_group['hours'] == hours:
+                this_day_group['to_day'] = day
+
+        day_groups.append(this_day_group)
+
+        opening_hours = ""
+        if len(day_groups) == 1 and day_groups[0]['hours'] in ('00:00-23:59', '00:00-00:00'):
+            opening_hours = '24/7'
+        else:
+            for day_group in day_groups:
+                if day_group['from_day'] == day_group['to_day']:
+                    opening_hours += '{from_day} {hours}; '.format(**day_group)
+                elif day_group['from_day'] == 'Su' and day_group['to_day'] == 'Sa':
+                    opening_hours += '{hours}; '.format(**day_group)
+                else:
+                    opening_hours += '{from_day}-{to_day} {hours}; '.format(**day_group)
+            opening_hours = opening_hours[:-2]
+
+        return opening_hours
+
+    def parse_store(self, response):
+        properties = {
+            'addr:full': response.xpath('//span[@itemprop="streetAddress"]/text()')[0].extract(),
+            'addr:city': response.xpath('//span[@itemprop="addressLocality"]/text()')[0].extract(),
+            'addr:state': response.xpath('//span[@itemprop="addressRegion"]/text()')[0].extract(),
+            'addr:postcode': response.xpath('//span[@itemprop="postalCode"]/text()')[0].extract(),
+            'ref': response.url,
+            'website': response.url,
+        }
+        phone = response.xpath('//a[@class="c-phone-number-link c-phone-main-number-link"]/text()')[0].extract()
+        if phone:
+            properties['phone'] = phone
+
+        hours = json.loads(response.xpath('//div[@class="c-location-hours-details-wrapper js-location-hours"]/@data-days')[0].extract())
+
+        opening_hours = self.store_hours(hours) if hours else None
+        if opening_hours:
+            properties['opening_hours'] = opening_hours
+
+        lon_lat = [
+            float(response.xpath('//span/meta[@itemprop="longitude"]/@content')[0].extract()),
+            float(response.xpath('//span/meta[@itemprop="latitude"]/@content')[0].extract()),
+        ]
+
+        yield GeojsonPointItem(
+            properties=properties,
+            lon_lat=lon_lat,
+        )
+
+    def parse_state(self, response):
+        base_url = get_base_url(response)
+        urls = response.xpath('//div/ul[@class="storelist"]/li/a/@href').extract()
+        for path in urls:
+            yield scrapy.Request(urljoin_rfc(base_url, path), callback=self.parse_store)
+
+    def parse(self, response):
+        base_url = get_base_url(response)
+        urls = response.xpath('//a[@class="c-directory-list-content-item-link"]/@href').extract()
+        for path in urls:
+            if path.rsplit('/', 1)[0].isnumeric():
+                # If there's only one store, the URL will have a store number at the end
+                yield scrapy.Request(urljoin_rfc(base_url, path), callback=self.parse_store)
+            else:
+                yield scrapy.Request(urljoin_rfc(base_url, path))
+
+        urls = response.xpath('//a[@class="c-location-grid-item-link"]/@href').extract()
+        for path in urls:
+            yield scrapy.Request(urljoin_rfc(base_url, path), callback=self.parse_store)


Test patch: