weather 2.0 - added emojis, made things look nicer

This commit is contained in:
Jason Ji 2017-09-21 17:50:36 -07:00
parent 9b42ebfb16
commit 2fb964b5b3
2 changed files with 51 additions and 33 deletions

View File

@ -4,7 +4,7 @@ services:
before_script: before_script:
- apk add --no-cache python3 - 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: stages:
- test - test
@ -28,4 +28,4 @@ build_and_push_container:
only: only:
- master - master
tags: tags:
- docker - docker

View File

@ -1,48 +1,66 @@
import requests import requests
from pyowm import OWM 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): def get_weather(location):
""" """
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. make life super easy.
TODO: Replace basic code block with nice looking, bold text w/ emojis TODO: Replace basic code block with nice looking, bold text w/ emojis
""" """
owm = OWM('593e0e182f9278a10443da354c4014db') try:
fc = owm.three_hours_forecast(location) openweathermap = OWM('593e0e182f9278a10443da354c4014db')
f = fc.get_forecast() forecaster = openweathermap.three_hours_forecast(location)
forecast = forecaster.get_forecast()
reg = f.get(0) reg = forecast.get(0)
indicator = int(reg.get_reference_time(timeformat='iso')[11:13]) // 3 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() + ' ' + ind = 0
'Current Temperature: ' + for i in range((4 - indicator) % 8, int((3 * len(forecast)) / 5), 8):
str(reg.get_temperature('fahrenheit').get('temp')) + high, low = 0, 100000
'F\n\nFive day forecast:\n') 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): temps = day.get_temperature('fahrenheit')
high, low = 0, 100000 h = temps.get('temp_max')
for j in range(0, 8): l = temps.get('temp_min')
if (i + j < len(f)): print(forecast.get(i + j).get_detailed_status())
day = f.get(i + j) if high < h:
else: high = h
break if low > l:
low = l
temps = day.get_temperature('fahrenheit') day = '{}{}{}'.format('***', days_from_ind.get(ind), '***\n')
h = temps.get('temp_max') weather_stat = '{}{}{}{}{}'.format('**Status:** *',
l = temps.get('temp_min') 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: return forecast_str
high = h except exceptions.not_found_error.NotFoundError:
if low > l: return 'Please input a valid location'
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 + '```'