dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

weather (1726B)


      1 #!/bin/sh
      2 # requirements: hurl(1) or curl(1)
      3 
      4 image_viewer=xdg-open
      5 
      6 show_help() {
      7 	echo "usage: ${0##*/} [OPTIONS] ZIPCODE ..."
      8 	echo "shows DMI and Clear Outside weather forecast."
      9 	echo "Only works with danish zipcodes."
     10 	echo
     11 	echo "OPTIONS are one or more of the following:"
     12 	echo "   -h  show this message"
     13 	echo "   --  do not consider any following args as options"
     14 }
     15 
     16 die() {
     17 	printf '%s: error: %s\n' "${0##*/}" "$1" >&2
     18 	exit 1
     19 }
     20 
     21 get_file() {
     22 	o="$(mktemp)"
     23 	$fetcher "$1" > "$o"
     24 	printf '%s\n' "$o"
     25 }
     26 
     27 get_dmi_2day() {
     28 	get_file "https://servlet.dmi.dk/byvejr/servlet/byvejr_dag1?by=${1}&mode=long&eps=true"
     29 }
     30 
     31 get_dmi_9day() {
     32 	get_file "https://servlet.dmi.dk/byvejr/servlet/byvejr?by=${1}&tabel=dag3_9&eps=true"
     33 }
     34 
     35 get_clear_outside() {
     36 
     37 	if [ "$1" = 1000 ]; then
     38 		lon="55.68"
     39 		lat="12.57"
     40 	elif [ "$1" = 7700 ]; then
     41 		lon="56.96"
     42 		lat="8.70"
     43 	elif [ "$1" = 7730 ]; then
     44 		lon="57.10"
     45 		lat="8.72"
     46 	elif [ "$1" = 8000 ]; then
     47 		lon="56.16"
     48 		lat="10.20"
     49 	elif [ "$1" = 9520 ]; then
     50 		lon="56.84"
     51 		lat="9.89"
     52 	else
     53 		return
     54 	fi
     55 
     56 	get_file "https://clearoutside.com/forecast_image_large/${lon}/${lat}/forecast.png"
     57 }
     58 
     59 while :; do
     60 	case "$1" in
     61 		-h)
     62 			show_help
     63 			exit 0;;
     64 		--) # end all options
     65 			shift
     66 			break;;
     67 		-?*)
     68 			die "unknown option specified: $1";;
     69 		*)  # No more options
     70 			break
     71 	esac
     72 	shift
     73 done
     74 
     75 if command -v hurl >/dev/null 2>&1; then
     76 	fetcher=hurl
     77 elif command -v curl >/dev/null 2>&1; then
     78 	fetcher=curl
     79 else
     80 	die "hurl or curl not found"
     81 fi
     82 
     83 show_weather() {
     84 	co="$(get_clear_outside "$1")"
     85 	dmi9="$(get_dmi_9day "$1")"
     86 	dmi2="$(get_dmi_2day "$1")"
     87 	$image_viewer "$co" "$dmi9" "$dmi2"
     88 }
     89 
     90 if [ $# -lt 1 ]; then
     91 	show_weather 8000
     92 fi
     93 
     94 for zip in "$@"; do
     95 	show_weather "$zip"
     96 done