Wrapper script for notifications

This commit is contained in:
MrTyton 2016-09-08 06:09:42 -04:00
parent b6293e431b
commit f95a59efdd
5 changed files with 196 additions and 0 deletions

85
balloonNotify.py Executable file
View File

@ -0,0 +1,85 @@
# -- coding: utf-8 --
from win32api import *
from win32gui import *
import win32con
import sys, os
import struct
import time
import threading
import cmd
from os.path import join, dirname, abspath
# Class
class WindowsBalloonTip:
def __init__(self, title, msg):
message_map = { win32con.WM_DESTROY: self.OnDestroy,}
# Register the window class.
wc = WNDCLASS()
hinst = wc.hInstance = GetModuleHandle(None)
wc.lpszClassName = 'PythonTaskbar'
wc.lpfnWndProc = message_map # could also specify a wndproc.
while True:
try:
classAtom = RegisterClass(wc)
break
except:
continue
# Create the window.
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = CreateWindow(classAtom, "Taskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None)
UpdateWindow(self.hwnd)
# Icons managment
iconPathName = join(dirname(abspath(__file__)), 'ff.png')
#print iconPathName
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
try:
hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags)
except:
hicon = LoadIcon(0, win32con.IDI_APPLICATION)
flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, 'Tooltip')
# Notify
Shell_NotifyIcon(NIM_ADD, nid)
Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20, hicon, 'Balloon Tooltip', msg, 200, title))
# self.show_balloon(title, msg)
time.sleep(5)
# Destroy
DestroyWindow(self.hwnd)
classAtom = UnregisterClass(classAtom, hinst)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
Shell_NotifyIcon(NIM_DELETE, nid)
#PostQuitMessage(0) # Terminate the app.
# Function
class myThread(threading.Thread):
def __init__(self, title, msg):
threading.Thread.__init__(self)
self.title = title
self.msg = msg
def run(self):
w = WindowsBalloonTip(self.title, self.msg)
def balloon_tip(title, msg):
thread1 = myThread(title, msg)
thread1.start()
return
#w=WindowsBalloonTip(title, msg)
class Notification():
def __init__(self):
pass
def send_notification(self, title, text):
balloon_tip(title, msg)

View File

@ -7,3 +7,8 @@ mailbox=
[locations]
library=
input=
[runner]
notification=
pushbullet=
pbdevice=

BIN
ff.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

21
giNotify.py Normal file
View File

@ -0,0 +1,21 @@
from gi import require_version as gir
gir('Notify', '0.7')
from gi.repository import GObject
from gi.repository import Notify
import sys
from os.path import join, dirname, abspath
class Notification(GObject.Object):
def __init__(self):
super(Notification, self).__init__()
# lets initialise with the application name
Notify.init("Fanfiction")
self.icon = join(dirname(abspath(__file__)), 'ff.png')
def send_notification(self, title, text):
n = Notify.Notification.new(title, text, self.icon)
n.show()

85
runner_notify.py Normal file
View File

@ -0,0 +1,85 @@
from StringIO import StringIO
import re
from subprocess import check_output, STDOUT
from sys import platform
from os import utime
if platform == "linux" or platform == "linux2":
from giNotify import Notification
elif platform == "win32":
from ballonNotify import Notification
from optparse import OptionParser
from ConfigParser import ConfigParser
def enable_notifications(options):
notary_options = []
if options.pushbullet:
pb = Pushbullet(options.pushbullet)
if options.pbdevice:
try:
pb = pb.get_device(options.pbdevice)
except:
print "Cannot get this device."
pass
temp_note = Notification()
temp_note.send_notification = pb.push_note
notary_options.append(temp_note)
if options.notify:
notary = Notification()
notary_options.append(notary)
return notary_options
def touch(fname, times=None):
with open(fname, 'a'):
utime(fname, times)
def main(options):
res = check_output("python fanficdownload.py -c config.ini", shell=True,stderr=STDOUT)
buf = StringIO(res)
regex = re.compile("Added (?:.*/)?(.*)-.* to library with id \d*")
for line in buf.readlines():
r = regex.search(line)
if r:
story = r.group(1).strip()
for notify in enable_notifications(options):
notify.send_notification("New Fanfiction Download", story)
if res != "": print res
return
if __name__ == "__main__":
option_parser = OptionParser(usage="usage: %prog [flags]")
option_parser.add_option('-p', '--pushbullet', action='store', dest='pushbullet', help='If you want to use pushbullet, pass in your key here.')
option_parser.add_option('-d', '--device', action='store', dest='pbdevice', help='If you wish to only send to a certian pushbullet device, put the device name here. If the device name is invalid, will just send to all pushbullets associated with the acc')
option_parser.add_option('-n', '--notify', action='store_true', dest='notify', help='Enable if you want to use system notifications. Only for Win/Linux.')
option_parser.add_option('-c', '--config', action='store', dest='config', help='Config file for inputs. Blank config file is provided. No default. If an option is present in whatever config file is passed it, the option will overwrite whatever is passed in through command line arguments unless the option is blank. Do not put any quotation marks in the options.')
(options, args) = option_parser.parse_args()
if options.config:
touch(options.config)
config = ConfigParser(allow_no_value=True)
config.read(options.config)
updater = lambda option, newval : newval if newval != "" else option
try: options.notify = updater(options.notify, config.getboolean('runner', 'notification'))
except: pass
try: options.pushbullet = updater(options.pushbullet, config.get('runner', 'pushbullet'))
except: pass
try: options.pbdevice = updater(options.pbdevice, config.get('runner', 'pbdevice'))
except: pass
if options.pbdevice and not options.pushbullet:
raise ValueError("Can't use a pushbullet device without key")
if options.pushbullet:
from pushbullet import Pushbullet
main(options)