def gather_data(cursor, date, city):
    api_key = os.environ['APIXUKEY']
    # print (api_key)
    client = ApixuClient(api_key)

    now = date
    history = client.history(
        q=city, since=datetime.date(now.year, now.month, now.day))

    country = history['location']['country']
    name = history['location']['name']
    lat = history['location']['lat']
    lon = history['location']['lon']
    # print(history['forecast']['forecastday'])

    for day_forecast in history['forecast']['forecastday']:
        date = day_forecast['date']
        avgtemp_c = day_forecast['day']['avgtemp_c']

        cursor.execute(f'''INSERT INTO {TABLE_NAME} VALUES (
            '{date}',
            '{name}',
            '{country}',
            {lat},
            {lon},
            {avgtemp_c}
            )''')
Exemplo n.º 2
0
    def test_history():
        api_key = os.environ['APIXUKEY']
        client = ApixuClient(api_key)

        now = datetime.datetime.now()
        history = client.history(
            q='London',
            since=datetime.date(now.year, now.month, now.day),
            until=datetime.date(now.year, now.month, now.day),
        )
        validate(history, schema.read("history.json"))
Exemplo n.º 3
0
import datetime
import os

from apixu.client import ApixuClient

api_key = os.environ['APIXUKEY']
client = ApixuClient(api_key)

now = datetime.datetime.now()
history = client.history(q='London',
                         since=datetime.date(now.year, now.month, now.day))

print(history['location']['name'])

for day in history['forecast']['forecastday']:
    print(day['date'])
    print(day['day']['maxtemp_c'])
'''
{  
   "location":{  
      "name":"London",
      "region":"City of London, Greater London",
      "country":"United Kingdom",
      "lat":51.52,
      "lon":-0.11,
      "tz_id":"Europe/London",
      "localtime_epoch":1548103791,
      "localtime":"2019-01-21 20:49"
   },
   "forecast":{  
      "forecastday":[  
Exemplo n.º 4
0
    def test_history_invalid_until(self):
        client = ApixuClient()
        with self.assertRaises(ApixuException) as cm:
            client.history(until='notdate')

        self.assertEqual(cm.exception.code, 0)
Exemplo n.º 5
0
    def test_history_no_api_key(self):
        client = ApixuClient()
        with self.assertRaises(ApixuException) as cm:
            client.history()

        self.assertEqual(cm.exception.code, errors.API_KEY_NOT_PROVIDED)
Exemplo n.º 6
0
    def test_history_invalid_api_key(self):
        client = ApixuClient('INVALID_KEY')
        with self.assertRaises(ApixuException) as cm:
            client.history()

        self.assertEqual(cm.exception.code, errors.API_KEY_INVALID)