Rewrite getweather script in python

This commit is contained in:
Eric Torres 2019-01-20 14:16:52 -08:00
parent 60efc6f130
commit ad74a3097e
2 changed files with 16 additions and 38 deletions

16
getweather.py Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/python3
"""Obtain a weather forecast."""
import argparse
import requests
WTTR_URI = 'http://wttr.in'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('location')
args = parser.parse_args()
location = args.location
print(requests.get(f"{WTTR_URI}/{location}").text)

View File

@ -1,38 +0,0 @@
#!/usr/bin/bash
# Obtain a weather forecast
printHelp() {
cat << EOF
Retrieve the weather of a give location
Usage: getweather [-h|--help] [location]
Options:
-h show this help page
EOF
}
while true; do
case "${1}" in
"-h"|"--help")
printHelp
exit
;;
--)
shift
break
;;
-*)
echo "Not an option: ${1}" >&2 && exit 1
exit
;;
*)
break;
;;
esac
done
[[ ! -x '/usr/bin/curl' ]] && echo 'curl is required for this script' && exit 1
[[ -z "${@}" ]] && echo "Please enter a location" >&2 && exit 1
xargs --no-run-if-empty -I {} curl wttr.in/{} <<< "${@}"