|
#!/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) |