<commit_before># -*- coding: utf-8 -*-

"""Weather data backed by the Dark Sky Forecast API.

https://developer.forecast.io/docs/v2
"""

import requests
from werkzeug.contrib.cache import SimpleCache
from werkzeug.http import parse_cache_control_header
from werkzeug.datastructures import ResponseCacheControl

CACHE_MIN_SECONDS = 2 * 60

cache = SimpleCache()


def build_url(config):
    return 'https://api.forecast.io/forecast/{key}/{loc}'.format(
        key=config.get('FORECAST_API_KEY'),
        loc=config.get('FORECAST_LOCATION'),
        exclude='minutely,hourly',
    )


def get_cache_timeout(r):
    # Cache the weather data at a rate that the upstream service dictates, but
    # that also keeps us easily within the 1000 requests per day free limit.
    header = r.headers.get('Cache-Control')
    control = parse_cache_control_header(header, cls=ResponseCacheControl)
    try:
        return max(control.max_age, CACHE_MIN_SECONDS)
    except TypeError:
        return CACHE_MIN_SECONDS


def get_weather_data(config):
    data = cache.get('forecast')
    if data is None:
        r = requests.get(build_url(config))
        data = r.json()
        cache.set('forecast', data, get_cache_timeout(r))
    return data
<commit_msg>Fixes #5: Update the weather API.<commit_after># -*- coding: utf-8 -*-

"""Weather data backed by the Dark Sky Forecast API.

https://darksky.net/dev/
"""

import requests
from werkzeug.contrib.cache import SimpleCache
from werkzeug.http import parse_cache_control_header
from werkzeug.datastructures import ResponseCacheControl

CACHE_MIN_SECONDS = 2 * 60

cache = SimpleCache()


def build_url(config):
    return 'https://api.darksky.net/forecast/{key}/{loc}'.format(
        key=config.get('FORECAST_API_KEY'),
        loc=config.get('FORECAST_LOCATION'),
        exclude='minutely,hourly',
    )


def get_cache_timeout(r):
    # Cache the weather data at a rate that the upstream service dictates, but
    # that also keeps us easily within the 1000 requests per day free limit.
    header = r.headers.get('Cache-Control')
    control = parse_cache_control_header(header, cls=ResponseCacheControl)
    try:
        return max(control.max_age, CACHE_MIN_SECONDS)
    except TypeError:
        return CACHE_MIN_SECONDS


def get_weather_data(config):
    data = cache.get('forecast')
    if data is None:
        r = requests.get(build_url(config))
        data = r.json()
        cache.set('forecast', data, get_cache_timeout(r))
    return data

{'fraction_non_alphanumeric': 0.10420841683366733, 'fraction_numerical': 0.007214428857715431, 'mean_word_length': 3.656716417910448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6087430', 'n_tokens_mistral': 800, 'n_tokens_neox': 778, 'n_words': 199}