Browse Source

Some fixes that allowed it to run on the server

master
sloum 4 months ago
parent
commit
515f50b830
1 changed files with 15 additions and 11 deletions
  1. +15
    -11
      weather

weather.py → weather View File

@ -3,15 +3,20 @@
import requests
import json
import sys
import os
import urllib.parse
GEOCODE_ROOT_URL = 'https://nominatim.openstreetmap.org/search?format=json&postalcode={}'
WEATHER_META_ROOT_URL = 'https://api.weather.gov/points/{},{}'
APPLICATION_URL = 'gemini://rawtext.club/~sloum/cgi/weather'
def error_exit(val=''):
print('20 text/gemini\r')
print(val)
print('\n=> {} Get weather by zip code...\n')
def error_exit(val='',query=False):
if query:
print('10 {}\r'.format(val))
else:
print('20 text/gemini\r')
print(val)
print('\n=> {} Get weather by zip code...\n'.format(APPLICATION_URL))
sys.exit()
def geocode(zip_code):
@ -54,8 +59,8 @@ def weather_forecast(meta):
return data['properties']['periods']
def format_single_forecast_point(point):
out = '''
### {}
out = '''### {}
* Date: {}
* Temp: {}°{}
* Wind: {}, {}
@ -71,13 +76,12 @@ def pretty_print(zipcode, meta, forecast):
print('* Station: {}\n\n## Forecast\n'.format(meta['station']))
for point in forecast:
print(format_single_forecast_point(point))
print('\n\n```\n+ + + + + + +\n```\nEND\n')
def main():
qs = os.environ['QUERY_STRING']
qs = urllib.parse.unquote_plus(qs)
if not qs.strip():
error_exit()
qs = os.environ.get('QUERY_STRING')
if not qs or not qs.strip():
error_exit('Enter US zip code', True)
qs = urllib.parse.unquote_plus(qs).strip()
location = geocode(qs)
meta = weather_meta(location)
forecast = weather_forecast(meta)

Loading…
Cancel
Save