#!/usr/bin/env python
# encoding=utf-8

"""ardj metadata source for ezstream.

To let ezstream retrieve track metadata, install this script as follows:

    <metadata_progname>/this/script</metadata_progname>
    <metadata_format>@t@</metadata_format>
"""

import os
import sys

# Support running from the source tree.
my_name = os.path.realpath(__file__)
if my_name.startswith(os.path.expanduser("~/")):
    modules = os.path.join(os.path.dirname(os.path.dirname(my_name)), "src")
    sys.path.insert(0, modules)

from ardj.tracks import get_last_track


def notify(track):
    try:
        import gtk
        import pynotify

        pynotify.init("ardj")
        n = pynotify.Notification(summary=track["artist"], message=track["title"])
        n.set_icon_from_pixbuf(gtk.Label().render_icon(gtk.STOCK_CDROM, gtk.ICON_SIZE_LARGE_TOOLBAR))
        n.show()
    except ImportError:
        pass


try:
    if len(sys.argv) != 2:
        print "ardj is playing"
        exit(0)

    track = get_last_track()
    if track is None:
        print "unknown track"
        exit(0)

    if sys.argv[1] == "artist":
        print track["artist"].encode("utf-8")
    elif sys.argv[1] == "title":
        print track["title"].encode("utf-8")
        notify(track)
    else:
        print "unknown"
except:
    print "error"
