Examples

Basic client

import os
import speck

from datetime import datetime

def main():
    cl = speck.Client(
        os.environ['WEATHERAPI_TOKEN'] # Get from https://www.weatherapi.com/my/
    )

    while input('Continue? [y/n] ').lower() in ['yes', 'y']:
        location = input('Location: ')

        try:
            start = datetime.now()
            (current, forecast) = cl.forecast(location)
            stop = datetime.now()
        except speck.errors.WeatherApiError as e:
            print(e.message)

        print('---------------------')
        print(f'Query: {(stop - start).total_seconds()}')
        print(f'Current Temperature: {current.temp_c}°C')
        print(f'Temperature tomorrow: {forecast[0].day.avgtemp_c}°C')
        print('---------------------')

if __name__ == '__main__':
    main()

Cached client

import os
import speck

from datetime import datetime

def main():
    cl = speck.Client(
        os.environ['WEATHERAPI_TOKEN'],
        use_cache=True # as easy as enabling this flag
    )

    while input('Continue? [y/n] ').lower() in ['yes', 'y']:
        location = input('Location: ')

        try:
            start = datetime.now()
            (current, forecast) = cl.forecast(location)
            stop = datetime.now()
        except speck.errors.WeatherApiError as e:
            print(e.message)

        print('---------------------')
        print(f'Query: {(stop - start).total_seconds()}')
        print(f'Current Temperature: {current.temp_c}°C')
        print(f'Temperature tomorrow: {forecast[0].day.avgtemp_c}°C')
        print('---------------------')

if __name__ == '__main__':
    main()

Equivalent code without speck for comparison

Cached client

import os
import zlib
import pickle
from pathlib import Path

from datetime import datetime

import requests

BASE = "https://api.weatherapi.com/v1"

def display_details(response, query):
    if 'error' in response:
        print(response["error"]["message"])

    print('---------------------')
    print(f'Query: {query}')
    print(f'Current Temperature: {response["current"]["temp_c"]}°C')
    print(f'Temperature tomorrow: {response["forecast"]["forecastday"][0]["day"]["avgtemp_c"]}°C')
    print('---------------------')

def main():
    session = requests.Session()

    token = os.environ['WEATHERAPI_TOKEN']

    Path('.cache').mkdir(parents=True, exist_ok=True)

    while input('Continue? [y/n] ').lower() in ['yes', 'y']:
        location = input('Location: ')

        data = None

        for i in os.listdir('.cache'):
            if i == f'{location.lower()}-{datetime.now().strftime("%Y-%m-%d %H-%M")[:-1]}.dat':
                with open(i, 'rb') as f:
                    data = pickle.loads(zlib.decompress(pickle.load(f)))

        if not data:
            start = datetime.now()
            data = session \
                .get(f'{BASE}/forecast.json?key={token}&q={location}', timeout=(5, 5)) \
                .json()
            stop = datetime.now()

            with open(f'.cache/{location.lower()}-{datetime.now().strftime("%Y-%m-%d %H-%M")[:-1]}.dat', 'wb') as f:
                pickle.dump(zlib.compress(pickle.dumps(data)), f)

        display_details(data, (stop - start).total_seconds())

if __name__ == '__main__':
    main()