Rewrite getweather script in python
This commit is contained in:
parent
60efc6f130
commit
ad74a3097e
16
getweather.py
Normal file
16
getweather.py
Normal 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)
|
@ -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/{} <<< "${@}"
|
|
Loading…
x
Reference in New Issue
Block a user