1. Anoop Thomas Mathew
  2. sentience

Commits

Anoop Thomas Mathew  committed f3d17d8

Perfectly working, usable code, bumped to v1.1

  • Participants
  • Parent commits b860ef4
  • Branches master

Comments (0)

Files changed (3)

File README

View file
  • Ignore whitespace
-Copyright (c) 2012, Anoop Thomas Matew<atmb4u@gmail.com>
+Copyright (c) 2012, Anoop Thomas Mathew<atmb4u@gmail.com>
+Released under BSD License
 
 Sentience - Pomodoro Notifier for gtk dbus enabled desktop environments.
 Best suited for awesome and gnome. KDE not tested.

File notifier.py

View file
  • Ignore whitespace
 import daemon
 import datetime
 import subprocess
-from time import strftime, localtime, sleep
+
 import settings
 
+from time import strftime, localtime, sleep
+from datetime import timedelta
+
 
 def get_did_you_know():
     try:
         opener.addheaders = [('User-agent', 'Mozilla/5.0')]
         infile = opener.open('http://en.wikipedia.org/wiki/Main_Page')
         soup = BeautifulSoup(infile)
-        item = randrange(0, 6)
+        item = randrange(0, 5)
         dyk = soup.find(id="mp-dyk").find_all('li')[item].text.replace("(pictured)", "").\
                                                                replace("...", "")
         dyk = "Did you know %s" % dyk
         return dyk
     except:
-        return
+        return ''
 
 
 def notify_me(body='', app_name='', app_icon='',
     status = "C"
     timer = False
     first_time = True
+    target_time = datetime.datetime.now()
     while 1:
         acpi_info = subprocess.check_output(["acpi", "-i"])
         splits = acpi_info.split(',')[2].split(' ')[1].split(':')
 
 #Notifies about time and battery status in a preset time interval
         dt = datetime.datetime.now()
-        if first_time:
-            notification_time = settings.NOTIFICATION_FREQUENCY
-        else:
-            notification_time = settings.NOTIFICATION_FREQUENCY + settings.BREAK_TIME
-        if not dt.minute % notification_time and dt.second <= (settings.NOTIFICATION_COUNT / 2.0):
+        if first_time or (dt.hour == next_time.hour and dt.minute == next_time.minute \
+                      and dt.second == next_time.second):
             first_time = False
             acpi_info = subprocess.check_output(["acpi", "-i"])
             percentage = "%s battery left" % (acpi_info.split(',')[1].split(' ')[1])
+
+            time_left = acpi_info.split(',')[2].split(' ')[1]
             if 'Charging' in acpi_info:
-                time_left = "%s hours for full charging" % (acpi_info.split(',')[2].split(' ')[1])
+                if time_left:
+                    time_left = "%s hours for full charging" % (time_left)
+                else:
+                    time_left = "Fully Charged, Plugged In."
             else:
-                time_left = "%s hours of battery left " % (acpi_info.split(',')[2].split(' ')[1])
+                if time_left:
+                    time_left = "%s hours  of battery left " % (time_left)
+                else:
+                    time_left = ""
 
-            news = "Its %s" % (strftime("%I:%M %p", localtime()))
+            if not first_time:
+                news = "Its %s" % (strftime("%I:%M %p", localtime()))
+            else:
+                news = ''
             dyk = get_did_you_know()
             timer = True
+            target_time = dt + timedelta(seconds=settings.BREAK_TIME)
+            next_time = dt + timedelta(seconds=settings.NOTIFICATION_SPAN + settings.BREAK_TIME)
             summary = "%s \n %s \n %s, Time to take a short break \n%s" \
                                 % (percentage, time_left, news, dyk)
             interface.Notify(app_name, replaces_id, app_icon,
                                  summary, body, actions, hints, timeout)
 
-# Notifies about end of break time
-        if timer and not dt.minute % int(settings.NOTIFICATION_FREQUENCY + settings.BREAKTIME) \
-                        and dt.second <= int(settings.NOTIFICATION_COUNT / 2.0):
+        # Notifies about end of break time
+        if timer and dt > target_time:
             timer = False
             acpi_info = subprocess.check_output(["acpi", "-i"])
             percentage = "%s battery left" % (acpi_info.split(',')[1].split(' ')[1])
+
+            time_left = acpi_info.split(',')[2].split(' ')[1]
             if 'Charging' in acpi_info:
-                time_left = "%s hours for full charging" % (acpi_info.split(',')[2].split(' ')[1])
+                if time_left:
+                    time_left = "%s hours for full charging" % (time_left)
+                else:
+                    time_left = "Fully Charged, Plugged In."
             else:
-                time_left = "%s hours of battery left " % (acpi_info.split(',')[2].split(' ')[1])
+                if time_left:
+                    time_left = "%s hours  of battery left " % (time_left)
+                else:
+                    time_left = ""
 
             news = "Its %s" % (strftime("%I:%M %p", localtime()))
-            summary = "%s \n %s \n %s, Break time is over,  to Work dude !!! %s" \
+            summary = "%s \n %s \n %s, Break time is over, Time to Work dude !!!" \
                                 % (percentage, time_left, news)
             interface.Notify(app_name, replaces_id, app_icon,
                                  summary, body, actions, hints, timeout)
-
-        sleep(2)
+        print dt, next_time, target_time
+        sleep(0.75)
 
 if __name__ == '__main__':
-    print "Daemon Starting ..."
+    print "Sentience v1.1 Daemon Starting ...\n"
+    print "Session Duration: %s\n" % (settings.NOTIFICATION_SPAN)
+    print "Break Time: %s\n" % (settings.BREAK_TIME)
     with daemon.DaemonContext():
         notify_me()

File settings.py

View file
  • Ignore whitespace
 BATTERY_MIN_SEC = '00'
 
 #General settings
-NOTIFICATION_COUNT = '1'  # no of times notification to be shown
-NOTIFICATION_DURATION = '10000'  # time to live in millisecond
-NOTIFICATION_FREQUENCY = 15  # once every X minutes
-BREAK_TIME = 5  # break time in between notifications
-# in effective, it would be NOTIFICATION_FREQUENCY + BREAK_TIME, the time between notifications
+NOTIFICATION_DURATION = '15000'  # time to live in millisecond
+NOTIFICATION_SPAN = 900  # once every X seconds
+BREAK_TIME = 300  # break time in between notifications in seconds
+# in effective, it would be NOTIFICATION_SPAN+ BREAK_TIME, the time for each sprint