From 5f9f68fc74fce895886bd9e3f638ec2c5e0af0a8 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Mon, 4 Sep 2017 13:55:18 -0700 Subject: [PATCH 1/4] weather memes --- app/weather.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 app/weather.py diff --git a/app/weather.py b/app/weather.py new file mode 100644 index 00000000..3a5eddc6 --- /dev/null +++ b/app/weather.py @@ -0,0 +1,47 @@ +import requests +from pyowm import OWM + +def get_weather(zip_code): + """ + get_weather(zip_code) + + Returns the weather forecast for the selected zip code. Uses PythonOWM to + make life super easy. + """ + owm = OWM('593e0e182f9278a10443da354c4014db') + fc = owm.three_hours_forecast(zip_code) + f = fc.get_forecast() + + reg = f.get(0) + indicator = int(reg.get_reference_time(timeformat='iso')[11:13]) // 3 + + ret = '' + + ret = ret + ('Status: ' + reg.get_detailed_status() + ' ' + 'Current Temperature: ' + str(reg.get_temperature('fahrenheit').get('temp')) + '\n') + + for i in range((4 - indicator) % 8, len(f), 8): + high, low = 0, 100000 + for j in range(0, 8): + if (i + j < len(f)): + day = f.get(i + j) + else: + break + + temps = day.get_temperature('fahrenheit') + h = temps.get('temp_max') + l = temps.get('temp_min') + + if high < h: + high = h + if low > l: + low = l + + ret = ret + ('Status: ' + f.get(i).get_detailed_status() + ' ' + 'High: ' + str(high) + ' ' + 'Low: ' + str(low) + ' ' + 'Date: ' + + str(f.get(i).get_reference_time(timeformat='iso'))[:10] + '\n') + + # print(ret) + return ret From 2d3d8cf10d41a892afaa4801a7a97b6a51e6fb2a Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Mon, 4 Sep 2017 13:59:37 -0700 Subject: [PATCH 2/4] pyowm xd --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index f3aeb963..f24bbadb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.6.2-alpine3.6 LABEL name="Dragon Bot" RUN apk update && apk add --no-cache docker -RUN pip install requests discord.py docker wolframalpha +RUN pip install requests discord.py docker wolframalpha pyowm ADD app /app -CMD python app/dragon-bot.py \ No newline at end of file +CMD python app/dragon-bot.py From 9b42ebfb16d451bd25c52eb41b948b133abe04e5 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Mon, 4 Sep 2017 17:56:44 -0700 Subject: [PATCH 3/4] it works lol --- Dockerfile-test-env | 2 +- app/weather.py | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Dockerfile-test-env b/Dockerfile-test-env index 0f629ee9..fe563b6a 100644 --- a/Dockerfile-test-env +++ b/Dockerfile-test-env @@ -2,7 +2,7 @@ FROM python:3.6.2-alpine3.6 LABEL name="Dragon Bot Test environment" RUN apk update && apk add --no-cache vim docker -RUN pip install requests discord.py docker pylint wolframalpha +RUN pip install requests discord.py docker pylint wolframalpha pyowm ADD app /app RUN printf "\n\nTesting your python code for errors\n\n" && \ diff --git a/app/weather.py b/app/weather.py index 3a5eddc6..9dc50f07 100644 --- a/app/weather.py +++ b/app/weather.py @@ -1,24 +1,28 @@ import requests from pyowm import OWM -def get_weather(zip_code): +def get_weather(location): """ - get_weather(zip_code) + get_weather(location) Returns the weather forecast for the selected zip code. Uses PythonOWM to make life super easy. + + TODO: Replace basic code block with nice looking, bold text w/ emojis """ owm = OWM('593e0e182f9278a10443da354c4014db') - fc = owm.three_hours_forecast(zip_code) + fc = owm.three_hours_forecast(location) f = fc.get_forecast() reg = f.get(0) indicator = int(reg.get_reference_time(timeformat='iso')[11:13]) // 3 - ret = '' + ret = '```' - ret = ret + ('Status: ' + reg.get_detailed_status() + ' ' - 'Current Temperature: ' + str(reg.get_temperature('fahrenheit').get('temp')) + '\n') + ret = ret + ('\nStatus: ' + reg.get_detailed_status() + ' ' + + 'Current Temperature: ' + + str(reg.get_temperature('fahrenheit').get('temp')) + + 'F\n\nFive day forecast:\n') for i in range((4 - indicator) % 8, len(f), 8): high, low = 0, 100000 @@ -37,11 +41,8 @@ def get_weather(zip_code): if low > l: low = l - ret = ret + ('Status: ' + f.get(i).get_detailed_status() + ' ' - 'High: ' + str(high) + ' ' - 'Low: ' + str(low) + ' ' - 'Date: ' + - str(f.get(i).get_reference_time(timeformat='iso'))[:10] + '\n') + ret = ret + '{:<30}'.format('Status: ' + f.get(i).get_detailed_status()) + ret = ret + '{:<18}'.format('High: ' + str(high) + 'F ') + ret = ret + 'Low: ' + str(low) + 'F\n' - # print(ret) - return ret + return ret + '```' From 2fb964b5b3b6fa1d580194440c48f7e4fcb3e368 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Thu, 21 Sep 2017 17:50:36 -0700 Subject: [PATCH 4/4] weather 2.0 - added emojis, made things look nicer --- .gitlab-ci.yml | 4 +-- app/weather.py | 80 +++++++++++++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f7bb5601..fe87fbe2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ services: before_script: - apk add --no-cache python3 - - pip3 install pylint requests discord.py docker pylint wolframalpha + - pip3 install pylint requests discord.py docker pylint wolframalpha pyowm stages: - test @@ -28,4 +28,4 @@ build_and_push_container: only: - master tags: - - docker \ No newline at end of file + - docker diff --git a/app/weather.py b/app/weather.py index 9dc50f07..311ae871 100644 --- a/app/weather.py +++ b/app/weather.py @@ -1,48 +1,66 @@ import requests from pyowm import OWM +from pyowm import exceptions + +days_from_ind = {0: 'Today', 1: 'Tomorrow', 2: 'Day after tomorrow'} +emojis = {'clear sky': ':sunny:', 'scattered clouds': ':partly_sunny:', + 'light rain': ':white_sun_rain_cloud:', 'overcast clouds': ':cloud:', + 'broken clouds': ':white_sun_cloud:', 'moderate rain': ':cloud_with_rain:', + 'light snow': ':cloud_snow:', 'few clouds': ':white_sun_small_cloud:'} def get_weather(location): """ get_weather(location) - Returns the weather forecast for the selected zip code. Uses PythonOWM to + Returns the weather forecast for the selected location. Uses PythonOWM to make life super easy. TODO: Replace basic code block with nice looking, bold text w/ emojis """ - owm = OWM('593e0e182f9278a10443da354c4014db') - fc = owm.three_hours_forecast(location) - f = fc.get_forecast() + try: + openweathermap = OWM('593e0e182f9278a10443da354c4014db') + forecaster = openweathermap.three_hours_forecast(location) + forecast = forecaster.get_forecast() - reg = f.get(0) - indicator = int(reg.get_reference_time(timeformat='iso')[11:13]) // 3 + reg = forecast.get(0) + location = forecast.get_location().get_name() + indicator = int(reg.get_reference_time(timeformat='iso')[11:13]) // 3 - ret = '```' + curr_stat = '{}{}{}{}{}'.format(' \n***Status in ', location, ':*** *', + reg.get_detailed_status().title(), '*\n') + curr_temp = '{}{}{}'.format('**Current Temperature:** *', + str(reg.get_temperature('fahrenheit').get('temp')), + 'F*\n\n__**3 day forecast:**__\n') + forecast_str = '{}{}'.format(curr_stat, curr_temp) - ret = ret + ('\nStatus: ' + reg.get_detailed_status() + ' ' + - 'Current Temperature: ' + - str(reg.get_temperature('fahrenheit').get('temp')) + - 'F\n\nFive day forecast:\n') + ind = 0 + for i in range((4 - indicator) % 8, int((3 * len(forecast)) / 5), 8): + high, low = 0, 100000 + for j in range(0, 8): + if (i + j < (3 * len(forecast)) / 5): + day = forecast.get(i + j) + else: + break - for i in range((4 - indicator) % 8, len(f), 8): - high, low = 0, 100000 - for j in range(0, 8): - if (i + j < len(f)): - day = f.get(i + j) - else: - break + temps = day.get_temperature('fahrenheit') + h = temps.get('temp_max') + l = temps.get('temp_min') + print(forecast.get(i + j).get_detailed_status()) + if high < h: + high = h + if low > l: + low = l - temps = day.get_temperature('fahrenheit') - h = temps.get('temp_max') - l = temps.get('temp_min') + day = '{}{}{}'.format('***', days_from_ind.get(ind), '***\n') + weather_stat = '{}{}{}{}{}'.format('**Status:** *', + forecast.get(i).get_detailed_status().title(), + '* ', emojis.get(forecast.get(i).get_detailed_status()), '\n') + high_temp = '{}{}{}'.format('**High:** *', str(high), 'F*\n') + low_temp = '{}{}{}'.format('**Low:** *', str(low), 'F*\n\n') + forecast_str = '{}{}{}{}{}'.format( + forecast_str, day, weather_stat, high_temp, low_temp) + ind += 1 - if high < h: - high = h - if low > l: - low = l - - ret = ret + '{:<30}'.format('Status: ' + f.get(i).get_detailed_status()) - ret = ret + '{:<18}'.format('High: ' + str(high) + 'F ') - ret = ret + 'Low: ' + str(low) + 'F\n' - - return ret + '```' + return forecast_str + except exceptions.not_found_error.NotFoundError: + return 'Please input a valid location'