broken af but what are you gonna do

This commit is contained in:
KeeganForelight 2023-01-13 15:59:14 -05:00
parent b447b8b1d0
commit d48c183f12
7 changed files with 122 additions and 223 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env.key

View File

@ -1,35 +0,0 @@
#!/usr/bin/env bash
# creates a hidden file of your location details based on your address, will expand to support addition via zip codes
usage() { echo "Usage: $0 [-z zipcode]" 1>&2; exit 1; }
if [[ $# -eq 0 ]] ; then
# called without args
CURRENT_LOCATION=$(curl --silent http://ip-api.com/csv)
CITY=$(echo "$CURRENT_LOCATION" | cut -d , -f 6)
LAT=$(echo "$CURRENT_LOCATION" | cut -d , -f 8)
LON=$(echo "$CURRENT_LOCATION" | cut -d , -f 9)
printf "CITY=%s\nLAT=%s\nLON=%s\n" "$CITY" $LAT $LON > "$HOME/.dotfiles/bin/weather/.env.location"
fi
while getopts "z:" arg; do
case ${arg} in
z)
ZIPCODE=${OPTARG}
source "$HOME/.dotfiles/.priv/key"
URL="http://api.openweathermap.org/geo/1.0/zip?zip=$ZIPCODE&appid=$API_KEY"
LOCATION=$(curl --silent "$URL")
CITY=$(echo $LOCATION | jq -r '.name')
LAT=$(echo $LOCATION | jq -r '.lat')
LON=$(echo $LOCATION | jq -r '.lon')
printf "CITY=%s\nLAT=%s\nLON=%s\n" "$CITY" $LAT $LON > "$HOME/.dotfiles/bin/weather/.env.location"
# emptying weather buffer
>"$HOME/.dotfiles/bin/weather/.env.weather"
;;
?)
usage
;;
esac
done

Binary file not shown.

View File

@ -1,20 +0,0 @@
#!/bin/bash
source "$HOME/.dotfiles/.priv/key"
source "$HOME/.dotfiles/bin/weather/.env.location"
source "$HOME/.dotfiles/bin/weather/.env.weather"
# API_KEY=$(cat "$HOME/.dotfiles/bin/weather/.priv/key")
# getting weather
CUR_TIMESTAMP=$(date +%s)
# only update weather every 30 seconds
if [[ $CUR_TIMESTAMP -gt $EXPIRATION || -z $EXPIRATION ]] ; then
WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
echo $WEATHER
TEMP=$(echo $WEATHER | gojq -r '.main.temp')
HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity')
ICON=$(echo $WEATHER | gojq -r '.weather[0].icon')
EXPIRATION=$(($CUR_TIMESTAMP+30))
printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$EXPIRATION" >"$HOME/.dotfiles/bin/weather/.env.weather"
fi

64
client_weather.sh Executable file
View File

@ -0,0 +1,64 @@
#!/bin/bash
# server version, calls server for actual API info
usage() {
cat <<EOF
usage: $0 [-c COORD_PAIR][-s QUERY][-z ZIPCODE]
Queries OpenWeatherMapAPI for weather or lat/long
options:
-c, --coords searches for weather based on COORD_PAIR
-z, --zipcode returns the coordinate pair(s) for given zipcode
-s, --search returns the coordinate pair(s) for QUERY
COORD_PAIR is a string of the format "LAT,LON"
QUERY is a city, state or city to search
EOF
}
WORKING_DIR="$HOME/.local/share/weather"
get_location() {
# no search, use IP
url="http://ip-api.com/csv/?fields=252"
# search provided, feed to script
}
get_weather() {
# calls server for weather based on $LAT, $LONG
}
# getting weather
CUR_TIMESTAMP=$(date +%s)
# only update weather every 30 seconds
if [[ $CUR_TIMESTAMP -gt $EXPIRATION || -z $EXPIRATION ]] ; then
WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
echo $WEATHER
TEMP=$(echo $WEATHER | gojq -r '.main.temp')
HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity')
ICON=$(echo $WEATHER | gojq -r '.weather[0].icon')
EXPIRATION=$(($CUR_TIMESTAMP+30))
printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$EXPIRATION" >"$HOME/.dotfiles/bin/weather/.env.weather"
fi
# shifting longform
for arg in "$@"; do
shift
case "$arg" in
'--zipcode') set -- "$@" "-z" ;;
'--coords') set -- "$@" "-c" ;;
'--search') set -- "$@" "-s" ;;
*) set -- "$@" "$arg" ;;
esac
done
while getopts "o:a:" arg; do
case ${arg} in
esac
done

57
server_weather.sh Executable file
View File

@ -0,0 +1,57 @@
#!/bin/bash
# server version, calls server for actual API info
WORKING_DIR="$HOME/.local/share/weather"
# sets $API_KEY
source "$WORKING_DIR/.env.key"
lookup_location() {
# search for a location
url="http://api.openweathermap.org/geo/1.0/direct?q=$SEARCH&appid=$API_KEY"
RES=$(curl --silent "$url")
echo "$RES" | gojq '.'
}
lookup_zipcode() {
# lookup based on zipcode
url="http://api.openweathermap.org/geo/1.0/zip?zip=$ZIPCODE&appid=$API_KEY"
RES=$(curl --silent "$url")
echo "$RES" | gojq '.'
}
get_weather() {
# calls server for weather based on $LAT, $LONG
url="https://api.openweathermap.org/data/2.5/weather\?lat=$LAT\&lon=$LON\&appid=$API_KEY\&units=imperial"
RES=$(curl --silent "$url")
echo "$RES" | gojq '.'
}
while getopts "c:s:z:" arg; do
case "$opt" in
'c' )
COORDS="$OPTARG"
;;
's' )
SEARCH="$OPTARG"
;;
'z' )
ZIPCODE="$OPTARG"
;;
'?' )
echo "ERROR: UNRECOGNZIED ARGUMENT"
exit 1
;;
esac
done
if [[ -n "$COORDS" ]] ; then
get_weather
elif [[ -n "$SEARCH" ]] ; then
lookup_location
elif [[ -n "$ZIPCODE" ]] ; then
lookup_zipcode
fi
exit 0

168
weather
View File

@ -1,168 +0,0 @@
#!/usr/bin/env bash
WEATHER_DIR="${HOME}/.dotfiles/bin/weather"
usage() {
cat <<EOF
Usage: wttr [-s] [-d] [-u] [-i] [-f] [-h]
Queries the weather and outputs it in a Nerd Font compaitable way
Options:
-p, --print print the current weather information
-s, --setloc=<LOC> change location used for wttr.in query
will use IP if left blank
-d, --disploc=<LOC> show location used in query
-f, --force force refresh weather data
-h, --help Show this help message
EOF
}
get_wttr() {
if [[ -z "$ZONE" || ! -z "$UPDATE" ]] ; then
# getting grid points
url="https://api.weather.gov/points/$LATITUDE,$LONGITUDE"
GRID=$(curl --silent -fL "$url")
query="{\
zone: .properties.forecastZone,\
city: .properties.relativeLocation.properties.city,\
state: .properties.relativeLocation.properties.state}"
res=$(echo "$GRID" | gojq "$query")
echo "$res"
CITY=$(echo "$res" | gojq '.city')
STATE=$(echo "$res" | gojq '.state')
ZONE=$(echo "$res" | gojq '.zone' | tr -d '"')
fi
WEATHER=$(curl --silent -fL "$ZONE/observations" | gojq '.features[0].properties')
query="{\
temp: .temperature.value,\
humidity: .relativeHumidity.value,\
icon: .icon}"
PARSED=$(echo "$WEATHER" | gojq "$query")
TEMP=$(echo "$PARSED" | gojq '.temp')
TEMP=$(echo "$TEMP * (9/5) + 32" | bc -l)
HUMIDITY=$(echo "$PARSED" | gojq '.humidity')
TOD=$(echo "$PARSED" | gojq '.icon' | awk -F / '{print $6}')
ICON=$(echo "$PARSED" | gojq '.icon' | awk -F / '{print $7}' | awk -F ? '{print $1}')
EXPIRATION=$(($(date +%s) + 60))
printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nLOCATION=%s\nZONE=%s\nCITY=%s\nSTATE=%s\nTOD=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$LOCATION" "$ZONE" "$CITY" "$STATE" "$TOD" "$EXPIRATION" > ${WEATHER_DIR}/.weather
####
# below is WTTR impl, using NWS above
####
# gets weather for $LOCATION and stores it into file with timestamp
# url="https://wttr.in/?format=j1&nonce=$RANDOM"
# FULL_WTTR=$(curl --silent -fL "$url")
# # parse relevant info
# query="{\
# temp_F: .current_condition[0].temp_F,\
# temp_C: .current_condition[0].temp_C,\
# humidity: .current_condition[0].humidity,\
# conditions: .current_condition[0].weatherCode,\
# location: .nearest_area[0].areaName[0].value,\
# sunset: .weather[0].astronomy[0].sunset,\
# sunrise: .weather[1].astronomy[0].sunrise}"
# WTTR=$(echo "$FULL_WTTR" | gojq "$query")
# # Parsing info
# TEMP=$(echo "$WTTR" | gojq .temp_"$UNITS")
# HUMIDITY=$(echo "$WTTR" | gojq '.humidity')
# CONDITIONS=$(echo "$WTTR" | gojq '.conditions')
# LOCATION=$(echo "$WTTR" | gojq '.location')
# # TOD info
# SUNSET=$(echo "$WTTR" | gojq '.sunset' | tr -d ' "')
# SUNRISE=$(echo "$WTTR" | gojq '.sunrise' | tr -d ' "')
# # getting TOD
# get_tod
# # setting symbol
# # updating weather info
# printf 'UNITS=%s\nTEMP=%s\nHUMIDITY=%s\nCONDITIONS=%s\nLOCATION=%s\nEXPIRATION=%s\nTOD=%s\n' "$UNITS" "$TEMP" "$HUMIDITY" "$CONDITIONS" "$LOCATION" "$EXPIRATION" "$TOD" > ${WEATHER_DIR}/.weather
}
set_location() {
# updates location of weather query to $new_loc, can be empty
LOCATION="$new_loc"
UPDATE="true"
url="https://v2.wttr.in/$LOCATION?format=j1"
query="{\
lat: .nearest_area[0].latitude,\
lon: .nearest_area[0].longitude}"
WTTR=$(curl --silent -fL "$url" | gojq "$query")
# Grabbing env vars
LATITUDE=$(echo "$WTTR" | gojq '.lat' | tr -d ' "')
LONGITUDE=$(echo "$WTTR" | gojq '.lon' | tr -d ' "')
get_wttr
}
display_location() {
# updates location to display to $disp_loc
printf 'Searching %s, (%s, %s)\n' "$LOCATION" "$CITY" "$STATE"
}
load_wttr() {
# loads and checks weather
if [[ ! -f ${WEATHER_DIR}/.weather ]] ; then
# file doesnt exist
UNITS="F" # defaults to farenheit
get_wttr
fi
# load env
source ${WEATHER_DIR}/.weather
# check for expiration
if [[ $(date +%s) -ge $EXPIRATION ]] ; then
get_wttr
fi
}
print_wttr() {
# stripping and reformatting quotations
query=$(printf '.%s.%s' $TOD $ICON)
SYMBOL=$(cat ${WEATHER_DIR}/symbols.json | gojq "$query" | tr -d '"')
UNIT=$(echo -e "\ufa04")
HUMID_SYMB=$(echo -e "\ue373")
printf '%s %.1f%s %0.1f %s in %s, %s\n' "$SYMBOL" "$TEMP" "$UNIT" "$HUMIDITY" "$HUMID_SYMB" "$CITY" "$STATE"
}
load_wttr
while [[ "$1" != "" ]]; do
case $1 in
-h | --help)
usage
;;
-p | --print)
print_wttr
;;
-s | --setloc)
shift
new_loc="$1"
set_location
;;
-d | --disploc)
shift
display_location
;;
-f | --force)
get_wttr
;;
*)
echo "Error: wttr $@"
usage
;;
esac
shift
done