
#!/usr/bin/env python
import os
import sys
from gi.repository import Gst as gst
from gi.repository import GObject as gobject

class tag_getter:
    def __init__(self):
        #make a dictionary to hold our tag info
        self.file_tags = {}
        #make a playbin to parse the audio file
        gst.init_check(None)
        self.pbin = gst.ElementFactory.make("playbin")
        #we need to receive signals from the playbin's bus
        self.bus = self.pbin.get_bus()
        #make sure we are watching the signals on the bus
        self.bus.add_signal_watch()
        #what do we do when a tag is part of the bus signal?
        self.bus.connect("message::tag", self.bus_message_tag)
        #create a loop to control our app
        self.mainloop = gobject.MainLoop()
        
    def bus_message_tag (self, bus, message):
        #we received a tag message
        taglist = message.parse_tag()
        #put the keys in the dictionary
        #for key in taglist.keys():
        
        for x in range(taglist.n_tags()):
            name = taglist.nth_tag_name(x)
            print('  %s: %s' % (name, taglist.get_string(name)[1]))
            if name == "date":
                datetrue, date = taglist.get_date(name)
                if datetrue:
                    print "this is the year of the track: %d" % date.get_year()
            
            if taglist.get_string(name)[0]:
                self.file_tags[name] = taglist.get_string(name)[1]
                    
        if self.file_tags['artist']:
            print self.file_tags
            sys.exit()
        
    def set_file(self,file):
        #set the uri of the playbin to our audio file
        self.pbin.set_property("uri","file://"+file)
        #pause the playbin, we don't really need to play
        self.pbin.set_state(gst.State.PAUSED)
        
    def run(self):
        #start the main loop
        self.mainloop.run()

if __name__=="__main__":
    if len(sys.argv)>1:
        file = sys.argv[1]
        pwd = os.getcwd()
        filepath = os.path.join(pwd,file)
        getter = tag_getter()
        getter.set_file(file)
        getter.run()

    else:
        print "select an audio file"
