diff --git a/__pycache__/weather.cpython-37.pyc b/__pycache__/weather.cpython-37.pyc new file mode 100644 index 0000000..5c7b81a Binary files /dev/null and b/__pycache__/weather.cpython-37.pyc differ diff --git a/taskrunner.py b/taskrunner.py index 0400153..b954dfe 100644 --- a/taskrunner.py +++ b/taskrunner.py @@ -1,16 +1,27 @@ from apscheduler.schedulers.blocking import BlockingScheduler -import time sched = BlockingScheduler() from weather import * - +import os @sched.scheduled_job('interval', hours=3) -def timed_job1(): - thedict=get_current_weather() - print('This job is run every three hours.') +def weatehrupdate(): + try: + connection = psycopg2.connect(user = os.environ["DB_USER"], + password = os.environ["DB_PASS"], + host = os.environ["DB_HOST"], + port = os.environ["DB_PORT"], + database = os.environ["DB_NAME"] ) -@sched.scheduled_job('interval', hours=3) + cursor = connection.cursor() + connection.autocommit = True + except (Exception, psycopg2.Error) as error : + print ("Error while connecting to PostgreSQL", error) + finally: + if(connection): + cursor.close() + connection.close() +@sched.scheduled_job('interval', hours=16) def timed_job2(): print('This is the number in number.txt: {0}') diff --git a/weather.py b/weather.py index cbcdba3..4e1b91d 100644 --- a/weather.py +++ b/weather.py @@ -1,24 +1,14 @@ import requests from flask import Flask, request, jsonify, redirect - -def get_current_weather_json(): - CLIENT_IP = request.remote_addr - GEO_KEY = "Api Key Goes Here" - WEATHER_KEY = 'Api Key Goes Here' - CURRENT_GEO_ENDPOINT = 'https://api.ipgeolocation.io/ipgeo?apiKey={0}&ip={1}&fields=geo'.format(GEO_KEY,CLIENT_IP) - r = requests.get(CURRENT_GEO_ENDPOINT) - thedict=r.json() +import os +def get_current_weather(zipcode): + WEATHER_KEY = os.environ["c"] try: zipcode=thedict['zipcode'] - countrycode=thedict['country_code2'] + countrycode="US" wr = requests.get('http://api.openweathermap.org/data/2.5/weather?zip={0},{1}&appid={2}'.format(zipcode,countrycode,WEATHER_KEY)) weatherdict=wr.json() except Exception: return dict() return weatherdict -def get_current_weather(): - if bool(get_current_weather_json()): - if get_current_weather_json()['cod'] == 200: - return get_current_weather_json()['weather'][0] - return dict() \ No newline at end of file