diff --git a/weatherapi.py b/weatherapi.py index 4766dd4..4cfae47 100644 --- a/weatherapi.py +++ b/weatherapi.py @@ -1,109 +1,72 @@ -from __future__ import print_function -import time -import swagger_client -from swagger_client.rest import ApiException from pprint import pprint import requests - -configuration = swagger_client.Configuration() -configuration.api_key['key'] = '6eb9d101698a47a49db72950230706' - -api_instance = swagger_client.APIsApi(swagger_client.ApiClient(configuration)) -q = 'vit chennai' -dt = '2023-07-06' - -try: - # Astronomy API - api_response = api_instance.astronomy(q, dt) - #pprint(api_response) -except ApiException as e: - print("Exception when calling APIsApi->astronomy: %s\n" % e) - -def test_weather(location,date): - - api_instance = swagger_client.APIsApi(swagger_client.ApiClient(configuration)) - q = location - days = 56 - dt = date - unixdt = 56 - hour = 56 - lang = 'lang_example' +api_key = '6eb9d101698a47a49db72950230706' +base_url = 'https://api.weatherapi.com/v1' + +def astronomy(location, date): + endpoint = f'/astronomy.json?key={api_key}&q={location}&dt={date}' + url = base_url + endpoint + response = requests.get(url) + data = response.json() + pprint(data) + +def forecast_weather(location, days, date, unixdt, hour, lang): + endpoint = f'/forecast.json?key={api_key}&q={location}&days={days}&dt={date}&unixdt={unixdt}&hour={hour}&lang={lang}' + url = base_url + endpoint + response = requests.get(url) + data = response.json() + pprint(data) + x = data['location'] + y = data['current'] + pprint(x) try: - # Forecast API - api_response = api_instance.forecast_weather(q, days, dt=dt, unixdt=unixdt, hour=hour, lang=lang) - pprint(api_response) - x = api_response.location - y = api_response.current - pprint(x) - - except ApiException as e: - print("Exception when calling APIsApi->forecast_weather: %s\n" % e) - - try: - geeky_file = open('/Users/aman/AgriVision/media/location.txt', 'wt') - geeky_file.write(str(x)) - geeky_file.close() - + with open('media/location.txt', 'wt') as file: + file.write(str(x)) except: print("Unable to write to file") try: - geeky_file = open('/Users/aman/AgriVision/media/attributes.txt', 'wt') - geeky_file.write(str(y)) - geeky_file.close() - + with open('media/attributes.txt', 'wt') as file: + file.write(str(y)) except: print("Unable to write to file") - - lat= api_response.location.lat - lng= api_response.location.lon - response = requests.get('https://api.stormglass.io/v2/bio/point',params={'lat': lat,'lng': lng ,'params': 'soilMoisture'}, + lat = x['lat'] + lng = x['lon'] + response = requests.get('https://api.stormglass.io/v2/bio/point', params={'lat': lat, 'lng': lng, 'params': 'soilMoisture'}, headers={'Authorization': 'f6088ca4-1674-11ee-86b2-0242ac130002-f6088d12-1674-11ee-86b2-0242ac130002'}) json_data = response.json() - #print(json_data) + # print(json_data) try: - geeky_file = open('/Users/aman/AgriVision/media/full_info.txt', 'wt') - geeky_file.write(str(api_response)+str(json_data)) - geeky_file.close() - + with open('media/full_info.txt', 'wt') as file: + file.write(str(data) + str(json_data)) except: print("Unable to write to file") - - - - -api_instance = swagger_client.APIsApi(swagger_client.ApiClient(configuration)) -q = 'VIT Chennai' -dt = '2023-08-08' -lang = 'lang_example' - -try: - # Future API - api_response = api_instance.future_weather(q, dt=dt, lang=lang) - #pprint(api_response) -except ApiException as e: - print("Exception when calling APIsApi->future_weather: %s\n" % e) - - - - - - - - -api_instance = swagger_client.APIsApi(swagger_client.ApiClient(configuration)) +def future_weather(location, date, lang): + endpoint = f'/forecast.json?key={api_key}&q={location}&dt={date}&lang={lang}' + url = base_url + endpoint + response = requests.get(url) + data = response.json() + pprint(data) + +def realtime_weather(location, lang): + endpoint = f'/current.json?key={api_key}&q={location}&lang={lang}' + url = base_url + endpoint + response = requests.get(url) + data = response.json() + pprint(data) + +# Usage q = 'vit chennai' -lang = 'lang_example' -try: - # Realtime API - api_response = api_instance.realtime_weather(q, lang=lang) - # pprint(api_response) -except ApiException as e: - print("Exception when calling APIsApi->realtime_weather: %s\n" % e) +dt = '2023-07-06' +lang = 'lang_example' +astronomy(q, dt) +forecast_weather(q, 56, dt, 56, 56, lang) +future_weather(q, dt, lang) +realtime_weather(q, lang) \ No newline at end of file