def wrapper(*args, **kwargs):
     WINDOW.setProperty('{0}.IsBusy'.format(pane),
                        'true')  #@UndefinedVariable
     try:
         return f(*args, **kwargs)
     finally:
         WINDOW.clearProperty(
             '{0}.IsBusy'.format(pane))  #@UndefinedVariable
Beispiel #2
0
 def wrapper(*args, **kwargs):
     WINDOW.setProperty('{0}.IsBusy'.format(pane), 'true')#@UndefinedVariable
     try:
         return f(*args, **kwargs)
     finally:
         WINDOW.clearProperty('{0}.IsBusy'.format(pane))#@UndefinedVariable
Beispiel #3
0
def main():
    if ADDON.getSetting('EraseCache') == 'true':
        try:
            urlcache.URLCache(ADDON_DATA_PATH).erase()
        finally:
            ADDON.setSetting('EraseCache', 'false')#@UndefinedVariable

    if not API_KEY:
        raise Exception(_("No API Key."), _("Enter your Met Office API Key under settings."))

    if len(sys.argv) > 1 and sys.argv[1].isdigit():
        properties.observation()
    if not CURRENT_VIEW:
        properties.daily()
    elif CURRENT_VIEW == '3hourly':
        properties.threehourly()
    elif CURRENT_VIEW == 'forecastmap':
        properties.forecastlayer()
    elif CURRENT_VIEW == 'observationmap':
        properties.observationlayer()
    elif CURRENT_VIEW == 'text':
        properties.text()

    WINDOW.setProperty('WeatherProvider', ADDON.getAddonInfo('name'))#@UndefinedVariable
    WINDOW.setProperty('WeatherProviderLogo', ADDON_BANNER_PATH)#@UndefinedVariable
    WINDOW.setProperty('ObservationLocation', ADDON.getSetting('ObservationLocation'))#@UndefinedVariable
    WINDOW.setProperty('ForecastLocation', ADDON.getSetting('ForecastLocation'))#@UndefinedVariable
    WINDOW.setProperty('RegionalLocation', ADDON.getSetting('RegionalLocation'))#@UndefinedVariable
    WINDOW.setProperty('Location1', ADDON.getSetting('ForecastLocation'))#@UndefinedVariable
    WINDOW.setProperty('Locations', '1')#@UndefinedVariable

    #Explicitly set unused flags to false, so there are no unusual side
    #effects/residual data when moving from another weather provider.
    WINDOW.setProperty('Daily.IsFetched', '')#@UndefinedVariable
    WINDOW.setProperty('36Hour.IsFetched', '')#@UndefinedVariable
    WINDOW.setProperty('Weekend.IsFetched', '')#@UndefinedVariable
    WINDOW.setProperty('Hourly.IsFetched', '')#@UndefinedVariable
    WINDOW.setProperty('Map.IsFetched', '')#@UndefinedVariable
    WINDOW.setProperty('Today.Sunrise', '')#@UndefinedVariable
    WINDOW.setProperty('Today.Sunset', '')#@UndefinedVariable
    WINDOW.setProperty('Weather.CurrentView', '')#@UndefinedVariable
def threehourly():
    utilities.log( "Fetching 3 Hourly Forecast for '%s (%s)' from the Met Office..." % (FORECAST_LOCATION, FORECAST_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(THREEHOURLY_LOCATION_FORECAST_URL, threehourly_expiry)
        data=json.load(open(filename))
    try:
        dv = data['SiteRep']['DV']
        dataDate = utilities.strptime(dv.get('dataDate').rstrip('Z'), DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('3HourlyForecast.IssuedAt', dataDate.astimezone(TZ).strftime(ISSUEDAT_FORMAT))#@UndefinedVariable
        count = 0
        for period in dv['Location']['Period']:
            for rep in period['Rep']:
                #extra xbmc targeted info:
                weather_type = rep.get('W', 'na')
                WINDOW.setProperty('3Hourly%d.Outlook' % count, WEATHER_CODES.get(weather_type)[1])#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.WindSpeed' % count, rep.get('S', 'n/a'))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.WindDirection' % count, rep.get('D', 'na').lower())#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.GustSpeed' % count, rep.get('G', 'n/a'))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.UVIndex' % count, rep.get('U', 'n/a'))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.Precipitation' % count, rep.get('Pp'))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.OutlookIcon' % count, WEATHER_ICON_PATH % WEATHER_CODES.get(weather_type, 'na')[0])#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.Day' % count, time.strftime(SHORT_DAY_FORMAT, time.strptime(period.get('value'), DATAPOINT_DATE_FORMAT)))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.Time' % count, utilities.minutes_as_time(int(rep.get('$'))))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.ActualTemp' % count, utilities.rownd(utilities.localised_temperature(rep.get('T', 'na'))))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.ActualTempIcon' % count, rep.get('T', 'na'))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.FeelsLikeTemp' % count, utilities.rownd(utilities.localised_temperature(rep.get('F', 'na'))))#@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.FeelsLikeTempIcon' % count, rep.get('F', 'na'))#@UndefinedVariable
                count +=1
    except KeyError as e:
        e.args = ("Key Error in JSON File", "Key '{0}' not found while processing file from url:".format(e.args[0]), THREEHOURLY_LOCATION_FORECAST_URL)
        raise
    WINDOW.setProperty('3HourlyForecast.IsFetched', 'true')#@UndefinedVariable
def daily():
    utilities.log( "Fetching Daily Forecast for '%s (%s)' from the Met Office..." % (FORECAST_LOCATION, FORECAST_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(DAILY_LOCATION_FORECAST_URL, daily_expiry)
        data=json.load(open(filename))
    try:
        dv = data['SiteRep']['DV']
        dataDate = utilities.strptime(dv.get('dataDate').rstrip('Z'), DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('DailyForecast.IssuedAt', dataDate.astimezone(TZ).strftime(ISSUEDAT_FORMAT))#@UndefinedVariable
        for p, period in enumerate(dv['Location']['Period']):
            WINDOW.setProperty('Day%d.Title' %p, time.strftime(SHORT_DAY_FORMAT, time.strptime(period.get('value'), DATAPOINT_DATE_FORMAT)))#@UndefinedVariable
            for rep in period['Rep']:
                weather_type = rep.get('W', 'na')
                if rep.get('$') == 'Day':
                    WINDOW.setProperty('Day%d.HighTemp' %p, rep.get('Dm', 'na'))#@UndefinedVariable
                    WINDOW.setProperty('Day%d.HighTempIcon' %p, rep.get('Dm'))#@UndefinedVariable
                    WINDOW.setProperty('Day%d.Outlook' %p, WEATHER_CODES.get(weather_type)[1])#@UndefinedVariable
                    WINDOW.setProperty('Day%d.OutlookIcon' % p, WEATHER_ICON_PATH % WEATHER_CODES.get(weather_type, 'na')[0])#@UndefinedVariable
                    WINDOW.setProperty('Day%d.WindSpeed' % p,  rep.get('S', 'na'))#@UndefinedVariable
                    WINDOW.setProperty('Day%d.WindDirection' % p, rep.get('D', 'na').lower())#@UndefinedVariable

                elif rep.get('$') == 'Night':
                    WINDOW.setProperty('Day%d.LowTemp' %p, rep.get('Nm', 'na'))#@UndefinedVariable
                    WINDOW.setProperty('Day%d.LowTempIcon' %p, rep.get('Nm'))#@UndefinedVariable
    except KeyError as e:
        e.args = ("Key Error in JSON File", "Key '{0}' not found while processing file from url:".format(e.args[0]), DAILY_LOCATION_FORECAST_URL)
        raise

    WINDOW.setProperty('DailyForecast.IsFetched', 'true')#@UndefinedVariable
def observationlayer():
    utilities.log( "Fetching '{0}' Observation Map with index '{1}'...".format(OBSERVATIONMAP_LAYER_SELECTION, OBSERVATIONMAP_SLIDER))
    
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        surface = cache.get(GOOGLE_SURFACE, lambda x:  datetime.utcnow() + timedelta(days=30))
        marker = cache.get(GOOGLE_MARKER, lambda x:  datetime.utcnow() + timedelta(days=30))

        filename = cache.get(OBSERVATION_LAYER_CAPABILITIES_URL, observationlayer_capabilities_expiry)
        data = json.load(open(filename))
        #pull parameters out of capabilities file - TODO: consider using jsonpath here
        try:
            issuedat = utilities.strptime(data['Layers']['Layer'][-1]['Service']['Times']['Time'][0], DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
            for thislayer in data['Layers']['Layer']:
                if thislayer['@displayName'] == OBSERVATIONMAP_LAYER_SELECTION:
                    layer_name = thislayer['Service']['LayerName']
                    image_format = thislayer['Service']['ImageFormat']
                    times = thislayer['Service']['Times']['Time']
                    break
            else:
                raise Exception('Error', "Couldn't find layer '%s'" % OBSERVATIONMAP_LAYER_SELECTION)
        except KeyError as e:
            e.args = ("Key Error in JSON File", "Key '{0}' not found while processing file from url:".format(e.args[0]), OBSERVATION_LAYER_CAPABILITIES_URL)
            raise

        index = OBSERVATIONMAP_SLIDER
        if int(index) < 0:
            utilities.log('Slider is negative. Fetching with index 0')
            WINDOW.setProperty('ObservationMap.Slider', '0')#@UndefinedVariable
            index = '0'
        elif int(index) > len(times)-1:
            utilities.log('Slider exceeds available index range. Fetching with index {0}'.format(str(len(times)-1)))
            WINDOW.setProperty('ObservationMap.Slider', str(len(times)-1))#@UndefinedVariable
            index = str(len(times)-1)

        indexedtime = times[int(index)]
        maptime = utilities.strptime(indexedtime, DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)

        #get overlay using parameters from gui settings
        try:
            LayerURL = data['Layers']['BaseUrl']['$']
        except KeyError as e:
            e.args = ("Key Error in JSON File", "Key '{0}' not found while processing file from url:".format(e.args[0]), OBSERVATION_LAYER_CAPABILITIES_URL)
            raise

        url = LayerURL.format(LayerName=layer_name,
                                 ImageFormat=image_format,
                                 Time=indexedtime,
                                 key=API_KEY)
        layer = cache.get(url, lambda x: datetime.utcnow() + timedelta(days=1), image_resize)

        WINDOW.setProperty('ObservationMap.Surface', surface)#@UndefinedVariable
        WINDOW.setProperty('ObservationMap.Marker', marker)#@UndefinedVariable
        WINDOW.setProperty('ObservationMap.IssuedAt', issuedat.astimezone(TZ).strftime(ISSUEDAT_FORMAT))#@UndefinedVariable
        WINDOW.setProperty('ObservationMap.MapTime', maptime.astimezone(TZUK).strftime(MAPTIME_FORMAT))#@UndefinedVariable
        WINDOW.setProperty('ObservationMap.Layer', layer)#@UndefinedVariable
        WINDOW.setProperty('ObservationMap.IsFetched', 'true')#@UndefinedVariable
def observation():
    utilities.log( "Fetching Hourly Observation for '%s (%s)' from the Met Office..." % (OBSERVATION_LOCATION, OBSERVATION_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(HOURLY_LOCATION_OBSERVATION_URL, observation_expiry)
        data=json.load(open(filename))
    try:
        dv = data['SiteRep']['DV']
        dataDate = utilities.strptime(dv.get('dataDate').rstrip('Z'), DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('HourlyObservation.IssuedAt', dataDate.astimezone(TZ).strftime(ISSUEDAT_FORMAT))#@UndefinedVariable
        try:
            latest_period = dv['Location']['Period'][-1]
        except KeyError:
            latest_period = dv['Location']['Period']
        try:
            latest_obs = latest_period['Rep'][-1]
        except KeyError:
            latest_obs = latest_period['Rep']
        WINDOW.setProperty('Current.Condition', WEATHER_CODES[latest_obs.get('W', 'na')][1])#@UndefinedVariable
        WINDOW.setProperty('Current.Visibility', latest_obs.get('V', 'n/a'))#@UndefinedVariable
        WINDOW.setProperty('Current.Pressure', latest_obs.get('P', 'n/a'))#@UndefinedVariable
        WINDOW.setProperty('Current.Temperature', str(round(float(latest_obs.get('T', 'n/a')))).split('.')[0])#@UndefinedVariable
        WINDOW.setProperty('Current.Wind', latest_obs.get('S', 'n/a'))#@UndefinedVariable
        WINDOW.setProperty('Current.WindDirection', latest_obs.get('D', 'n/a'))#@UndefinedVariable
        WINDOW.setProperty('Current.WindGust', latest_obs.get('G', 'n/a'))#@UndefinedVariable
        WINDOW.setProperty('Current.OutlookIcon', '%s.png' % WEATHER_CODES[latest_obs.get('W', 'na')][0])#@UndefinedVariable
        WINDOW.setProperty('Current.FanartCode', '%s.png' % WEATHER_CODES[latest_obs.get('W','na')][0])#@UndefinedVariable
        WINDOW.setProperty('Current.DewPoint', str(round(float(latest_obs.get('Dp', 'n/a')))).split('.')[0])#@UndefinedVariable
        WINDOW.setProperty('Current.Humidity', str(round(float(latest_obs.get('H', 'n/a')))).split('.')[0])#@UndefinedVariable

        WINDOW.setProperty('HourlyObservation.IsFetched', 'true')#@UndefinedVariable
    except KeyError as e:
        e.args = ("Key Error in JSON File", "Key '{0}' not found while processing file from url:".format(e.args[0]), HOURLY_LOCATION_OBSERVATION_URL)
        raise
def text():
    utilities.log( "Fetching Text Forecast for '%s (%s)' from the Met Office..." % (REGIONAL_LOCATION, REGIONAL_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(TEXT_FORECAST_URL, text_expiry)
        data=json.load(open(filename))
    try:
        rf = data['RegionalFcst']
        issuedat = utilities.strptime(rf['issuedAt'].rstrip('Z'), DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('TextForecast.IssuedAt', issuedat.astimezone(TZ).strftime(ISSUEDAT_FORMAT))#@UndefinedVariable
        count = 0
        for period in rf['FcstPeriods']['Period']:
            #have to check type because json can return list or dict here
            if isinstance(period['Paragraph'],list):
                for paragraph in period['Paragraph']:
                    WINDOW.setProperty('Text.Paragraph%d.Title' % count, paragraph['title'].rstrip(':').lstrip('UK Outlook for'))#@UndefinedVariable
                    WINDOW.setProperty('Text.Paragraph%d.Content' % count, paragraph['$'])#@UndefinedVariable
                    count+=1
            else:
                WINDOW.setProperty('Text.Paragraph%d.Title' % count, period['Paragraph']['title'].rstrip(':').lstrip('UK Outlook for'))#@UndefinedVariable
                WINDOW.setProperty('Text.Paragraph%d.Content' % count, period['Paragraph']['$'])#@UndefinedVariable
                count+=1
    except KeyError as e:
        e.args = ("Key Error in JSON File", "Key '{0}' not found while processing file from url:".format(e.args[0]), TEXT_FORECAST_URL)
        raise
    WINDOW.setProperty('TextForecast.IsFetched', 'true')#@UndefinedVariable
Beispiel #9
0
def main():
    if ADDON.getSetting('EraseCache') == 'true':
        try:
            urlcache.URLCache(ADDON_DATA_PATH).erase()
        finally:
            ADDON.setSetting('EraseCache', 'false')  #@UndefinedVariable

    if not API_KEY:
        raise Exception(_("No API Key."),
                        _("Enter your Met Office API Key under settings."))

    properties.observation()
    properties.daily()
    properties.threehourly()

    WINDOW.setProperty('WeatherProvider',
                       ADDON.getAddonInfo('name'))  #@UndefinedVariable
    WINDOW.setProperty('WeatherProviderLogo',
                       ADDON_BANNER_PATH)  #@UndefinedVariable
    WINDOW.setProperty(
        'ObservationLocation',
        ADDON.getSetting('ObservationLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'Current.Location',
        ADDON.getSetting('ForecastLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'ForecastLocation',
        ADDON.getSetting('ForecastLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'RegionalLocation',
        ADDON.getSetting('RegionalLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'Location1', ADDON.getSetting('ForecastLocation'))  #@UndefinedVariable
    WINDOW.setProperty('Locations', '1')  #@UndefinedVariable

    #Explicitly set unused flags to false, so there are no unusual side
    #effects/residual data when moving from another weather provider.
    WINDOW.setProperty('36Hour.IsFetched', '')  #@UndefinedVariable
    WINDOW.setProperty('Weekend.IsFetched', '')  #@UndefinedVariable
    WINDOW.setProperty('Map.IsFetched', '')  #@UndefinedVariable
    WINDOW.setProperty('Today.Sunrise', '')  #@UndefinedVariable
    WINDOW.setProperty('Today.Sunset', '')  #@UndefinedVariable
    WINDOW.setProperty('Weather.CurrentView', '')  #@UndefinedVariable
def main():
    if ADDON.getSetting('EraseCache') == 'true':
        try:
            urlcache.URLCache(ADDON_DATA_PATH).erase()
        finally:
            ADDON.setSetting('EraseCache', 'false')#@UndefinedVariable

    if not API_KEY:
        raise Exception(_("No API Key."), _("Enter your Met Office API Key under settings."))

    if len(sys.argv) > 1 and sys.argv[1].isdigit():
        properties.observation()
    if not CURRENT_VIEW:
        properties.daily()
    elif CURRENT_VIEW == '3hourly':
        properties.threehourly()
    elif CURRENT_VIEW == 'forecastmap':
        properties.forecastlayer()
    elif CURRENT_VIEW == 'observationmap':
        properties.observationlayer()
    elif CURRENT_VIEW == 'text':
        properties.text()

    WINDOW.setProperty('WeatherProvider', ADDON.getAddonInfo('name'))#@UndefinedVariable
    WINDOW.setProperty('ObservationLocation', ADDON.getSetting('ObservationLocation'))#@UndefinedVariable
    WINDOW.setProperty('ForecastLocation', ADDON.getSetting('ForecastLocation'))#@UndefinedVariable
    WINDOW.setProperty('RegionalLocation', ADDON.getSetting('RegionalLocation'))#@UndefinedVariable
    WINDOW.setProperty('Location1', ADDON.getSetting('ObservationLocation'))#@UndefinedVariable
    WINDOW.setProperty('Locations', '1')#@UndefinedVariable
Beispiel #11
0
def daily():
    utilities.log(
        "Fetching Daily Forecast for '%s (%s)' from the Met Office..." %
        (FORECAST_LOCATION, FORECAST_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(DAILY_LOCATION_FORECAST_URL, daily_expiry)
        data = json.load(open(filename))
    try:
        dv = data['SiteRep']['DV']
        dataDate = utilities.strptime(
            dv.get('dataDate').rstrip('Z'),
            DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('DailyForecast.IssuedAt',
                           dataDate.astimezone(TZ).strftime(
                               ISSUEDAT_FORMAT))  #@UndefinedVariable
        for p, period in enumerate(dv['Location']['Period']):
            WINDOW.setProperty(
                'Day%d.Title' % p,
                time.strftime(
                    SHORT_DAY_FORMAT,
                    time.strptime(period.get('value'),
                                  DATAPOINT_DATE_FORMAT)))  #@UndefinedVariable
            for rep in period['Rep']:
                weather_type = rep.get('W', 'na')
                if rep.get('$') == 'Day':
                    WINDOW.setProperty('Day%d.HighTemp' % p,
                                       rep.get('Dm',
                                               'na'))  #@UndefinedVariable
                    WINDOW.setProperty('Day%d.HighTempIcon' % p,
                                       rep.get('Dm'))  #@UndefinedVariable
                    WINDOW.setProperty('Day%d.Outlook' % p,
                                       WEATHER_CODES.get(weather_type)
                                       [1])  #@UndefinedVariable
                    WINDOW.setProperty(
                        'Day%d.OutlookIcon' % p,
                        WEATHER_ICON_PATH % WEATHER_CODES.get(
                            weather_type, 'na')[0])  #@UndefinedVariable
                    WINDOW.setProperty('Day%d.WindSpeed' % p,
                                       rep.get('S', 'na'))  #@UndefinedVariable
                    WINDOW.setProperty(
                        'Day%d.WindDirection' % p,
                        rep.get('D', 'na').lower())  #@UndefinedVariable

                elif rep.get('$') == 'Night':
                    WINDOW.setProperty('Day%d.LowTemp' % p,
                                       rep.get('Nm',
                                               'na'))  #@UndefinedVariable
                    WINDOW.setProperty('Day%d.LowTempIcon' % p,
                                       rep.get('Nm'))  #@UndefinedVariable
    except KeyError as e:
        e.args = ("Key Error in JSON File",
                  "Key '{0}' not found while processing file from url:".format(
                      e.args[0]), DAILY_LOCATION_FORECAST_URL)
        raise

    WINDOW.setProperty('DailyForecast.IsFetched', 'true')  #@UndefinedVariable
Beispiel #12
0
def observationlayer():
    utilities.log("Fetching '{0}' Observation Map with index '{1}'...".format(
        OBSERVATIONMAP_LAYER_SELECTION, OBSERVATIONMAP_SLIDER))

    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        surface = cache.get(GOOGLE_SURFACE,
                            lambda x: datetime.utcnow() + timedelta(days=30))
        marker = cache.get(GOOGLE_MARKER,
                           lambda x: datetime.utcnow() + timedelta(days=30))

        filename = cache.get(OBSERVATION_LAYER_CAPABILITIES_URL,
                             observationlayer_capabilities_expiry)
        data = json.load(open(filename))
        #pull parameters out of capabilities file - TODO: consider using jsonpath here
        try:
            issuedat = utilities.strptime(
                data['Layers']['Layer'][-1]['Service']['Times']['Time'][0],
                DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
            for thislayer in data['Layers']['Layer']:
                if thislayer['@displayName'] == OBSERVATIONMAP_LAYER_SELECTION:
                    layer_name = thislayer['Service']['LayerName']
                    image_format = thislayer['Service']['ImageFormat']
                    times = thislayer['Service']['Times']['Time']
                    break
            else:
                raise Exception(
                    'Error', "Couldn't find layer '%s'" %
                    OBSERVATIONMAP_LAYER_SELECTION)
        except KeyError as e:
            e.args = (
                "Key Error in JSON File",
                "Key '{0}' not found while processing file from url:".format(
                    e.args[0]), OBSERVATION_LAYER_CAPABILITIES_URL)
            raise

        index = OBSERVATIONMAP_SLIDER
        if int(index) < 0:
            utilities.log('Slider is negative. Fetching with index 0')
            WINDOW.setProperty('ObservationMap.Slider',
                               '0')  #@UndefinedVariable
            index = '0'
        elif int(index) > len(times) - 1:
            utilities.log(
                'Slider exceeds available index range. Fetching with index {0}'
                .format(str(len(times) - 1)))
            WINDOW.setProperty('ObservationMap.Slider',
                               str(len(times) - 1))  #@UndefinedVariable
            index = str(len(times) - 1)

        indexedtime = times[int(index)]
        maptime = utilities.strptime(
            indexedtime, DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)

        #get overlay using parameters from gui settings
        try:
            LayerURL = data['Layers']['BaseUrl']['$']
        except KeyError as e:
            e.args = (
                "Key Error in JSON File",
                "Key '{0}' not found while processing file from url:".format(
                    e.args[0]), OBSERVATION_LAYER_CAPABILITIES_URL)
            raise

        url = LayerURL.format(LayerName=layer_name,
                              ImageFormat=image_format,
                              Time=indexedtime,
                              key=API_KEY)
        layer = cache.get(url, lambda x: datetime.utcnow() + timedelta(days=1),
                          image_resize)

        WINDOW.setProperty('ObservationMap.Surface',
                           surface)  #@UndefinedVariable
        WINDOW.setProperty('ObservationMap.Marker',
                           marker)  #@UndefinedVariable
        WINDOW.setProperty('ObservationMap.IssuedAt',
                           issuedat.astimezone(TZ).strftime(
                               ISSUEDAT_FORMAT))  #@UndefinedVariable
        WINDOW.setProperty('ObservationMap.MapTime',
                           maptime.astimezone(TZUK).strftime(
                               MAPTIME_FORMAT))  #@UndefinedVariable
        WINDOW.setProperty('ObservationMap.Layer', layer)  #@UndefinedVariable
        WINDOW.setProperty('ObservationMap.IsFetched',
                           'true')  #@UndefinedVariable
Beispiel #13
0
def text():
    utilities.log(
        "Fetching Text Forecast for '%s (%s)' from the Met Office..." %
        (REGIONAL_LOCATION, REGIONAL_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(TEXT_FORECAST_URL, text_expiry)
        data = json.load(open(filename))
    try:
        rf = data['RegionalFcst']
        issuedat = utilities.strptime(
            rf['issuedAt'].rstrip('Z'),
            DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('TextForecast.IssuedAt',
                           issuedat.astimezone(TZ).strftime(
                               ISSUEDAT_FORMAT))  #@UndefinedVariable
        count = 0
        for period in rf['FcstPeriods']['Period']:
            #have to check type because json can return list or dict here
            if isinstance(period['Paragraph'], list):
                for paragraph in period['Paragraph']:
                    WINDOW.setProperty(
                        'Text.Paragraph%d.Title' % count,
                        paragraph['title'].rstrip(':').lstrip(
                            'UK Outlook for'))  #@UndefinedVariable
                    WINDOW.setProperty('Text.Paragraph%d.Content' % count,
                                       paragraph['$'])  #@UndefinedVariable
                    count += 1
            else:
                WINDOW.setProperty(
                    'Text.Paragraph%d.Title' % count,
                    period['Paragraph']['title'].rstrip(':').lstrip(
                        'UK Outlook for'))  #@UndefinedVariable
                WINDOW.setProperty(
                    'Text.Paragraph%d.Content' % count,
                    period['Paragraph']['$'])  #@UndefinedVariable
                count += 1
    except KeyError as e:
        e.args = ("Key Error in JSON File",
                  "Key '{0}' not found while processing file from url:".format(
                      e.args[0]), TEXT_FORECAST_URL)
        raise
    WINDOW.setProperty('TextForecast.IsFetched', 'true')  #@UndefinedVariable
Beispiel #14
0
def observation():
    utilities.log(
        "Fetching Hourly Observation for '%s (%s)' from the Met Office..." %
        (OBSERVATION_LOCATION, OBSERVATION_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(HOURLY_LOCATION_OBSERVATION_URL,
                             observation_expiry)
        data = json.load(open(filename))
    try:
        dv = data['SiteRep']['DV']
        dataDate = utilities.strptime(
            dv.get('dataDate').rstrip('Z'),
            DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('HourlyObservation.IssuedAt',
                           dataDate.astimezone(TZ).strftime(
                               ISSUEDAT_FORMAT))  #@UndefinedVariable
        try:
            latest_period = dv['Location']['Period'][-1]
        except KeyError:
            latest_period = dv['Location']['Period']
        try:
            latest_obs = latest_period['Rep'][-1]
        except KeyError:
            latest_obs = latest_period['Rep']
        WINDOW.setProperty('Current.Condition', WEATHER_CODES[latest_obs.get(
            'W', 'na')][1])  #@UndefinedVariable
        WINDOW.setProperty('Current.Visibility',
                           latest_obs.get('V', 'n/a'))  #@UndefinedVariable
        WINDOW.setProperty('Current.Pressure',
                           latest_obs.get('P', 'n/a'))  #@UndefinedVariable
        WINDOW.setProperty('Current.Temperature',
                           str(round(float(latest_obs.get(
                               'T',
                               'n/a')))).split('.')[0])  #@UndefinedVariable
        WINDOW.setProperty('Current.Wind',
                           latest_obs.get('S', 'n/a'))  #@UndefinedVariable
        WINDOW.setProperty('Current.WindDirection',
                           latest_obs.get('D', 'n/a'))  #@UndefinedVariable
        WINDOW.setProperty('Current.WindGust',
                           latest_obs.get('G', 'n/a'))  #@UndefinedVariable
        WINDOW.setProperty(
            'Current.OutlookIcon', '%s.png' %
            WEATHER_CODES[latest_obs.get('W', 'na')][0])  #@UndefinedVariable
        WINDOW.setProperty(
            'Current.FanartCode', '%s.png' %
            WEATHER_CODES[latest_obs.get('W', 'na')][0])  #@UndefinedVariable
        WINDOW.setProperty('Current.DewPoint',
                           str(round(float(latest_obs.get(
                               'Dp',
                               'n/a')))).split('.')[0])  #@UndefinedVariable
        WINDOW.setProperty('Current.Humidity',
                           str(round(float(latest_obs.get(
                               'H',
                               'n/a')))).split('.')[0])  #@UndefinedVariable

        WINDOW.setProperty('HourlyObservation.IsFetched',
                           'true')  #@UndefinedVariable
    except KeyError as e:
        e.args = ("Key Error in JSON File",
                  "Key '{0}' not found while processing file from url:".format(
                      e.args[0]), HOURLY_LOCATION_OBSERVATION_URL)
        raise
Beispiel #15
0
def threehourly():
    utilities.log(
        "Fetching 3 Hourly Forecast for '%s (%s)' from the Met Office..." %
        (FORECAST_LOCATION, FORECAST_LOCATION_ID))
    with urlcache.URLCache(ADDON_DATA_PATH) as cache:
        filename = cache.get(THREEHOURLY_LOCATION_FORECAST_URL,
                             threehourly_expiry)
        data = json.load(open(filename))
    try:
        dv = data['SiteRep']['DV']
        dataDate = utilities.strptime(
            dv.get('dataDate').rstrip('Z'),
            DATAPOINT_DATETIME_FORMAT).replace(tzinfo=pytz.utc)
        WINDOW.setProperty('3HourlyForecast.IssuedAt',
                           dataDate.astimezone(TZ).strftime(
                               ISSUEDAT_FORMAT))  #@UndefinedVariable
        count = 0
        for period in dv['Location']['Period']:
            for rep in period['Rep']:
                #extra xbmc targeted info:
                weather_type = rep.get('W', 'na')
                WINDOW.setProperty(
                    '3Hourly%d.Outlook' % count,
                    WEATHER_CODES.get(weather_type)[1])  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.WindSpeed' % count,
                                   rep.get('S', 'n/a'))  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.WindDirection' % count,
                                   rep.get('D',
                                           'na').lower())  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.GustSpeed' % count,
                                   rep.get('G', 'n/a'))  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.UVIndex' % count,
                                   rep.get('U', 'n/a'))  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.Precipitation' % count,
                                   rep.get('Pp'))  #@UndefinedVariable
                WINDOW.setProperty(
                    '3Hourly%d.OutlookIcon' % count,
                    WEATHER_ICON_PATH % WEATHER_CODES.get(
                        weather_type, 'na')[0])  #@UndefinedVariable
                WINDOW.setProperty(
                    '3Hourly%d.Day' % count,
                    time.strftime(
                        SHORT_DAY_FORMAT,
                        time.strptime(
                            period.get('value'),
                            DATAPOINT_DATE_FORMAT)))  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.Time' % count,
                                   utilities.minutes_as_time(int(
                                       rep.get('$'))))  #@UndefinedVariable
                WINDOW.setProperty(
                    '3Hourly%d.ActualTemp' % count,
                    utilities.rownd(
                        utilities.localised_temperature(rep.get(
                            'T', 'na'))))  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.ActualTempIcon' % count,
                                   rep.get('T', 'na'))  #@UndefinedVariable
                WINDOW.setProperty(
                    '3Hourly%d.FeelsLikeTemp' % count,
                    utilities.rownd(
                        utilities.localised_temperature(rep.get(
                            'F', 'na'))))  #@UndefinedVariable
                WINDOW.setProperty('3Hourly%d.FeelsLikeTempIcon' % count,
                                   rep.get('F', 'na'))  #@UndefinedVariable
                count += 1
    except KeyError as e:
        e.args = ("Key Error in JSON File",
                  "Key '{0}' not found while processing file from url:".format(
                      e.args[0]), THREEHOURLY_LOCATION_FORECAST_URL)
        raise
    WINDOW.setProperty('3HourlyForecast.IsFetched',
                       'true')  #@UndefinedVariable
def main():
    if ADDON.getSetting('EraseCache') == 'true':
        try:
            urlcache.URLCache(ADDON_DATA_PATH).erase()
        finally:
            ADDON.setSetting('EraseCache', 'false')  #@UndefinedVariable

    if not API_KEY:
        raise Exception(_("No API Key."),
                        _("Enter your Met Office API Key under settings."))

    if len(sys.argv) > 1 and sys.argv[1].isdigit():
        properties.observation()
    if not CURRENT_VIEW:
        properties.daily()
    elif CURRENT_VIEW == '3hourly':
        properties.threehourly()
    elif CURRENT_VIEW == 'forecastmap':
        properties.forecastlayer()
    elif CURRENT_VIEW == 'observationmap':
        properties.observationlayer()
    elif CURRENT_VIEW == 'text':
        properties.text()

    WINDOW.setProperty('WeatherProvider',
                       ADDON.getAddonInfo('name'))  #@UndefinedVariable
    WINDOW.setProperty(
        'ObservationLocation',
        ADDON.getSetting('ObservationLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'ForecastLocation',
        ADDON.getSetting('ForecastLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'RegionalLocation',
        ADDON.getSetting('RegionalLocation'))  #@UndefinedVariable
    WINDOW.setProperty(
        'Location1',
        ADDON.getSetting('ObservationLocation'))  #@UndefinedVariable
    WINDOW.setProperty('Locations', '1')  #@UndefinedVariable