Pre-eden
[clinton/xbmc-groove.git] / default.py
index d4c6dfa..34f3814 100644 (file)
@@ -1,4 +1,31 @@
-import urllib, sys, os, shutil, re, time, xbmcaddon, xbmcplugin, xbmcgui, xbmc, pickle
+# Copyright 2011 Stephen Denham
+
+#    This file is part of xbmc-groove.
+#
+#    xbmc-groove is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    xbmc-groove is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with xbmc-groove.  If not, see <http://www.gnu.org/licenses/>.
+
+
+import urllib, sys, os, shutil, re, pickle, time, tempfile, xbmcaddon, xbmcplugin, xbmcgui, xbmc
+
+__addon__     = xbmcaddon.Addon('plugin.audio.groove')
+__addonname__ = __addon__.getAddonInfo('name')
+__cwd__       = __addon__.getAddonInfo('path')
+__author__    = __addon__.getAddonInfo('author')
+__version__   = __addon__.getAddonInfo('version')
+__language__  = __addon__.getLocalizedString
+__debugging__  = __addon__.getSetting('debug')
+
 MODE_SEARCH_SONGS = 1
 MODE_SEARCH_ALBUMS = 2
 MODE_SEARCH_ARTISTS = 3
@@ -34,38 +61,70 @@ ACTION_PREVIOUS_MENU = 10
 ARTIST_ALBUM_NAME_LABEL = 0
 NAME_ALBUM_ARTIST_LABEL = 1
 
-baseDir = os.getcwd()
+# Stream marking time (seconds)
+STREAM_MARKING_TIME = 30
+
+# Timeout
+STREAM_TIMEOUT = 30
+
+songMarkTime = 0
+player = xbmc.Player()
+playTimer = None
+
+baseDir = __cwd__
 resDir = xbmc.translatePath(os.path.join(baseDir, 'resources'))
 libDir = xbmc.translatePath(os.path.join(resDir,  'lib'))
 imgDir = xbmc.translatePath(os.path.join(resDir,  'img'))
-cacheDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(os.getcwd()))
+cacheDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(baseDir))
 thumbDirName = 'thumb'
-thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.basename(os.getcwd()), thumbDirName)
+thumbDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(baseDir), thumbDirName)
 
 baseModeUrl = 'plugin://plugin.audio.groove/'
 playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST)
 playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS)
 favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES)
 
-searchArtistsAlbumsName = "Search for artist's albums..."
+searchArtistsAlbumsName = __language__(30006)
 
 thumbDef = os.path.join(imgDir, 'default.tbn')
 listBackground = os.path.join(imgDir, 'listbackground.png')
 
 sys.path.append (libDir)
 from GroovesharkAPI import GrooveAPI
-from GroovesharkAPI import GrooveAPIv1
+from threading import Event, Thread
+
+if __debugging__ == 'true':
+    __debugging__ = True
+else:
+    __debugging__ = False
 
 try:
-    groovesharkApi = GrooveAPI()
+    groovesharkApi = GrooveAPI(__debugging__)
     if groovesharkApi.pingService() != True:
-        raise StandardError('No Grooveshark service')
+        raise StandardError(__language__(30007))
 except:
-    dialog = xbmcgui.Dialog()
-    dialog.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later')
+    dialog = xbmcgui.Dialog(__language__(30008),__language__(30009),__language__(30010))
+    dialog.ok()
     sys.exit(-1)
   
-
+# Mark song as playing or played
+def markSong(songid, duration, streamKey, streamServerID):
+    global songMarkTime
+    global playTimer
+    global player
+    if player.isPlayingAudio():
+        tNow = player.getTime()
+        if tNow >= STREAM_MARKING_TIME and songMarkTime == 0:
+            groovesharkApi.markStreamKeyOver30Secs(streamKey, streamServerID)
+            songMarkTime = tNow
+        elif duration > tNow and duration - tNow < 2 and songMarkTime >= STREAM_MARKING_TIME:
+            playTimer.cancel()
+            songMarkTime = 0
+            groovesharkApi.markSongComplete(songid, streamKey, streamServerID)
+    else:
+        playTimer.cancel()
+        songMarkTime = 0
+            
 class _Info:
     def __init__( self, *args, **kwargs ):
         self.__dict__.update( kwargs )
@@ -94,7 +153,7 @@ class GroovesharkPlaylistSelect(xbmcgui.WindowDialog):
         listitems = []
         for playlist in items:
             listitems.append(xbmcgui.ListItem(playlist[0]))
-        listitems.append(xbmcgui.ListItem('New...'))
+        listitems.append(xbmcgui.ListItem(__language__(30011)))
         self.playlistControl.addItems(listitems)
         self.setFocus(self.playlistControl)
         self.playlistControl.selectItem(0)
@@ -131,8 +190,45 @@ class GroovesharkPlaylistSelect(xbmcgui.WindowDialog):
         elif action == ACTION_MOVE_UP or action == ACTION_MOVE_DOWN or action == ACTION_PAGE_UP or action == ACTION_PAGE_DOWN == 6:
             self.setFocus(self.playlistControl)
             self.setHighlight()
-     
-class Groveshark:
+
+class PlayTimer(Thread):
+    # interval -- floating point number specifying the number of seconds to wait before executing function
+    # function -- the function (or callable object) to be executed
+
+    # iterations -- integer specifying the number of iterations to perform
+    # args -- list of positional arguments passed to function
+    # kwargs -- dictionary of keyword arguments passed to function
+    
+    def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
+        Thread.__init__(self)
+        self.interval = interval
+        self.function = function
+        self.iterations = iterations
+        self.args = args
+        self.kwargs = kwargs
+        self.finished = Event()
+    def run(self):
+        count = 0
+        while not self.finished.isSet() and (self.iterations <= 0 or count < self.iterations):
+            self.finished.wait(self.interval)
+            if not self.finished.isSet():
+                self.function(*self.args, **self.kwargs)
+                count += 1
+    def cancel(self):
+        self.finished.set()
+    
+    def setIterations(self, iterations):
+        self.iterations = iterations
+        
+
+    def getTime(self):
+        return self.iterations * self.interval
+
+
+class Grooveshark:
     
     albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png'))
     artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png'))
@@ -144,7 +240,7 @@ class Groveshark:
     popularSongsArtistImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongsArtist.png'))
     songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png'))
     defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn'))
-    fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png'))
+    fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.jpg'))
 
     settings = xbmcaddon.Addon(id='plugin.audio.groove')
     songsearchlimit = int(settings.getSetting('songsearchlimit'))
@@ -153,20 +249,20 @@ class Groveshark:
     songspagelimit = int(settings.getSetting('songspagelimit'))
     username = settings.getSetting('username')
     password = settings.getSetting('password')
-    sessionidv1 = settings.getSetting('sessionidv1')
+
     userid = 0
     
     def __init__( self ):
         self._handle = int(sys.argv[1])
         if os.path.isdir(cacheDir) == False:
             os.makedirs(cacheDir)
-            xbmc.log("Made " + cacheDir)
+            if __debugging__ :
+                xbmc.log(__language__(30012) + " " + cacheDir)
         artDir = xbmc.translatePath(thumbDir)
         if os.path.isdir(artDir) == False:
             os.makedirs(artDir)
-            xbmc.log("Made " + artDir)
-            
-        self.groovesharkApiv1 = GrooveAPIv1(self.sessionidv1)
+            if __debugging__ :
+                xbmc.log(__language__(30012) + " " + artDir)
             
     # Top-level menu
     def categories(self):
@@ -176,69 +272,70 @@ class Groveshark:
         # Setup
         xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
         
-        self._add_dir('Search for songs...', '', MODE_SEARCH_SONGS, self.songImg, 0)
-        self._add_dir('Search for albums...', '', MODE_SEARCH_ALBUMS, self.albumImg, 0)
-        self._add_dir('Search for artists...', '', MODE_SEARCH_ARTISTS, self.artistImg, 0)
+        self._add_dir(__language__(30013), '', MODE_SEARCH_SONGS, self.songImg, 0)
+        self._add_dir(__language__(30014), '', MODE_SEARCH_ALBUMS, self.albumImg, 0)
+        self._add_dir(__language__(30015), '', MODE_SEARCH_ARTISTS, self.artistImg, 0)
         self._add_dir(searchArtistsAlbumsName, '', MODE_SEARCH_ARTISTS_ALBUMS, self.artistsAlbumsImg, 0)
-        self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0)
-        self._add_dir('Popular songs for artist...', '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0)
-        self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
+        # Not supported by key
+        #self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0)
+        self._add_dir(__language__(30016), '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0)
+        self._add_dir(__language__(30017), '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
         if (self.userid != 0):
-            self._add_dir('My favorites', '', MODE_FAVORITES, self.favoritesImg, 0)
-            self._add_dir('My playlists', '', MODE_PLAYLISTS, self.playlistImg, 0)
+            self._add_dir(__language__(30018), '', MODE_FAVORITES, self.favoritesImg, 0)
+            self._add_dir(__language__(30019), '', MODE_PLAYLISTS, self.playlistImg, 0)
 
     # Search for songs            
     def searchSongs(self):
-        query = self._get_keyboard(default="", heading="Search for songs")
+        query = self._get_keyboard(default="", heading=__language__(30020))
         if (query != ''):
             songs = groovesharkApi.getSongSearchResults(query, limit = self.songsearchlimit)
             if (len(songs) > 0):
                 self._add_songs_directory(songs)
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'No matching songs.')
+                dialog.ok(__language__(30008), __language__(30021))
                 self.categories()
         else:
             self.categories()
     
     # Search for albums
     def searchAlbums(self):
-        query = self._get_keyboard(default="", heading="Search for albums")
+        query = self._get_keyboard(default="", heading=__language__(30022))
         if (query != ''): 
             albums = groovesharkApi.getAlbumSearchResults(query, limit = self.albumsearchlimit)
             if (len(albums) > 0):
                 self._add_albums_directory(albums)
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'No matching albums.')
+                dialog.ok(__language__(30008), __language__(30023))
                 self.categories()
         else:
             self.categories()
     
     # Search for artists
     def searchArtists(self):
-        query = self._get_keyboard(default="", heading="Search for artists")
+        query = self._get_keyboard(default="", heading=__language__(30024))
         if (query != ''): 
             artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
             if (len(artists) > 0):
                 self._add_artists_directory(artists)
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'No matching artists.')
+                dialog.ok(__language__(30008), __language__(30025))
                 self.categories()
         else:
             self.categories()
 
     # Search for playlists
     def searchPlaylists(self):
-        query = self._get_keyboard(default="", heading="Username")
+        query = self._get_keyboard(default="", heading=__language__(30026))
         if (query != ''): 
-            playlists = groovesharkApi.getUserPlaylistsEx(query)
+            playlists = groovesharkApi.getUserPlaylistsByUsername(query)
             if (len(playlists) > 0):
                 self._add_playlists_directory(playlists)
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'No Grooveshark playlists found.')
+                dialog.ok(__language__(30008), __language__(30027))
                 self.categories()
         else:
             self.categories()
@@ -246,7 +343,7 @@ class Groveshark:
     # Search for artists albums
     def searchArtistsAlbums(self, artistName = None):
         if artistName == None or artistName == searchArtistsAlbumsName:
-            query = self._get_keyboard(default="", heading="Search for artist's albums")
+            query = self._get_keyboard(default="", heading=__language__(30028))
         else:
             query = artistName
         if (query != ''): 
@@ -254,17 +351,18 @@ class Groveshark:
             if (len(artists) > 0):
                 artist = artists[0]
                 artistID = artist[1]
-                xbmc.log("Found " + artist[0] + "...")
-                albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit)
+                if __debugging__ :
+                    xbmc.log("Found " + artist[0] + "...")
+                albums = groovesharkApi.getArtistAlbums(artistID, self.albumsearchlimit)
                 if (len(albums) > 0):
                     self._add_albums_directory(albums, artistID)
                 else:
                     dialog = xbmcgui.Dialog()
-                    dialog.ok('Grooveshark XBMC', 'No matching albums.')
+                    dialog.ok(__language__(30008), __language__(30029))
                     self.categories()
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'No matching artists.')
+                dialog.ok(__language__(30008), __language__(30030))
                 self.categories()
         else:
             self.categories()
@@ -278,7 +376,7 @@ class Groveshark:
                 self._add_songs_directory(favorites, isFavorites=True)
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'You have no favorites.')
+                dialog.ok(__language__(30008), __language__(30031))
                 self.categories()
     
     # Get popular songs
@@ -288,7 +386,7 @@ class Groveshark:
             self._add_songs_directory(popular)
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'No popular songs.')
+            dialog.ok(__language__(30008), __language__(30032))
             self.categories()
 
     # Get my playlists             
@@ -300,36 +398,38 @@ class Groveshark:
                 self._add_playlists_directory(playlists)
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
+                dialog.ok(__language__(30008), __language__(30033))
                 self.categories()
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30035))
                 
     # Make songs a favorite 
     def favorite(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Favorite song: " + str(songid))
+            if __debugging__ :
+                xbmc.log("Favorite song: " + str(songid))
             groovesharkApi.addUserFavoriteSong(songID = songid)
-            xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')')
+            xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef + ')')
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30037))
             
     # Remove song from favorites
     def unfavorite(self, songid, prevMode=0):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
-            self.groovesharkApiv1.unfavorite(songID = songid)
-            xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed from Grooveshark favorites, 1000, ' + thumbDef + ')')
+            if __debugging__ :
+                xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
+            groovesharkApi.removeUserFavoriteSongs(songIDs = songid)
+            xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30038) + ', 1000, ' + thumbDef + ')')
             # Refresh to remove item from directory
             if (int(prevMode) == MODE_FAVORITES):
                 xbmc.executebuiltin("Container.Refresh(" + favoritesUrl + ")")
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to remove Grooveshark favorites.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30039))
             
 
     # Show selected album
@@ -340,7 +440,7 @@ class Groveshark:
     # Show selected artist
     def artist(self, artistid):
         albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit)
-        self._add_albums_directory(albums, artistid)
+        self._add_albums_directory(albums, artistid, True)
     
     # Show selected playlist
     def playlist(self, playlistid, playlistname):
@@ -350,42 +450,71 @@ class Groveshark:
             self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL, playlistid=playlistid, playlistname=playlistname)
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30040))
             
     # Show popular songs of the artist
     def artistPopularSongs(self):
-        query = self._get_keyboard(default="", heading="Artist")
+        query = self._get_keyboard(default="", heading=__language__(30041))
         if (query != ''): 
             artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
             if (len(artists) > 0):
                 artist = artists[0]
                 artistID = artist[1]
-                xbmc.log("Found " + artist[0] + "...")
+                if __debugging__ :
+                    xbmc.log("Found " + artist[0] + "...")
                 songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit)
                 if (len(songs) > 0):
                     self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL)
                 else:
                     dialog = xbmcgui.Dialog()
-                    dialog.ok('Grooveshark XBMC', 'No popular songs.')
+                    dialog.ok(__language__(30008), __language__(30042))
                     self.categories()
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'No matching artists.')
+                dialog.ok(__language__(30008), __language__(30043))
                 self.categories()
         else:
             self.categories()
             
     # Play a song
     def playSong(self, item):
-        songid = item.getProperty('songid')
-        song = groovesharkApi.getSongURLFromSongID(songid)
-        if song != '':
-            item.setPath(song)
-            xbmc.log("Playing: " + song)
-            xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
+        global playTimer
+        global player
+        player.stop()
+        if item != None:
+            url = ''
+            songid = item.getProperty('songid')
+            duration = int(self._getSongDuration(songid))
+            stream = groovesharkApi.getSubscriberStreamKey(songid)
+            if stream != False:
+                url = stream['url']
+                key = stream['StreamKey']
+                server = stream['StreamServerID']
+                duration = self._setDuration(stream['uSecs'])
+            if url != '':
+                item.setPath(url)
+                xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
+                if __debugging__ :
+                    xbmc.log("Grooveshark playing: " + url)
+                # Wait for play then start timer
+                seconds = 0
+                while seconds < STREAM_TIMEOUT:
+                    try:
+                        if player.isPlayingAudio() == True:
+                            if playTimer != None:
+                                playTimer.cancel()
+                                songMarkTime = 0
+                            playTimer = PlayTimer(1, markSong, duration, [songid, duration, key, server])
+                            playTimer.start()
+                            break
+                    except: pass
+                    time.sleep(1)
+                    seconds = seconds + 1
+            else:
+                xbmc.log("No song URL")
         else:
-            xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef + ')')
-    
+            xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef + ')')
+        
     # Make a song directory item
     def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL):
         songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image")
@@ -393,8 +522,9 @@ class Groveshark:
             trackLabel = name + " - " + album + " - " + artist
         else:
             trackLabel = artist + " - " + album + " - " + name
+        duration = self._getSongDuration(songid)
         item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg)
-        item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist} )
+        item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist, "duration": duration} )
         item.setProperty('mimetype', 'audio/mpeg')
         item.setProperty("IsPlayable", "true")
         item.setProperty('songid', str(songid))
@@ -402,70 +532,71 @@ class Groveshark:
         item.setProperty('title', name)
         item.setProperty('album', album)
         item.setProperty('artist', artist)
+        item.setProperty('duration', str(duration))
         
         return item
     
     # Next page of songs
-    def songPage(self, page, trackLabelFormat, playlistid = 0, playlistname = ''):
-        self._add_songs_directory([], trackLabelFormat, page, playlistid = playlistid, playlistname = playlistname)
+    def songPage(self, offset, trackLabelFormat, playlistid = 0, playlistname = ''):
+        self._add_songs_directory([], trackLabelFormat, offset, playlistid = playlistid, playlistname = playlistname)
         
     # Make a playlist from an album      
     def makePlaylist(self, albumid, name):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
             re.split(' - ',name,1)
             nameTokens = re.split(' - ',name,1) # suggested name
-            name = self._get_keyboard(default=nameTokens[0], heading="Grooveshark playlist name")
+            name = self._get_keyboard(default=nameTokens[0], heading=__language__(30045))
             if name != '':
                 album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit)
                 songids = []
                 for song in album:
                     songids.append(song[1])
-                if self.groovesharkApiv1.playlistCreateUnique(name, songids) == 0:
+                if groovesharkApi.createPlaylist(name, songids) == 0:
                     dialog = xbmcgui.Dialog()
-                    dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name)
+                    dialog.ok(__language__(30008), __language__(30046), name)
                 else:
-                    xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')')
+                    xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30047)+ ', 1000, ' + thumbDef + ')')
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to create a Grooveshark playlist.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30048))
     
     # Rename a playlist
     def renamePlaylist(self, playlistid, name):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
-            newname = self._get_keyboard(default=name, heading="Grooveshark playlist name")
+            newname = self._get_keyboard(default=name, heading=__language__(30049))
             if newname == '':
                 return
-            elif self.groovesharkApiv1.playlistRename(playlistid, newname) == 0:
+            elif groovesharkApi.playlistRename(playlistid, newname) == 0:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'Cannot rename Grooveshark playlist ', name)
+                dialog.ok(__language__(30008), __language__(30050), name)
             else:
                 # Refresh to show new item name
                 xbmc.executebuiltin("Container.Refresh")
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to rename a Grooveshark playlist.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30051))
         
     # Remove a playlist
     def removePlaylist(self, playlistid, name):
         dialog = xbmcgui.Dialog()
-        if dialog.yesno('Grooveshark XBMC', name, 'Delete this Grooveshark playlist?') == True:
-            userid = self._get_login(version = 1)
+        if dialog.yesno(__language__(30008), name, __language__(30052)) == True:
+            userid = self._get_login()
             if (userid != 0):
-                if self.groovesharkApiv1.playlistDelete(playlistid) == 0:
+                if groovesharkApi.playlistDelete(playlistid) == 0:
                     dialog = xbmcgui.Dialog()
-                    dialog.ok('Grooveshark XBMC', 'Cannot remove Grooveshark playlist ', name)
+                    dialog.ok(__language__(30008), __language__(30053), name)
                 else:
                     # Refresh to remove item from directory
                     xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")")
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a Grooveshark playlist.')
+                dialog.ok(__language__(30008), __language__(30034), __language__(30054))
 
     # Add song to playlist
     def addPlaylistSong(self, songid):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
             playlists = groovesharkApi.getUserPlaylists()
             if (len(playlists) > 0):
@@ -479,61 +610,73 @@ class Groveshark:
                 if i > -1:
                     # Add a new playlist
                     if i >= len(playlists):
-                        name = self._get_keyboard(default='', heading="Grooveshark playlist name")
+                        name = self._get_keyboard(default='', heading=__language__(30055))
                         if name != '':
                             songIds = []
                             songIds.append(songid)
-                            if self.groovesharkApiv1.playlistCreateUnique(name, songIds) == 0:
+                            if groovesharkApi.createPlaylist(name, songIds) == 0:
                                 dialog = xbmcgui.Dialog()
-                                dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name)
+                                dialog.ok(__language__(30008), __language__(30056), name)
                             else:
-                                xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')')
+                                xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30057) + ', 1000, ' + thumbDef + ')')
                     # Existing playlist
                     else:
                         playlist = playlists[i]
                         playlistid = playlist[1]
-                        xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid))
-                        ret = self.groovesharkApiv1.playlistAddSong(playlistid, songid, 0)
-                        if ret == 0:
+                        if __debugging__ :
+                            xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid))
+                        songIDs=[]
+                        songs = groovesharkApi.getPlaylistSongs(playlistid)
+                        for song in songs:
+                            songIDs.append(song[1])
+                        songIDs.append(songid)
+                        ret = groovesharkApi.setPlaylistSongs(playlistid, songIDs)
+                        if ret == False:
                             dialog = xbmcgui.Dialog()
-                            dialog.ok('Grooveshark XBMC', 'Cannot add to playlist ')
+                            dialog.ok(__language__(30008), __language__(30058))
                         else:    
-                            xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added song to Grooveshark playlist, 1000, ' + thumbDef + ')')
+                            xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30059) + ', 1000, ' + thumbDef + ')')
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
+                dialog.ok(__language__(30008), __language__(30060))
                 self.categories()
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add a song to a Grooveshark playlist.')
+            dialog.ok(__language__(30008), __language__(30034), __language__(30061))
 
     # Remove song from playlist
-    def removePlaylistSong(self, playlistid, playlistname, songpos):
+    def removePlaylistSong(self, playlistid, playlistname, songid):
         dialog = xbmcgui.Dialog()
-        if dialog.yesno('Grooveshark XBMC', 'Delete this song from', 'the Grooveshark playlist?') == True:
-            userid = self._get_login(version = 1)
+        if dialog.yesno(__language__(30008), __language__(30062), __language__(30063)) == True:
+            userid = self._get_login()
             if (userid != 0):
-                if self.groovesharkApiv1.playlistDeleteSong(playlistid, songpos) == 0:
+                songs = groovesharkApi.getPlaylistSongs(playlistID)
+                songIDs=[]
+                for song in songs:
+                    if (song[1] != songid):
+                        songIDs.append(song[1])
+                ret = groovesharkApi.setPlaylistSongs(playlistID, songIDs)
+                if ret == False:
                     dialog = xbmcgui.Dialog()
-                    dialog.ok('Grooveshark XBMC', 'Failed to remove', 'song from Grooveshark playlist.')
+                    dialog.ok(__language__(30008), __language__(30064), __language__(30065))
                 else:
                     # Refresh to remove item from directory
-                    xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed song from Grooveshark playlist, 1000, ' + thumbDef + ')')
+                    xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30066)+ ', 1000, ' + thumbDef + ')')
                     xbmc.executebuiltin("Container.Update(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ")")
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to delete a song from a Grooveshark playlist.')
+                dialog.ok(__language__(30008), __language__(30034), __language__(30067))
       
     # Find similar artists to searched artist
     def similarArtists(self, artistId):
-        similar = self.groovesharkApiv1.artistGetSimilar(artistId, limit = self.artistsearchlimit)
+        similar = groovesharkApi.getSimilarArtists(artistId, limit = self.artistsearchlimit)
         if (len(similar) > 0):
             self._add_artists_directory(similar)
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'No similar artists.')
+            dialog.ok(__language__(30008), __language__(30068))
             self.categories()
-        
+    
     # Get keyboard input
     def _get_keyboard(self, default="", heading="", hidden=False):
         kb = xbmc.Keyboard(default, heading, hidden)
@@ -543,22 +686,19 @@ class Groveshark:
         return ''
     
     # Login to grooveshark
-    def _get_login(self, version = 2):
+    def _get_login(self):
         if (self.username == "" or self.password == ""):
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
+            dialog.ok(__language__(30008), __language__(30069), __language__(30070))
             return 0
         else:
             if self.userid == 0:
-                if version == 1:
-                    uid = self.groovesharkApiv1.login(self.username, self.password)
-                else:
-                    uid = groovesharkApi.login(self.username, self.password)
+                uid = groovesharkApi.login(self.username, self.password)
             if (uid != 0):
                 return uid
             else:
                 dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
+                dialog.ok(__language__(30008), __language__(30069), __language__(30070))
                 return 0
     
     # Get a song directory item
@@ -585,82 +725,98 @@ class Groveshark:
             return thumbDef
     
     # Add songs to directory
-    def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0, playlistid=0, playlistname='', isFavorites=False):
+    def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, offset=0, playlistid=0, playlistname='', isFavorites=False):
 
         totalSongs = len(songs)
-        page = int(page)
+        offset = int(offset)
+        start = 0
+        end = totalSongs
 
         # No pages needed
-        if page == 0 and totalSongs <= self.songspagelimit:
-            xbmc.log("Found " + str(totalSongs) + " songs...")
+        if offset == 0 and totalSongs <= self.songspagelimit:
+            if __debugging__ :
+                xbmc.log("Found " + str(totalSongs) + " songs...")
         # Pages
         else:
-            # Cache all songs
-            if page == 0:
+            # Cache all next pages songs
+            if offset == 0:
                 self._setSavedSongs(songs)
             else:
                 songs = self._getSavedSongs()
                 totalSongs = len(songs)
                 
             if totalSongs > 0:
-                start = page * self.songspagelimit
-                end = start + self.songspagelimit
-                songs = songs[start:end]
+                start = offset
+                end = min(start + self.songspagelimit,totalSongs)
         
         id = 0
-        for song in songs:
-            item = self._get_song_item(song, trackLabelFormat)
-            coverart = item.getProperty('coverart')
-            songname = song[0]
+        n = start
+        items = end - start
+        while n < end:
+            song = songs[n]
             songid = song[1]
-            songalbum = song[2]
-            songartist = song[4]
-            u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \
-            +"&album="+urllib.quote_plus(songalbum) \
-            +"&artist="+urllib.quote_plus(songartist) \
-            +"&coverart="+urllib.quote_plus(coverart)
-            fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)
-            unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode="
-            menuItems = []
-            if isFavorites == True:
-                unfav = unfav +str(MODE_FAVORITES)
-            else:
-                menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")"))
-            if self.sessionidv1 != '':
-                menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")"))
+            duration = self._getSongDuration(songid)
+            if duration != -1:   
+                item = self._get_song_item(song, trackLabelFormat)
+                coverart = item.getProperty('coverart')
+                songname = song[0]
+                songalbum = song[2]
+                songartist = song[4]
+                u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \
+                +"&album="+urllib.quote_plus(songalbum) \
+                +"&artist="+urllib.quote_plus(songartist) \
+                +"&coverart="+urllib.quote_plus(coverart)
+                fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)
+                unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode="
+                menuItems = []
+                if isFavorites == True:
+                    unfav = unfav +str(MODE_FAVORITES)
+                else:
+                    menuItems.append((__language__(30071), "XBMC.RunPlugin("+fav+")"))
+                menuItems.append((__language__(30072), "XBMC.RunPlugin("+unfav+")"))
                 if playlistid > 0:
-                    rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(id+1)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname
-                    menuItems.append(("Remove from Grooveshark playlist", "XBMC.RunPlugin("+rmplaylstsong+")"))
+                    rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(songid)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname
+                    menuItems.append((__language__(30073), "XBMC.RunPlugin("+rmplaylstsong+")"))
                 else:
                     addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG)
-                    menuItems.append(("Add to Grooveshark playlist", "XBMC.RunPlugin("+addplaylstsong+")"))
-            item.addContextMenuItems(menuItems, replaceItems=False)
-            xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs))
-            id = id + 1
+                    menuItems.append((__language__(30074), "XBMC.RunPlugin("+addplaylstsong+")"))
+                item.addContextMenuItems(menuItems, replaceItems=False)
+                xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=items)
+                id = id + 1
+            else:
+                end = min(end + 1,totalSongs)
+                if __debugging__ :
+                    xbmc.log(song[0] + " does not exist.")
+            n = n + 1
 
-        page = page + 1
-        if totalSongs > page * self.songspagelimit:
-            u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=playlistid"+"&page="+str(page)+"&label="+str(trackLabelFormat)+"&name="+playlistname
-            self._add_dir('More songs...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - (page * self.songspagelimit))
+        if totalSongs > end:
+            u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=playlistid"+"&offset="+str(end)+"&label="+str(trackLabelFormat)+"&name="+playlistname
+            self._add_dir(__language__(30075) + '...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - end)
 
         xbmcplugin.setContent(self._handle, 'songs')
         xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
     
     # Add albums to directory
-    def _add_albums_directory(self, albums, artistid=0):
+    def _add_albums_directory(self, albums, artistid=0, isverified=False):
         n = len(albums)
-        xbmc.log("Found " + str(n) + " albums...")
+        itemsExisting = n
+        if __debugging__ :
+            xbmc.log("Found " + str(n) + " albums...")
         i = 0
         while i < n:
             album = albums[i]
-            albumArtistName = album[0]
-            albumName = album[2]
             albumID = album[3]
-            albumImage = self._get_icon(album[4], 'album-' + str(albumID))
-            self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
+            if isverified or groovesharkApi.getDoesAlbumExist(albumID):                    
+                albumArtistName = album[0]
+                albumName = album[2]
+                albumImage = self._get_icon(album[4], 'album-' + str(albumID))
+                self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, itemsExisting)
+            else:
+                itemsExisting = itemsExisting - 1
             i = i + 1
-        if artistid > 0 and self.sessionidv1 != '':
-            self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
+        # Not supported by key
+        #if artistid > 0:
+        #    self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
         xbmcplugin.setContent(self._handle, 'albums')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
         xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
@@ -668,13 +824,18 @@ class Groveshark:
     # Add artists to directory
     def _add_artists_directory(self, artists):
         n = len(artists)
-        xbmc.log("Found " + str(n) + " artists...")
+        itemsExisting = n
+        if __debugging__ :
+            xbmc.log("Found " + str(n) + " artists...")
         i = 0
         while i < n:
             artist = artists[i]
-            artistName = artist[0]
             artistID = artist[1]
-            self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n)
+            if groovesharkApi.getDoesArtistExist(artistID):                    
+                artistName = artist[0]
+                self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, itemsExisting)
+            else:
+                itemsExisting = itemsExisting - 1
             i = i + 1
         xbmcplugin.setContent(self._handle, 'artists')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE)
@@ -683,7 +844,8 @@ class Groveshark:
     # Add playlists to directory          
     def _add_playlists_directory(self, playlists):
         n = len(playlists)
-        xbmc.log("Found " + str(n) + " playlists...")
+        if __debugging__ :
+            xbmc.log("Found " + str(n) + " playlists...")
         i = 0
         while i < n:
             playlist = playlists[i]
@@ -706,17 +868,17 @@ class Groveshark:
         dir.setInfo( type="Music", infoLabels={ "title": name } )
         
         # Custom menu items
-        if self.sessionidv1 != '':
-            menuItems = []
-            if mode == MODE_ALBUM:
-                mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id)
-                menuItems.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst+")"))
-            if mode == MODE_PLAYLIST:
-                rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
-                menuItems.append(("Delete Grooveshark playlist", "XBMC.RunPlugin("+rmplaylst+")"))
-                mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
-                menuItems.append(("Rename Grooveshark playlist", "XBMC.RunPlugin("+mvplaylst+")"))
-            dir.addContextMenuItems(menuItems, replaceItems=False)
+        menuItems = []
+        if mode == MODE_ALBUM:
+            mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id)
+            menuItems.append((__language__(30076), "XBMC.RunPlugin("+mkplaylst+")"))
+        if mode == MODE_PLAYLIST:
+            rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
+            menuItems.append((__language__(30077), "XBMC.RunPlugin("+rmplaylst+")"))
+            mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
+            menuItems.append((__language__(30078), "XBMC.RunPlugin("+mvplaylst+")"))
+
+        dir.addContextMenuItems(menuItems, replaceItems=False)
         
         return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items)
     
@@ -743,12 +905,59 @@ class Groveshark:
         except:
             xbmc.log("An error occurred saving songs")
             pass
+
+    def _getSongDuration(self, songid):
+        id = int(songid)
+        duration = -1
+        durations = []
+        path = os.path.join(cacheDir, 'duration.dmp')
+        try:
+            f = open(path, 'rb')
+            durations = pickle.load(f)
+            for song in durations:
+                if song[0] == id:
+                    duration = song[1]
+                    break;
+            f.close()
+        except:
+            pass
+
+        # Not in cache
+        if duration < 0:
+            stream = groovesharkApi.getSubscriberStreamKey(songid)
+            if stream != False and stream['url'] != '':
+                duration = self._setDuration(stream['uSecs'])
+                song = [id, duration]
+                self._setSongDuration(song, durations)
+
+        return duration
+        
+    def _setSongDuration(self, song, durations):            
+        try:
+            durations.append(song)                
+            # Create the cache directory if it doesn't exist.
+            if not os.path.exists(cacheDir):
+                os.makedirs(cacheDir)
+            path = os.path.join(cacheDir, 'duration.dmp')
+            f = open(path, 'wb')
+            pickle.dump(durations, f, protocol=pickle.HIGHEST_PROTOCOL)
+            f.close()
+        except:
+            xbmc.log("An error occurred saving duration")
+            pass
+
+    # Duration to seconds
+    def _setDuration(self, usecs):
+        if usecs < 60000000:
+            usecs = usecs * 10 # Some durations are 10x to small
+        return usecs / 1000000
     
 # Parse URL parameters
 def get_params():
     param=[]
     paramstring=sys.argv[2]
-    xbmc.log(paramstring)
+    if __debugging__ :
+        xbmc.log(paramstring)
     if len(paramstring)>=2:
         params=sys.argv[2]
         cleanedparams=params.replace('?','')
@@ -764,7 +973,7 @@ def get_params():
     return param
         
 # Main
-grooveshark = Groveshark();
+grooveshark = Grooveshark();
 
 params=get_params()
 mode=None
@@ -809,11 +1018,11 @@ elif mode==MODE_PLAYLISTS:
     grooveshark.playlists()
     
 elif mode==MODE_SONG_PAGE:
-    try: page=urllib.unquote_plus(params["page"])
+    try: offset=urllib.unquote_plus(params["offset"])
     except: pass
     try: label=urllib.unquote_plus(params["label"])
     except: pass
-    grooveshark.songPage(page, label, id, name)
+    grooveshark.songPage(offset, label, id, name)
 
 elif mode==MODE_SONG:
     try: album=urllib.unquote_plus(params["album"])