<commit_before>#!/usr/bin/env bash

# By <NAME> (http://benkogan.com)
# Usage:
#       weather [-ctv]
#
# [-t]  for tmux -- uses cache file
# [-c]  check in Celcius; default is Fahrenheit

cachedir="$HOME/scripts/.cache"
cache="$HOME/scripts/.cache/weather.log"
time=$(date +%s)
modold=210 # 3 minutes 30 seconds
modmid=180 # 3 minutes
modnew=5   # 5 seconds

# TODO: change ping interval to 1 min or respond to wifi signal?
# TODO: respond to curl failure


## output usage
usage () {
    echo "usage: weather [-ctv]"
}

# tmux
if [[ "$1" == "-t" ]]; then

    # If .cache dir doesn't exist, make it
    if [[ ! -d "$cachedir" ]]; then
        mkdir "$cachedir"
        touch "$cache"
        error=$?
        if [ ! "$error" -eq 0 ]; then
            echo "mkdir $cachedir failed"
            exit $error
        fi
    fi

    cachemod=$(stat -f%m "$cache") # Mod time of cache in sec since the epoch
    let diff=($time - $cachemod)   # Seconds from now since file was modified

    echo "$diff"

    # First case:  on tmux startup; assumes this is the case if cache file
    #              hasn't been used for longer than modold
    # Second case: cache is newly created; should thus be empty
    if [[ $diff -gt $modold || $diff -lt $modnew ]]; then
        # TODO: maybe this is a bad idea? delays loading twice as long
        echo "loading"

    # Load cached version
    elif [[ $diff -lt $modmid ]]; then
        echo "cache test"
        read weather < $cache
        echo "$weather"
        exit $?
    fi # If diff is between modmid and modold, continues
fi

# Get zipcode
zipcode=$(curl --silent "http://ipinfo.io/" | grep -E '(postal|})' \
    | sed -e 's/"postal": "//' -e 's/}//' -e 's/"//' | tr -d '\n  ')

# Get weather
if [[ "$2" == "-c" || "$1" == "-c" ]]; then
    weather=$(curl --silent \
        "http://xml.weather.yahoo.com/forecastrss?p=$zipcode&u=c" \
        | grep -E '(Current Conditions:|C<BR)' \
        | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' \
        -e 's/<\/b>//' -e 's/C<BR \/>/ºC/' -e 's/<description>//' \
        -e 's/<\/description>//' | tr -d '\n')
else
    weather=$(curl --silent \
        "http://xml.weather.yahoo.com/forecastrss?p=$zipcode" \
        | grep -E '(Current Conditions:|F<BR)' \
        | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' \
        -e 's/<\/b>//' -e 's/F<BR \/>/°F/' -e 's/<description>//' \
        -e 's/<\/description>//' | tr -d '\n')
fi
echo "$weather"

# tmux: add weather to cache file
if [ "$1" == "-t" ]; then
    echo "$weather" > "$cache"
fi
<commit_msg>Moved most ops into functions, still messy/incomplete<commit_after>#!/usr/bin/env bash

# By <NAME> (http://benkogan.com)

VERSION="0.0.1"

cachedir="$HOME/scripts/.cache"
cache="$HOME/scripts/.cache/weather.log"
time=$(date +%s)
modold=210 # 3 minutes 30 seconds
modmid=180 # 3 minutes
modnew=5   # 5 seconds

# TODO: change ping interval to 1 min or respond to wifi signal?
# TODO: respond to curl failure

## output usage
usage () {
    echo "usage: weather [-chtv]"
}

## get zipcode
zipcode () {
    zipcode=$(curl --silent "http://ipinfo.io/" | grep -e '(postal|})' \
        | sed -e 's/"postal": "//' -e 's/}//' -e 's/"//' | tr -d '\n  ')
}

# get weather
getweather () {
    if [[ "$2" == "-c" || "$1" == "-c" ]]; then
        weather=$(curl --silent \
            "http://xml.weather.yahoo.com/forecastrss?p=$zipcode&u=c" \
            | grep -E '(Current Conditions:|C<BR)' \
            | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' \
            -e 's/<\/b>//' -e 's/C<BR \/>/ºC/' -e 's/<description>//' \
            -e 's/<\/description>//' | tr -d '\n')
    else
        weather=$(curl --silent \
            "http://xml.weather.yahoo.com/forecastrss?p=$zipcode" \
            | grep -E '(Current Conditions:|F<BR)' \
            | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' \
            -e 's/<\/b>//' -e 's/F<BR \/>/°F/' -e 's/<description>//' \
            -e 's/<\/description>//' | tr -d '\n')
    fi
    echo "$weather"
}

## tmux
tmux () {
    if [[ "$1" == "-t" ]]; then

        # If .cache dir doesn't exist, make it
        if [[ ! -d "$cachedir" ]]; then
            mkdir "$cachedir"
            touch "$cache"
            error=$?
            if [ ! "$error" -eq 0 ]; then
                echo "mkdir $cachedir failed"
                exit $error
            fi
        fi

        cachemod=$(stat -f%m "$cache") # Mod time of cache in sec since the epoch
        let diff=($time - $cachemod)   # Seconds from now since file was modified

        echo "$diff"

        # First case:  on tmux startup; assumes this is the case if cache file
        #              hasn't been used for longer than modold
        # Second case: cache is newly created; should thus be empty
        if [[ $diff -gt $modold || $diff -lt $modnew ]]; then
            # TODO: maybe this is a bad idea? delays loading twice as long
            echo "loading"

        # Load cached version
        elif [[ $diff -lt $modmid ]]; then
            echo "cache test"
            read weather < $cache
            echo "$weather"
            exit $?
        fi # If diff is between modmid and modold, continues
    fi

    zipcode
    getweather

    # tmux: add weather to cache file
    if [ "$1" == "-t" ]; then
        echo "$weather" > "$cache"
    fi
}

## main
weather () {
    local arg="$1"

    case "${arg}" in

        ## flags
        -V|--version)
            echo "${VERSION}"
            return 0
            ;;

        -h|--help)
            usage
            return 0
            ;;

        -c|--celcius)
            celcius
            return 0
            ;;

        -t|--tmux)
            tmux
            return 0
            ;;

    esac
    getweather
}

## export or run
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
    export -f weather
else
    weather "{@}"
    exit $?
fi

{'fraction_non_alphanumeric': 0.18160337552742617, 'fraction_numerical': 0.007763713080168776, 'mean_word_length': 1.7409805735430157, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5898507', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2010, 'n_words': 627}