From ad74a3097e273bfd6dc3c2089793a1f8fb3d4079 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sun, 20 Jan 2019 14:16:52 -0800 Subject: [PATCH] Rewrite getweather script in python --- getweather.py | 16 ++++++++++++++++ getweather.sh | 38 -------------------------------------- 2 files changed, 16 insertions(+), 38 deletions(-) create mode 100644 getweather.py delete mode 100755 getweather.sh diff --git a/getweather.py b/getweather.py new file mode 100644 index 0000000..2fcd5a3 --- /dev/null +++ b/getweather.py @@ -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) diff --git a/getweather.sh b/getweather.sh deleted file mode 100755 index a405038..0000000 --- a/getweather.sh +++ /dev/null @@ -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/{} <<< "${@}"