Created
February 7, 2016 09:54
-
-
Save testeddoughnut/fd2241c41640b16537b1 to your computer and use it in GitHub Desktop.
Libnotify eventcmd for pianobar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from gi.repository import Notify | |
from urllib.parse import urlparse | |
import os | |
import requests | |
import sys | |
TMP_DIR = os.path.expanduser("~/.config/pianobar/tmp/") | |
DEFAULT_ICON = os.path.expanduser("~/.config/pianobar/pandora-transparent.png") | |
def main(): | |
fields = dict([l.strip().split("=", 1) for l in sys.stdin.readlines()]) | |
artist = fields["artist"] | |
title = fields["title"] | |
album = fields["album"] | |
cover_art_url = fields["coverArt"] | |
filename = TMP_DIR + urlparse(cover_art_url).path.split("/")[-1] | |
if sys.argv[1] == "songstart": | |
Notify.init("pianobar") | |
try: | |
r = requests.get(cover_art_url) | |
with open(filename, "wb") as fd: | |
fd.write(r.content) | |
icon = filename | |
except: | |
icon = DEFAULT_ICON | |
notification = Notify.Notification.new( | |
summary="Now playing on pianobar:", | |
body='"%s" by "%s" on "%s"' % (title, artist, album), | |
icon=icon) | |
notification.show() | |
if sys.argv[1] == "songfinish": | |
# cleanup old albumart | |
if os.path.isfile(filename): | |
os.remove(filename) | |
if sys.argv[1] == "userlogin": | |
if os.path.isdir(TMP_DIR): | |
# cleanup the tmp dir... | |
for oldfile in os.listdir(TMP_DIR): | |
file_path = os.path.join(TMP_DIR, oldfile) | |
os.remove(file_path) | |
else: | |
# make the temp dir if it doesn't exist | |
os.mkdir(TMP_DIR) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment