Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Forked from yloiseau/watson-notify
Last active September 22, 2017 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpotlightKid/3519751a3dfa4fb80fc9 to your computer and use it in GitHub Desktop.
Save SpotlightKid/3519751a3dfa4fb80fc9 to your computer and use it in GitHub Desktop.
Watson desktop notifier (Python)

Requirements

Indirect dependencies in parentheses:

  • python-notify2 (python-dbus, python-gi)
  • python-arrow (python-dateutil)
  • Watson (click)

Installation

mkdir -p "$HOME/bin"
install watson-notify "$HOME/bin"
mkdir -p "$HOME/.local/share/icons"
install logo-watson-notext.svg "$HOME/.local/share/icons"
crontab -e


*/15 * * * 1-5 watson-notify -i "~/.local/share/icons"
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/bin/bash
#
# Copyright © 2016 Yannick Loiseau <me@yloiseau.net>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#
#==============================================================================
# Script to check if you are working on a project with Watson and display a
# notification when:
# - you are working on the same project for more than $ALERT_TIME (seconds)
# - you are not working on a project
#
# The purpose is to remind you to start tracking your time, or to check is you
# have forgotten to change the project (and remind you to take a break).
#
# Dependencies:
# - Watson: http://tailordev.github.io/Watson/
# - notify-send: (libnotify-bin on Debian)
#
# Usage:
# 1. Customize the alert message and time.
# 1a. Customize DISPLAY if needed
# 2. Set a cron task to run the script. E.g. (every 5min)
#
# */5 * * * * $HOME/bin/watson-notify
#
#==============================================================================
set -e
ALERT_TIME=7200 # 2 hours
ALERT_MESSAGE="Still working on %s?\nTime for a break."
ALERT_NOTIF="-u low -t 5000"
QUESTION_MESSAGE="What are you doing?"
QUESTION_NOTIF="-u critical -t 10000"
WATSON_STATUS="python3 -m watson status"
NOTIFY="notify-send"
export DISPLAY=:0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$WATSON_STATUS |
sed -r '
s!^Project !!g;
s!([^ ]+).+started .* ago \(([^)]+)\)!\1 \2!g
' | while read name date; do
if [ $name == "No" -a "$date" == "project started" ] ; then
$NOTIFY $QUESTION_NOTIF "Watson" "$QUESTION_MESSAGE"
exit 0
fi
now=$(date +%s)
ts=$(date -d "$(echo $date | tr '.' '-')" +%s)
duration=$((now - ts))
if [ $duration -ge $ALERT_TIME ] ; then
$NOTIFY $ALERT_NOTIF "Watson" "$(printf "$ALERT_MESSAGE" $name)"
fi
done
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Check for current Watson project and display a notification when:
- you are working on the same project for more than the maximum duration.
- you are not working on a project.
The maximum duration defaults to 120 minutes and can be set with the `-d` resp.
`--max-duration` command line option.
The time (in seconds) for which the notification is displayed can be set with
the `-t` resp. `--timeout` option (defaults to 5 seconds).
Add the following line to your crontab (`crontab -e`), to run this script
every ten minutes from 10-20h on weekdays::
*/10 10-20 * * mon-fri DISPLAY=:0 "$HOME/bin/watson-notify" 2>/dev/null
"""
import argparse
import os
import sys
import arrow
import notify2
from watson import Watson
WARN_MSG = "Still working on <b>{}</b>?\nTime for a break!"
REMIND_MSG = "What are you doing now?"
def main(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--max-duration',
metavar='MIN', type=int, default=120,
help='maximum frame duration before suggesting a break '
'(default: %(default)i min.)')
parser.add_argument('-i', '--icon',
metavar='PATH',
help='path to icon to display with notification')
parser.add_argument('-t', '--timeout',
metavar='SEC', type=int, default=5,
help='timeout for notification display (default: %(default)i sec.)')
args = parser.parse_args(sys.argv[1:] if args is None else args)
current = Watson().current
if current:
duration = (arrow.now() - current['start']).total_seconds()
if duration < args.max_duration * 60:
return
cat = "presence.online"
subject = "Long Watson frame duration"
message = WARN_MSG.format(current['project'])
else:
cat = "presence.offline"
subject = "No Watson project running"
message = REMIND_MSG
if not notify2.init('watson-notify'):
return "Could not connect to notification service."
icon = ("file://%s" % os.path.abspath(args.icon) if args.icon
else "notification-message-IM")
n = notify2.Notification(subject, message, icon=icon)
n.set_category(cat)
n.set_urgency(notify2.URGENCY_LOW)
n.set_timeout(args.timeout * 1000)
if not n.show():
return "Failed to send notification"
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]) or 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment