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

from locations.items import GeojsonPointItem


class PublicStorageSpider(scrapy.Spider):
    name = "public_storage"
    allowed_domains = ["www.publicstorage.com"]
    start_urls = (
        'https://www.publicstorage.com/handlers/searchcoordinates.ashx?north=90.0&east=180.0&south=-90.0&west=-180.0',
    )

    def address(self, store):
        addr_tags = {
            "addr:full": store.get('address'),
            "addr:city": store.get('city'),
            "addr:state": store.get('state'),
            "addr:postcode": store.get('zip'),
        }

        return addr_tags

    def parse(self, response):
        data = json.loads(response.body_as_unicode())

        for store in data['response']['properties']['property']:
            yield scrapy.Request(
                'https://www.publicstorage.com/storage-location-details-modal.aspx?site_id={}&property_id={}'.format(
                    store['site_id'],
                    store['property_id'],
                ),
                meta=store,
                callback=self.parse_store,
            )

    def parse_store(self, response):
        properties = {
            "ref": response.meta.get('property_id'),
            "opening_hours": '; '.join(response.xpath('//time[@itemprop="openingHours"]/@datetime').extract())
        }

        address = self.address(response.meta)
        if address:
            properties.update(address)

        lat, lon = map(float, response.meta['lat_long'].split(', '))
        lon_lat = [
            lon,
            lat,
        ]

        yield GeojsonPointItem(
            properties=properties,
            lon_lat=lon_lat,
        )

@@ -46,18 +46,8 @@
 json
-%0Aimport re
 %0A%0Afr
@@ -366,798 +366,220 @@
 def 
-address(self, store):%0A        addr_tags = %7B%0A            %22addr:full%22: store.get('address'),%0A            %22addr:city%22: store.get('city'),%0A            %22addr:state%22: store.get('state'),%0A            %22addr:postcode%22: store.get('zip'),%0A        %7D%0A%0A        return addr_tags%0A%0A    def parse(self, response):%0A        data = json.loads(response.body_as_unicode())%0A%0A        for store in data%5B'response'%5D%5B'properties'%5D%5B'property'%5D:%0A            yield scrapy.Request(%0A                'https://www.publicstorage.com/storage-location-details-modal.aspx?site_id=%7B%7D&property_id=%7B%7D'.format(%0A                    store%5B'site_id'%5D,%0A                    store%5B'property_id'%5D,%0A                ),%0A                meta=store,%0A                callback=self.parse_store,%0A            
+parse(self, response):%0A        data = json.loads(response.body_as_unicode())%0A%0A        for store in data%5B'response'%5D%5B'properties'%5D%5B'property'%5D:%0A            lat, lon = map(float, store%5B'lat_long'%5D.split(', ')
 )%0A
-%0A
     
-def parse_store(self, response):%0A
     
@@ -613,28 +613,24 @@
     
+    
 %22ref%22: 
-response.meta
+store
 .get
@@ -646,16 +646,20 @@
 y_id'),%0A
+    
         
@@ -760,16 +760,17 @@
 tract())
+,
 %0A       
@@ -774,19 +774,16 @@
     
-%7D%0A%0A
         
 addr
@@ -782,195 +782,208 @@
     
+%22
 addr
-ess = self.address(response.meta)%0A        if address:%0A            properties.update(address)%0A%0A        lat, lon = map(float, response.meta%5B'lat_long'%5D.split(', '))%0A        lon_lat = %5B%0A
+_full%22: store.get('address'),%0A                %22city%22: store.get('city'),%0A                %22state%22: store.get('state'),%0A                %22postcode%22: store.get('zip'),%0A                %22lat%22: lat,%0A   
     
@@ -985,24 +985,32 @@
             
+  %22lon%22:
  lon,%0A      
@@ -1019,24 +1019,15 @@
     
-lat,
+%7D%0A
 %0A    
-    %5D%0A%0A
     
@@ -1057,79 +1057,18 @@
 tem(
-%0A            properties=properties,%0A            lon_lat=lon_lat,%0A        
+**properties
 )%0A

Update Public Storage spider