Skip to content

Commit

Permalink
Random fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverguest committed Mar 24, 2015
1 parent 1309b5e commit 8804754
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 53 deletions.
3 changes: 1 addition & 2 deletions GoogleMusicApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def getPlaylistSongs(self, playlist_id, forceRenew=False):
songs = storage.getAutoPlaylistSongs(playlist_id)
if playlist_id == 'thumbsup':
""" Try to fetch all access thumbs up songs """
for track in self.getApi().get_thumbs_up_songs():
for track in self.getApi().get_promoted_songs():
songs.append(self._convertAATrack(track))
else:
if forceRenew:
Expand Down Expand Up @@ -73,7 +73,6 @@ def updatePlaylistSongs(self):

def getSongStreamUrl(self, song_id):
stream_url = self.getLogin().getStreamUrl(song_id)
#storage.updateSongStreamUrl(song_id, stream_url)
return stream_url

def incrementSongPlayCount(self, song_id):
Expand Down
57 changes: 8 additions & 49 deletions GoogleMusicStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ def clearCache(self):
utils.addon.setSetting("fetched_all_songs", "0")

def getPlaylistSongs(self, playlist_id):
#self._connect()
if playlist_id == 'all_songs':
result = self.curs.execute("SELECT * FROM songs ORDER BY display_name")
else:
result = self.curs.execute("SELECT * FROM songs "+
"INNER JOIN playlists_songs ON songs.song_id = playlists_songs.song_id "+
"WHERE playlists_songs.playlist_id = ?", (playlist_id,))
songs = result.fetchall()
#self._connect()
return songs

def getFilterSongs(self, filter_type, filter_criteria, albumArtist):
Expand All @@ -57,39 +55,30 @@ def getFilterSongs(self, filter_type, filter_criteria, albumArtist):
elif filter_type == 'composer':
query = "select * from songs where composer = :filter order by album asc, disc asc, track asc, title asc"

#self._connect()
songs = self.curs.execute(query,{'filter':filter_criteria if filter_criteria else '','albumArtist':albumArtist}).fetchall()
#self.conn.close()

return songs

def getCriteria(self, criteria, name):
#print "### storage getcriteria: "+repr(criteria)+" "+repr(name)

if criteria == 'album':
query = "select album_artist, album, year, max(album_art_url) from songs where album <> '-Unknown-' group by album_artist, album"
query = "select album_artist, album, year, max(album_art_url) from songs where album <> '-Unknown-' group by lower(album_artist), lower(album)"
else:
#if criteria == 'artist': criteria = 'album_artist'
if criteria == 'artist' and not name:
query = "select artist, max(artist_art_url) from songs group by artist"
query = "select album_artist, max(artist_art_url) from songs group by lower(album_artist)"
elif criteria == 'artist' and name:
query = "select album_artist, album, year, max(album_art_url) from songs where (artist=:name or album_artist=:name) group by lower(album_artist), lower(album)"
elif name:
query = "select album_artist, album, year, max(album_art_url) from songs where %s=:name group by album_artist, album" % criteria
query = "select album_artist, album, year, max(album_art_url) from songs where %s=:name group by lower(album_artist), lower(album)" % criteria
else:
query = "select %s from songs group by lower(%s)" % (criteria, criteria)

#self._connect()
criterias = self.curs.execute(query,{'name':name.decode('utf8')}).fetchall()
#self.conn.close()

return criterias
return self.curs.execute(query,{'name':name.decode('utf8')}).fetchall()

def getPlaylists(self):
#self._connect()
playlists = self.curs.execute("SELECT playlist_id, name FROM playlists ORDER BY name").fetchall()
#self.conn.close()
return playlists
return self.curs.execute("SELECT playlist_id, name FROM playlists ORDER BY name").fetchall()

def getAutoPlaylistSongs(self,playlist):
querys = {'thumbsup':'SELECT * FROM songs WHERE rating > 3 ORDER BY display_name',
Expand All @@ -98,30 +87,20 @@ def getAutoPlaylistSongs(self,playlist):
'freepurchased':'SELECT * FROM songs WHERE type = 0 OR type = 1',
'feellucky':'SELECT * FROM songs ORDER BY random() LIMIT 500',
}
#self._connect()
result = self.curs.execute(querys[playlist]).fetchall()
#self.conn.close()
return result
return self.curs.execute(querys[playlist]).fetchall()

def getSong(self, song_id):
#self._connect()
result = self.curs.execute("SELECT * FROM songs WHERE song_id = ? ", (song_id,)).fetchone()
#result = self.curs.execute("SELECT stream_url FROM songs WHERE song_id = ? ", (song_id,)).fetchone()
#self.conn.close()
return result
return self.curs.execute("SELECT * FROM songs WHERE song_id = ? ", (song_id,)).fetchone()

def getSearch(self, query):
query = '%'+ query.replace('%','') + '%'
#self._connect()
result = {}
result['artists'] = self.curs.execute("SELECT artist, max(artist_art_url) FROM songs WHERE artist like ? GROUP BY artist", (query,)).fetchall()
result['tracks'] = self.curs.execute("SELECT * FROM songs WHERE display_name like ? ORDER BY display_name", (query,)).fetchall()
result['albums'] = self.curs.execute("SELECT album, artist, max(album_art_url) FROM songs WHERE album like ? GROUP BY album, artist", (query,)).fetchall()
#self.conn.close()
return result

def storePlaylistSongs(self, playlists_songs):
#self._connect()
self.curs.execute("PRAGMA foreign_keys = OFF")

self.curs.execute("DELETE FROM playlists_songs")
Expand All @@ -140,12 +119,9 @@ def storePlaylistSongs(self, playlists_songs):
api_songs.append(entry['track'])

self.conn.commit()
#self.conn.close()

self.storeInAllSongs(api_songs)

def storeApiSongs(self, api_songs, playlist_id = 'all_songs'):
#self._connect()
self.curs.execute("PRAGMA foreign_keys = OFF")

if playlist_id == 'all_songs':
Expand All @@ -162,12 +138,9 @@ def storeApiSongs(self, api_songs, playlist_id = 'all_songs'):
self.curs.execute("UPDATE playlists SET fetched = 1 WHERE playlist_id = ?", (playlist_id,))

self.conn.commit()
#self.conn.close()

self.storeInAllSongs(api_songs)

def storeInAllSongs(self, api_songs):
#self._connect()
self.curs.execute("PRAGMA foreign_keys = OFF")

#for i in range(5):
Expand All @@ -193,7 +166,7 @@ def songs():
'play_count': get("playCount", 0),
'creation_date': get("creationDate", get("creationTimestamp", 0)),
'name': get("name", api_song["title"]),
'artist': get("artist") if get("artist") else '-Unknown-',
'artist': get("artist") if get("artist") else get("albumArtist") if get("albumArtist") else '-Unknown-',
'url': get("url", None),
'total_discs': get("total_discs", get("totalDiscCount", 0)),
'duration': int(get("durationMillis",0))/1000,
Expand All @@ -208,10 +181,8 @@ def songs():
":url, :total_discs, :duration, :album_art_url, :display_name, NULL, :artist_art_url)", songs())

self.conn.commit()
#self.conn.close()

def storePlaylists(self, playlists, playlist_type):
#self._connect()
self.curs.execute("PRAGMA foreign_keys = OFF")

# (deletes will not cascade due to pragma)
Expand All @@ -228,40 +199,29 @@ def playlist_rows():
# clean up dangling songs
self.curs.execute("DELETE FROM playlists_songs WHERE playlist_id NOT IN (SELECT playlist_id FROM playlists)")
self.conn.commit()
#self.conn.close()

def getSongStreamUrl(self, song_id):
#self._connect()
song = self.curs.execute("SELECT stream_url FROM songs WHERE song_id = ?", (song_id,)).fetchone()
#self.conn.close()
return song[0]

def incrementSongPlayCount(self, song_id):
import time
#self._connect()
self.curs.execute("UPDATE songs SET play_count = play_count+1, last_played = ? WHERE song_id = ?", (int(time.time()*1000000), song_id))
self.conn.commit()
#self.conn.close()

def addToPlaylist(self, playlist_id, song_id, entry_id):
#self._connect()
self.curs.execute("INSERT OR REPLACE INTO playlists_songs(playlist_id, song_id, entry_id) VALUES (?,?,?)", (playlist_id, song_id, entry_id))
self.conn.commit()
#self.conn.close()

def delFromPlaylist(self, playlist_id, song_id):
#self._connect()
entry_id = self.curs.execute("SELECT entry_id FROM playlists_songs WHERE playlist_id=? and song_id=?", (playlist_id, song_id)).fetchone()
self.curs.execute("DELETE from playlists_songs WHERE entry_id=?", (entry_id[0], ))
self.conn.commit()
#self.conn.close()
return entry_id[0]

def updateSongStreamUrl(self, song_id, stream_url):
#self._connect()
self.curs.execute("UPDATE songs SET stream_url = ? WHERE song_id = ?", (stream_url, song_id))
self.conn.commit()
#self.conn.close()

def _connect(self):
self.conn = sqlite3.connect(self.path)
Expand Down Expand Up @@ -319,7 +279,6 @@ def initializeDatabase(self):
self.curs.execute('''CREATE INDEX IF NOT EXISTS songindex ON playlists_songs(song_id)''')

self.conn.commit()
#self.conn.close()

def _getSongDisplayName(self, api_song):
displayName = "-Unknown-"
Expand Down
4 changes: 3 additions & 1 deletion default.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@

# check if library needs to be loaded
if addon.getSetting('fetched_all_songs') == '0':

xbmc.executebuiltin("XBMC.Notification(%s,%s,5000,%s)" % (utils.plugin, addon.getLocalizedString(30105) ,addon.getAddonInfo('icon')))
utils.log('Loading library')
navigation.api.loadLibrary()
if addon.getSetting('auto_export') and addon.getSetting('export_path'):

if addon.getSetting('auto_export')=='true' and addon.getSetting('export_path'):
import GoogleMusicActions
GoogleMusicActions.GoogleMusicActions().exportLibrary(addon.getSetting('export_path'))

Expand Down
2 changes: 1 addition & 1 deletion resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<setting id="clear_cache" type="action" label="30062" option="close" action="RunPlugin(plugin://plugin.audio.googlemusic.exp/?action=clear_cache)" />
<setting type="sep" />
<setting id="sqlite_db" type="text" label="30064" default="googlemusic.db"/>
<setting id="device_id" type="text" label="Device ID" default="" />
<setting id="device_id" type="text" label="Device ID" default="" enable="false"/>
</category>
</settings>

0 comments on commit 8804754

Please sign in to comment.