# -*- coding: utf-8 -*-
import scrapy
import re

from locations.items import GeojsonPointItem


class LaBreweriesSpider(scrapy.Spider):
    name = "labreweries"
    allowed_domains = ["labeerhop.com"]
    start_urls = (
        'http://labeerhop.com/breweries-sitemap.xml',
    )

    def store_hours(self, store_hours):
        day_groups = []
        this_day_group = None
        for day in store_hours:
            day = day.replace('  :-', ' 12:00 -')
            match = re.search(r'(\S*)\s+(\d{1,2}):(\d{2})\s*-\s*(\d{1,2}):(\d{2})', day)
            (dow, f_hr, f_min, t_hr, t_min) = match.groups()
            day_short = dow[:2]

            f_hr = int(f_hr)
            t_hr = int(t_hr)

            hours = '{:02d}:{}-{:02d}:{}'.format(
                f_hr,
                f_min,
                t_hr,
                t_min,
            )

            if not this_day_group:
                this_day_group = {
                    'from_day': day_short,
                    'to_day': day_short,
                    'hours': hours
                }
            elif this_day_group['hours'] != hours:
                day_groups.append(this_day_group)
                this_day_group = {
                    'from_day': day_short,
                    'to_day': day_short,
                    'hours': hours
                }
            elif this_day_group['hours'] == hours:
                this_day_group['to_day'] = day_short

        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 address(self, address):
        if not address:
            return None

        addr_tags = {
            "addr_full": address[0].split(',')[0].strip(),
            "city": address[0].split(',')[1].strip(),
            "state": address[0].split(' ')[-2].strip(),
            "postcode": address[0].split(' ')[-1].strip(),
        }

        return addr_tags

    def parse(self, response):
        response.selector.remove_namespaces()
        city_urls = response.xpath('//url/loc/text()').extract()
        open('/tmp/what1.txt','w').write(str(city_urls))
        for path in city_urls:
            yield scrapy.Request(
                path.strip(),
                callback=self.parse_store,
            )

    def parse_store(self, response):

        properties = {
            'website': response.xpath('//head/link[@rel="canonical"]/@href').extract_first(),
            'ref': response.xpath('/html/body/div[1]/div[1]/header/h1/text()').extract(),
            'opening_hours': response.css('#secondary').extract(),
            # 'lon': float(data['geo']['longitude']),
            # 'lat': float(data['geo']['latitude']),
        }

        address = self.address(response.xpath('/html/body/div[1]/div[1]/aside/address/text()').extract())
        if address:
            properties.update(address)

        # open('/tmp/test1.txt', 'w').write(str(properties))

        yield GeojsonPointItem(**properties)

@@ -450,16 +450,97 @@
 :00 -')%0A
+            day = day.split('%3Ch5%3EHours%3C/h5%3E')%5B1%5D.strip('%3Cbr%3E').strip('%3C/aside%3E')%0A
         
@@ -568,15 +568,15 @@
 (r'(
-%5CS*)%5Cs+
+closed%7C
 (%5Cd%7B
@@ -580,24 +580,19 @@
 %5Cd%7B1,2%7D)
-:(%5Cd%7B2%7D)
+%5CS.
 %5Cs*-%5Cs*(
@@ -603,23 +603,83 @@
 ,2%7D)
-:(%5Cd%7B2%7D)', 
+%5CS.)', day.lower())%0A            open('/tmp/test1.txt', 'w').write(str(
 day)
+)
 %0A   
@@ -703,26 +703,12 @@
 hr, 
-f_min, t_hr, t_min
+t_hr
 ) = 
@@ -845,21 +845,15 @@
 02d%7D
-:%7B%7D
 -%7B:02d%7D
-:%7B%7D
 '.fo
@@ -900,58 +900,12 @@
     
-f_min,%0A                t_hr,%0A                t_min
+t_hr
 ,%0A  
@@ -2757,65 +2757,8 @@
 t()%0A
-        open('/tmp/what1.txt','w').write(str(city_urls))%0A
     
@@ -3080,16 +3080,20 @@
  'ref': 
+str(
 response
@@ -3145,32 +3145,47 @@
 xt()').extract()
+).strip(%22%5B''%5D%22)
 ,%0A            'o
@@ -3198,16 +3198,35 @@
 _hours':
+ re.sub('%5Cs+', ' ',
  respons
@@ -3254,16 +3254,150 @@
 xtract()
+%5B0%5D.split('%3Ch5%3EHours%3C/h5%3E')%5B1%5D.replace('%3Cbr%3E','').replace('%3C/aside%3E','').replace('%5Ct',' ').replace('%5Cn','').replace('%5Cr',' ')).strip()
 ,%0A      
@@ -3435,32 +3435,52 @@
 %5D%5B'longitude'%5D),
+   # not lon on page
 %0A            # '
@@ -3516,16 +3516,37 @@
 tude'%5D),
+    # not lat on page
 %0A       
@@ -3719,69 +3719,8 @@
 s)%0A%0A
-        # open('/tmp/test1.txt', 'w').write(str(properties))%0A
 %0A   
@@ -3760,9 +3760,8 @@
 perties)
-%0A

Add files via upload