mirror of
https://github.com/sbrl/bin.git
synced 2018-01-10 21:33:46 +00:00
Add quote-get
This commit is contained in:
parent
4e0ff4f617
commit
7e627fb53a
2 changed files with 52 additions and 1 deletions
|
@ -55,6 +55,7 @@ Here's a list of the most interesting ones, along with what they do and where th
|
|||
- [colortrans](https://gist.github.com/MicahElliott/719710) - Convert rgb colours to xterm ones
|
||||
- nautilus-here - A quick command that opens nautilus in the current directory. For lazy people :P
|
||||
- ssh-retry - I forget where it came from, but this command reepeatedly calls ssh until it succeeds in connecting.
|
||||
- quote-get - Displays a random quote from a remote file. The quote stays the same until the following day. The remote file is cached locally for convenience :smiley_cat:
|
||||
|
||||
## cloudsurfer.sh
|
||||
`cloudsurfer.sh` is a new script I'm writing to bring some of the customisations I store here in this repository to terminals on shared machines - where I can't modify `.bashrc`, for example.
|
||||
|
|
50
quote-get
Executable file
50
quote-get
Executable file
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
quotes_cache_file="${HOME}/.cache/quotes.txt";
|
||||
update_cache_every="1 day";
|
||||
|
||||
if [[ "$#" -lt "1" ]]; then
|
||||
echo -e "Quote generation tool"
|
||||
echo -e "By Starbeamrainbowlabs"
|
||||
echo -e ""
|
||||
echo -e "Downloads and caches a quote file and picks out a random (per day) quote."
|
||||
echo -e ""
|
||||
echo -e "Usage:"
|
||||
echo -e " quote-get {quote-file-url}"
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
quotes_url="$1";
|
||||
|
||||
if [ ! -d "$(dirname "${quotes_cache_file}")" ]; then
|
||||
mkdir -p "${quotes_cache_file}";
|
||||
fi
|
||||
|
||||
function download_quotes {
|
||||
#echo -ne "> Downloading quotes file - " 1>&2;
|
||||
curl -o "${quotes_cache_file}" -L ${quotes_url} 2>/dev/null;
|
||||
#echo -e "done" 1>&2;
|
||||
}
|
||||
|
||||
# Download the quotes file if it doesn't exist already
|
||||
if [ ! -f "${quotes_cache_file}" ]; then
|
||||
download_quotes;
|
||||
fi
|
||||
|
||||
# Update the quotes file if it's out of date
|
||||
oldest_allowed=$(date -d "now - ${update_cache_every}" +%s);
|
||||
cache_filemtime=$(date -r "${quotes_cache_file}" +%s);
|
||||
|
||||
if (( $cache_filemtime <= $oldest_allowed )); then
|
||||
download_quotes;
|
||||
fi
|
||||
|
||||
# Generate a quote
|
||||
quote_count=$(cat "${quotes_cache_file}" | wc -l);
|
||||
|
||||
# Set the random number generator seed to ensure we give the same quote on a per-day basis
|
||||
RANDOM=$(date +%Y-%m-%d);
|
||||
|
||||
quote_line=$((( $RANDOM % ${quote_count} )));
|
||||
|
||||
sed -n ${quote_line}p "${quotes_cache_file}";
|
Loading…
Reference in a new issue