iTunes Class (iTclass)

By RJosh on Dec 02, 2009

I had an idea of making a lyrics GUI for iTunes and the current track. I got as far as making the object to retrieve the info and get the lyrics, and then stopped.

Get DEMO

get(ITEM) -> ITEM_VAL (IE: get(Artist) -> CURRENT_ARTIST)
get() -> Artist='CURRENT_ARTIST' Track='CURRENT_SONG' Album='CURRENT_ALBUM' Year='ALBUM_YEAR' Position='CURRENT_POSITION/LENGTH_OF_SONG' [PERCENTAGE_FINISHED]

Lyrics DEMO

lyrics('ARTIST','SONG') -> (prints the lyrics)

lyrics() -> (grabs the current playing artist/song using the itunes.get function) (prints lyrics)

PROTIP:
× Uncomment the import for itunes in the lyrics function if you plan on saving them in different files. I made them at different times so i kept them seperated for a specific reason. What that was i forget now.
× You will also need PyWin32 for th WIN32 COM MODULE which is available here http://sourceforge.net/projects/pywin32/

EDIT:

Completely re-visited this subject made it into a class.

To use:

import MODULE_NAME

Note: If you choose to import iTunes from MODULE_NAME, the MODULE_NAME will not

     be required for the following call procedure.

var = MODULE_NAME.iTunes()
print var.DEF() #Use the following to call various methods within the class

class iTunes:
    """iTunes control class"""

    def __init__(self):
        import win32com.client
        self.cTrack = win32com.client.Dispatch("iTunes.Application")

    def isOpen(self):
        from win32com.client import GetObject
        WMI  = GetObject("winmgmts:")
        proc = WMI.ExecQuery("SELECT * FROM Win32_Process WHERE Name='iTunes.exe'")
        if proc[0].Name == "iTunes.exe":
            return 1
        else:
            return 0

    def cTrackInfo(self, obj = None):
        song = self.cTrack.CurrentTrack
        if obj is not None:
            return getattr(song, obj)
        else:
            pos  = self.cTrack.PlayerPosition
            pd   = int(round(float(pos)/self.revDur(song.time)*100, 0))
            return "[iTunes] {0}. {1} by {2} ({3}/{4}/{5}kbps)({6}/{7})[{8}%]".format(song.TrackNumber, song.name, song.artist, song.album, song.year, song.bitrate,
                                                                                      self.duration(pos), song.time, pd)

    def Lyrics(self, artist = None, track = None, ret = 0):
        import urllib, urllib2
        if artist is None: 
            artist    = self.cTrackInfo("artist")
        if track is None: 
            track     = self.cTrackInfo("name")
        s             = "http://josh.phantomnet.net/public/lyrics.php?{0}".format(urllib.urlencode([("a", artist), ("t", track), ("raw", 1)]))
        lyrics        = urllib2.urlopen(urllib2.Request(s))
        itemsToRemove = ["<br />", "\r\n", "<br />\r\n", "@@START", "@@END", "<pre>"]
        if ret == 1:
            return lyrics
        else:
            for line in lyrics:
                for item in itemsToRemove:
                    line = line.replace(item, '')
                print line

    def revDur(self, time):
        min, sec = map(int, time.split(':')[:2])
        ctime    = min*60+sec
        return ctime

    def duration(self, seconds):
        hours    = seconds / 3600
        seconds -= 3600*hours
        minutes  = seconds / 60
        seconds -= 60*minutes
        if hours == 0:
            return "%02d:%02d" % (minutes, seconds)
        return "%02d:%02d:%02d" % (hours, minutes, seconds)

    def Next(self): self.cTrack.nexttrack()
    def Previous(self): self.cTrack.previoustrack() 
    def Pause(self): self.cTrack.pause()    
    def Play(self): self.cTrack.play()  
    def Stop(self): self.cTrack.stop()

Comments

Sign in to comment.
Jonesy44   -  Dec 02, 2009

Nice one, big fan of integrating apps, especially iTunes hehe, as much as i love to hate it. well done! :)

 Respond  
RJosh   -  Dec 02, 2009

If it was it's news to me. But I Highly doubt it. This is just a COM snippet made in python to get the current track information.

 Respond  
^Neptune   -  Dec 02, 2009

iTunes was written in python?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.