Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 159 lines (136 sloc) 5.96 KB
#!/usr/bin/env bash
# oldnotifier -- notifier for The Old Reader
#
# Copyright 2014-2015 Kylie McClain <somasis@exherbo.org>
# Distributed under the terms of the ISC License
# https://github.com/somasis/scripts/
#
# Prerequisites: notify-send, curl, Old Reader account
# notify-send might be found in `libnotify-bin`, `libnotify`, or `notify-send`
#
# How to run: Just run `oldnotifier` without any arguments.
# All settings are just variables in oldnotifier.conf.
# $XDG_CONFIG_HOME/oldnotifier.conf is the config location, this is likely a
# directory named ".config" in your home directory.
# If it doesn't exist, echo "$XDG_CONFIG_HOME"
#
# How to use:
# Put the script in a directory in your $PATH (for example ~/bin/ or /usr/local/bin/, or really just any directory)
# Make sure you have common.sh in the same folder, or set it's location
# (this is set in the varaibles below, but it's easier to just put it in
# the same folder as the script. Change $common_location if you want to.)
#
# Set it to autostart when you log in (in GNOME/Unity this would be in
# Startup Applications, in KDE this is Startup and Shutdown -> Autostart)
#
# Make sure you set the $email and $password in your configuration file.
#
# Make sure the script is executable (chmod +x $script_location)
#
# Run it (explained above) or just log out and log back in, and
# depending on your settings it will either notify you as soon as it
# starts, or at the top of the coming hour. The default is not to wait.
# (changed with $wait_until_top_of_hour)
#
# Lastly, enjoy your addiction, thanks to The Old Reader and this program. :)
## Don't change these variables, use oldnotifier.conf for your own settings!
## These are used as the fallbacks for unset settings. They might change with
## updates, so it's best to set them in your config so you don't rely on defaults
email=
password=
notify_every="30m" # Don't set this under 10m unless you want to get in trouble!
wait_until_top_of_hour="false" # wait until the minute is 00 to notify?
icon='internet-feed-reader' # Icon used for notification. Location or system icon name
# About $common_location and $config_location:
# Yes, you need to use single quotes though this is a variable-using setting.
# Normally in bash you use double quotes for variable-using strings, but
# since we don't get $SCRIPT_DIR until /after/ this is declared, we need to
# re-declare it, with `eval` to find the variables.
## This is where the script begins;
## don't edit this unless you know what you are doing! :)
me=oldnotifier
stderr() { # stderr <msg>
echo "$@" >&2
}
die() { # die <err msg> <err code> or die <err code>
if [[ "$#" -gt 1 ]];then
local err_msg="$1"
local err_code="$2"
stderr "$me: $err_msg; dying"
else
local err_code="$1"
fi
exit $err_code
}
sleepuntil() { # sleepuntil - borrowed with some modifications from http://stackoverflow.com/a/19067658
local slp tzoff now
local hms=(${1//:/ })
printf -v now '%(%s)T' -1
printf -v tzoff '%(%z)T\n' $now
tzoff=$((0${tzoff:0:1}(3600*${tzoff:1:2}+60*${tzoff:3:2})))
slp=$(((86400+(now-now%86400)+10#$hms*3600+10#${hms[1]}*60+${hms[2]}-tzoff-now)%86400))
sleep $slp
}
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
config_location="$XDG_CONFIG_HOME/oldnotifier.conf"
[[ -f "$config_location" ]] && . "$config_location"
user_agent="oldnotifier - https://github.com/somasis/scripts"
# Make sure notify-send is installed.
if ! command -v notify-send 2>&1 >/dev/null;then
die "\`notify-send\` is not installed, it is needed for notifications" 6
fi
if [[ "$email" != *@* ]];then
die "You must supply an email. You can change the configuration by adding your own settings to \"$XDG_CONFIG_HOME/oldnotifier.conf\"" 1
fi
if [[ "$wait_until_top_of_hour" == "true" ]];then
if [[ "$(( $(date +%H) + 1 ))" -lt 24 ]];then
echo "Waiting until $(date +$(( $(date +%H) + 1 )):00)..."
sleepuntil "$(date +$(( $(date +%H) + 1 )):00)"
else
echo "Waiting until 0:00..."
sleepuntil "0:00"
fi
fi
key=$(curl -sd "client=oldnotifier&Email=$email&Passwd=$password" "https://theoldreader.com/accounts/ClientLogin" | grep "^Auth=" | cut -d'=' -f2)
if [[ -z "$key" ]];then
die "Key is invalid, check your password or email configuration" 2
fi
while true; do
unread_raw=$(curl -sH "Authorization: GoogleLogin auth=$key" "https://theoldreader.com/reader/api/0/unread-count?output=json" | sed 's/[0-9],/&,\n/g' | sed 's/","count":/:/g;s/.*id":"//g;s/,,//g;s/.*}]}//;s/{"//g;s/":/:/g' | grep -v '^$')
if [[ -z "$unread_raw" ]];then
die "Failed to get unread count, run this script with bash -x to get debug output" 3
fi
unread_total=$(echo "$unread_raw" | grep "^max:" | cut -d':' -f2)
unread_feeds=$(echo "$unread_raw" | grep '^feed/' | cut -d'/' -f2 | tr '\n' ',')
unread_labels=$(echo "$unread_raw" | grep 'label/' | cut -d'/' -f4 | tr '\n' ',')
unread_other=$(OLDIFS="$IFS"
IFS=","
for unread_label in $unread_labels;do
unread_label_name=$(echo "$unread_label" | cut -d':' -f1)
unread_label_count=$(echo "$unread_label" | cut -d':' -f2)
if [[ "$unread_label_count" -gt 0 ]];then
if [[ "$unread_label_count" -gt 1 ]];then
article_word="articles"
grammar_ugh="are"
else
article_word="article"
grammar_ugh="is"
fi
echo "There $grammar_ugh $unread_label_count unread $article_word in $unread_label_name."
fi
done
IFS="$OLDIFS"
OLDIFS=
)
if [[ "$unread_total" -gt 0 ]];then
if [[ "$unread_total" -gt 1 ]];then
article_word="articles"
else
article_word="article"
fi
notify-send -i "$icon" "The Old Reader" "You have $unread_total unread $article_word.\n$unread_other"
echo -e "You have $unread_total unread $article_word.
$unread_other"
fi
sleep "$notify_every"
done