diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 84f3770f..3542efce 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -1145,18 +1145,9 @@
- +
-
- -
-
- Enter the path/application name to be registered with the Notification Center, default is /Applications/Headphones -
-
- -
diff --git a/headphones/config.py b/headphones/config.py index 7f29d50e..b86ae80f 100644 --- a/headphones/config.py +++ b/headphones/config.py @@ -197,7 +197,6 @@ _CONFIG_DEFINITIONS = { 'OMGWTFNZBS_UID': (str, 'omgwtfnzbs', ''), 'OPEN_MAGNET_LINKS': (int, 'General', 0), # 0: Ignore, 1: Open, 2: Convert, 3: Embed (rtorrent) 'MAGNET_LINKS': (int, 'General', 0), - 'OSX_NOTIFY_APP': (str, 'OSX_Notify', '/Applications/Headphones'), 'OSX_NOTIFY_ENABLED': (int, 'OSX_Notify', 0), 'OSX_NOTIFY_ONSNATCH': (int, 'OSX_Notify', 0), 'PIRATEBAY': (int, 'Piratebay', 0), diff --git a/headphones/notifiers.py b/headphones/notifiers.py index 95ac9cc9..64a8eba3 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -840,78 +840,14 @@ class TwitterNotifier(object): class OSX_NOTIFY(object): - def __init__(self): + def notify(self, title, subtitle): try: - self.objc = __import__("objc") - self.AppKit = __import__("AppKit") - except: - logger.warn('OS X Notification: Cannot import objc or AppKit') - pass - - def swizzle(self, cls, SEL, func): - old_IMP = getattr(cls, SEL, None) - if old_IMP is None: - old_IMP = cls.instanceMethodForSelector_(SEL) - - def wrapper(self, *args, **kwargs): - return func(self, old_IMP, *args, **kwargs) - - new_IMP = self.objc.selector( - wrapper, - selector=old_IMP.selector, - signature=old_IMP.signature - ) - self.objc.classAddMethod(cls, SEL.encode(), new_IMP) - - def notify(self, title, subtitle=None, text=None, sound=True, image=None): - - try: - self.swizzle( - self.objc.lookUpClass('NSBundle'), - 'bundleIdentifier', - self.swizzled_bundleIdentifier - ) - - NSUserNotification = self.objc.lookUpClass('NSUserNotification') - NSUserNotificationCenter = self.objc.lookUpClass( - 'NSUserNotificationCenter') - NSAutoreleasePool = self.objc.lookUpClass('NSAutoreleasePool') - - if not NSUserNotification or not NSUserNotificationCenter: - return False - - pool = NSAutoreleasePool.alloc().init() - - notification = NSUserNotification.alloc().init() - notification.setTitle_(title) - if subtitle: - notification.setSubtitle_(subtitle) - if text: - notification.setInformativeText_(text) - if sound: - notification.setSoundName_( - "NSUserNotificationDefaultSoundName") - if image: - source_img = self.AppKit.NSImage.alloc().\ - initByReferencingFile_(image) - notification.setContentImage_(source_img) - # notification.set_identityImage_(source_img) - notification.setHasActionButton_(False) - - notification_center = NSUserNotificationCenter.\ - defaultUserNotificationCenter() - notification_center.deliverNotification_(notification) - - del pool - return True - + script = f'display notification "{subtitle}" with title "{title}"' + subprocess.run(["osascript", "-e", script]) except Exception as e: - logger.warn('Error sending OS X Notification: %s' % e) + logger.warn(f"Error sending MacOS Notification: {e}") return False - def swizzled_bundleIdentifier(self, original, swizzled): - return 'ade.headphones.osxnotify' - class BOXCAR(object): def __init__(self): diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index c6db9d4f..eafb255f 100755 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -623,15 +623,9 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list, #twitter.notify_download(pushmessage) if headphones.CONFIG.OSX_NOTIFY_ENABLED: - from headphones import cache - c = cache.Cache() - album_art = c.get_artwork_from_cache(None, release['AlbumID']) - logger.info("Sending OS X notification") - osx_notify = notifiers.OSX_NOTIFY() - osx_notify.notify(release['ArtistName'], - release['AlbumTitle'], - statusmessage, - image=album_art) + logger.info("Sending MacOS notification") + osx = notifiers.OSX_NOTIFY() + osx.notify(f"Headphones Processed", f"{pushmessage}\n{statusmessage}") if headphones.CONFIG.BOXCAR_ENABLED: logger.info("Sending Boxcar2 notification") diff --git a/headphones/searcher.py b/headphones/searcher.py index fbe616f9..c7dedb7b 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -1228,16 +1228,12 @@ def send_to_downloader(data, result, album): logger.info("Sending Pushalot notification") pushalot = notifiers.PUSHALOT() pushalot.notify(name, "Download started") + if headphones.CONFIG.OSX_NOTIFY_ENABLED and headphones.CONFIG.OSX_NOTIFY_ONSNATCH: - from headphones import cache - c = cache.Cache() - album_art = c.get_artwork_from_cache(None, rgid) - logger.info("Sending OS X notification") - osx_notify = notifiers.OSX_NOTIFY() - osx_notify.notify(artist, - albumname, - 'Snatched: ' + provider + '. ' + name, - image=album_art) + logger.info("Sending MacOS notification") + osx = notifiers.OSX_NOTIFY() + osx.notify(f"Headphones Snatched", f"{artist} - {albumname}\nFrom {provider}, {name}") + if headphones.CONFIG.BOXCAR_ENABLED and headphones.CONFIG.BOXCAR_ONSNATCH: logger.info("Sending Boxcar2 notification") b2msg = 'From ' + provider + '

' + name diff --git a/headphones/webserve.py b/headphones/webserve.py index 7de97ddd..57cc856e 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -1369,7 +1369,6 @@ class WebInterface(object): "twitter_onsnatch": checked(headphones.CONFIG.TWITTER_ONSNATCH), "osx_notify_enabled": checked(headphones.CONFIG.OSX_NOTIFY_ENABLED), "osx_notify_onsnatch": checked(headphones.CONFIG.OSX_NOTIFY_ONSNATCH), - "osx_notify_app": headphones.CONFIG.OSX_NOTIFY_APP, "boxcar_enabled": checked(headphones.CONFIG.BOXCAR_ENABLED), "boxcar_onsnatch": checked(headphones.CONFIG.BOXCAR_ONSNATCH), "boxcar_token": headphones.CONFIG.BOXCAR_TOKEN, @@ -1718,20 +1717,6 @@ class WebInterface(object): else: return "Error sending tweet" - @cherrypy.expose - def osxnotifyregister(self, app): - cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store" - from osxnotify import registerapp as osxnotify - result, msg = osxnotify.registerapp(app) - if result: - osx_notify = notifiers.OSX_NOTIFY() - osx_notify.notify('Registered', result, 'Success :-)') - logger.info( - 'Registered %s, to re-register a different app, delete this app first' % result) - else: - logger.warn(msg) - return msg - @cherrypy.expose def testPushover(self): logger.info("Sending Pushover notification") diff --git a/lib/osxnotify/__init__.py b/lib/osxnotify/__init__.py deleted file mode 100755 index e69de29b..00000000 diff --git a/lib/osxnotify/appIcon.icns b/lib/osxnotify/appIcon.icns deleted file mode 100755 index caf5b293..00000000 Binary files a/lib/osxnotify/appIcon.icns and /dev/null differ diff --git a/lib/osxnotify/registerapp.py b/lib/osxnotify/registerapp.py deleted file mode 100644 index ed9d4b5b..00000000 --- a/lib/osxnotify/registerapp.py +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/python - -import shutil -import os -import stat -import platform -import subprocess - -def registerapp(app): - - # don't do any of this unless >= 10.8 - if not [int(n) for n in platform.mac_ver()[0].split('.')] >= [10, 8]: - return None, 'Registering requires OS X version >= 10.8' - - app_path = None - - # check app bundle doesn't already exist - app_path = subprocess.check_output(['/usr/bin/mdfind', 'kMDItemCFBundleIdentifier == "ade.headphones.osxnotify"']).strip() - if app_path: - return app_path, 'App previously registered' - - # check app doesn't already exist - app = app.strip() - if not app: - return None, 'Path/Application not entered' - if os.path.splitext(app)[1] == ".app": - app_path = app - else: - app_path = app + '.app' - if os.path.exists(app_path): - return None, 'App %s already exists, choose a different name' % app_path - - # generate app - try: - os.mkdir(app_path) - os.mkdir(app_path + "/Contents") - os.mkdir(app_path + "/Contents/MacOS") - os.mkdir(app_path + "/Contents/Resources") - shutil.copy(os.path.join(os.path.dirname(__file__), "appIcon.icns"), app_path + "/Contents/Resources/") - - version = "1.0.0" - bundleName = "OSXNotify" - bundleIdentifier = "ade.headphones.osxnotify" - - f = open(app_path + "/Contents/Info.plist", "w") - f.write(""" - - - - CFBundleDevelopmentRegion - English - CFBundleExecutable - main.py - CFBundleGetInfoString - %s - CFBundleIconFile - appIcon.icns - CFBundleIdentifier - %s - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - %s - CFBundlePackageType - APPL - CFBundleShortVersionString - %s - CFBundleSignature - ???? - CFBundleVersion - %s - NSAppleScriptEnabled - YES - NSMainNibFile - MainMenu - NSPrincipalClass - NSApplication - - -""" % (bundleName + " " + version, bundleIdentifier, bundleName, bundleName + " " + version, version)) - f.close() - - f = open(app_path + "/Contents/PkgInfo", "w") - f.write("APPL????") - f.close() - - f = open(app_path + "/Contents/MacOS/main.py", "w") - f.write("""#!/usr/bin/python - -objc = None - -def swizzle(cls, SEL, func): - old_IMP = cls.instanceMethodForSelector_(SEL) - def wrapper(self, *args, **kwargs): - return func(self, old_IMP, *args, **kwargs) - new_IMP = objc.selector(wrapper, selector=old_IMP.selector, - signature=old_IMP.signature) - objc.classAddMethod(cls, SEL, new_IMP) - -def notify(title, subtitle=None, text=None, sound=True): - global objc - objc = __import__("objc") - swizzle(objc.lookUpClass('NSBundle'), - b'bundleIdentifier', - swizzled_bundleIdentifier) - NSUserNotification = objc.lookUpClass('NSUserNotification') - NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter') - NSAutoreleasePool = objc.lookUpClass('NSAutoreleasePool') - pool = NSAutoreleasePool.alloc().init() - notification = NSUserNotification.alloc().init() - notification.setTitle_(title) - notification.setSubtitle_(subtitle) - notification.setInformativeText_(text) - notification.setSoundName_("NSUserNotificationDefaultSoundName") - notification_center = NSUserNotificationCenter.defaultUserNotificationCenter() - notification_center.deliverNotification_(notification) - del pool - -def swizzled_bundleIdentifier(self, original): - return 'ade.headphones.osxnotify' - -if __name__ == '__main__': - notify('Half Man Half Biscuit', 'Back in the DHSS', '99% Of Gargoyles Look Like Bob Todd') -""") - f.close() - - oldmode = os.stat(app_path + "/Contents/MacOS/main.py").st_mode - os.chmod(app_path + "/Contents/MacOS/main.py", oldmode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) - - return app_path, 'App registered' - - except Exception as e: - return None, 'Error creating App %s. %s' % (app_path, e) \ No newline at end of file