# -*- coding: utf-8 -*-
import json
from urllib.parse import urlencode

import scrapy

from locations.items import GeojsonPointItem
from locations.hours import OpeningHours

DAY_MAPPING = {'Monday': 'Mo',
               'Tuesday': 'Tu',
               'Wednesday': 'We',
               'Thursday': 'Th',
               'Friday': 'Fr',
               'Saturday': 'Sa',
               'Sunday': 'Su'}

BASE_URL = 'https://onmyj41p3c.execute-api.us-west-2.amazonaws.com/prod/getStoresByCoordinates?'


class TMobileUSSpider(scrapy.Spider):
    name = "tmobile_us"
    item_attributes = { 'brand': "T-Mobile" }
    allowed_domains = ["www.t-mobile.com"]
    download_delay = 0.2

    def parse_hours(self, store_hours):
        opening_hours = OpeningHours()
        if store_hours is None:
            return

        for store_day in store_hours:
            day = DAY_MAPPING[store_day.get("day")]
            open_time = store_day.get("opens")
            close_time = store_day.get("closes")
            if open_time is None and close_time is None:
                continue
            opening_hours.add_range(day=day,
                                    open_time=open_time,
                                    close_time=close_time,
                                    time_format='%H:%M'
                                    )

        return opening_hours.as_opening_hours()

    def start_requests(self):
        url = BASE_URL

        with open('./locations/searchable_points/us_centroids_25mile_radius.csv') as points:

            next(points)  # Ignore the header
            for point in points:
                _, lat, lon = point.strip().split(',')

                params = {
                    'latitude': '{}'.format(lat),
                    'longitude': '{}'.format(lon),
                    'count': '1000',
                    'radius': '25',
                    'ignoreLoadingBar': 'false'
                }

                yield scrapy.http.Request(url + urlencode(params), callback=self.parse)

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

        for store in data:
            properties = {
                'name': store["name"],
                'ref': store["id"],
                'addr_full': store["location"]["address"]["streetAddress"],
                'city': store["location"]["address"]["addressLocality"],
                'state': store["location"]["address"]["addressRegion"],
                'postcode': store["location"]["address"]["postalCode"],
                'phone': store.get("telephone"),
                'website': store.get("url") or response.url,
                'lat': float(store["location"]["latitude"]),
                'lon': float(store["location"]["longitude"]),
            }

            hours = self.parse_hours(store.get("hours", []))
            if hours:
                properties["opening_hours"] = hours

            yield GeojsonPointItem(**properties)

locations/spiders/tmobile_us.py
# -*- coding: utf-8 -*-
import json
from urllib.parse import urlencode

import scrapy

from locations.items import GeojsonPointItem
from locations.hours import OpeningHours

DAY_MAPPING = {'Monday': 'Mo',
               'Tuesday': 'Tu',
               'Wednesday': 'We',
               'Thursday': 'Th',
               'Friday': 'Fr',
               'Saturday': 'Sa',
               'Sunday': 'Su'}

BASE_URL = 'https://onmyj41p3c.execute-api.us-west-2.amazonaws.com/prod/v2.1/getStoresByCoordinates?'


class TMobileUSSpider(scrapy.Spider):
    name = "tmobile_us"
    item_attributes = { 'brand': "T-Mobile" }
    allowed_domains = ["www.t-mobile.com"]
    download_delay = 0.2

    def parse_hours(self, store_hours):
        opening_hours = OpeningHours()
        if store_hours is None:
            return

        for store_day in store_hours:
            day = DAY_MAPPING[store_day.get("day")]
            open_time = store_day.get("opens")
            close_time = store_day.get("closes")
            if open_time is None and close_time is None:
                continue
            opening_hours.add_range(day=day,
                                    open_time=open_time,
                                    close_time=close_time,
                                    time_format='%H:%M'
                                    )

        return opening_hours.as_opening_hours()

    def start_requests(self):
        url = BASE_URL

        with open('./locations/searchable_points/us_centroids_25mile_radius.csv') as points:

            next(points)  # Ignore the header
            for point in points:
                _, lat, lon = point.strip().split(',')

                params = {
                    'latitude': '{}'.format(lat),
                    'longitude': '{}'.format(lon),
                    'count': '1000',
                    'radius': '25',
                    'ignoreLoadingBar': 'false'
                }

                yield scrapy.http.Request(url + urlencode(params), callback=self.parse)

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

        for store in data:
            properties = {
                'name': store["name"],
                'ref': store["id"],
                'addr_full': store["location"]["address"]["streetAddress"],
                'city': store["location"]["address"]["addressLocality"],
                'state': store["location"]["address"]["addressRegion"],
                'postcode': store["location"]["address"]["postalCode"],
                'phone': store.get("telephone"),
                'website': store.get("url") or response.url,
                'lat': float(store["location"]["latitude"]),
                'lon': float(store["location"]["longitude"]),
            }

            hours = self.parse_hours(store.get("hours", []))
            if hours:
                properties["opening_hours"] = hours

            yield GeojsonPointItem(**properties)

locations/spiders/tmobile_us.py
diff --git a/locations/spiders/tmobile_us.py b/locations/spiders/tmobile_us.py
index 3ddcdefaae3..fc16eb0e2bd 100644
--- a/locations/spiders/tmobile_us.py
+++ b/locations/spiders/tmobile_us.py
@@ -15,7 +15,7 @@
                'Saturday': 'Sa',
                'Sunday': 'Su'}
 
-BASE_URL = 'https://onmyj41p3c.execute-api.us-west-2.amazonaws.com/prod/getStoresByCoordinates?'
+BASE_URL = 'https://onmyj41p3c.execute-api.us-west-2.amazonaws.com/prod/v2.1/getStoresByCoordinates?'
 
 
 class TMobileUSSpider(scrapy.Spider):

Spider tmobile_us is broken
During the global build at 2021-06-30-14-42-26, spider **tmobile_us** failed with **7563 features** and **2 errors**.

Here's [the log](https://data.alltheplaces.xyz/runs/2021-06-30-14-42-26/logs/tmobile_us.txt) and [the output](https://data.alltheplaces.xyz/runs/2021-06-30-14-42-26/output/tmobile_us.geojson) ([on a map](https://data.alltheplaces.xyz/map.html?show=https://data.alltheplaces.xyz/runs/2021-06-30-14-42-26/output/tmobile_us.geojson))
