Skip to content

Instantly share code, notes, and snippets.

@diox
Created March 31, 2011 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diox/896618 to your computer and use it in GitHub Desktop.
Save diox/896618 to your computer and use it in GitHub Desktop.
Patch against rhythmbox-random-album-player to make it use library filters instead of play queue
Index: __init__.py
===================================================================
--- __init__.py (revision 4)
+++ __init__.py (working copy)
@@ -39,17 +39,24 @@
def __init__(self):
rb.Plugin.__init__(self)
- def queue_random_album(self):
+ def get_random_album_first_song(self):
#find all of the albums in the user's library
albums = []
library = self.shell.props.library_source
for row in library.props.query_model:
entry = row[0]
+ track_num = self.shell.props.db.entry_get(entry, rhythmdb.PROP_TRACK_NUMBER)
+ if not track_num or track_num < 5:
+ #ignore entries without track number or track_number too low to represent
+ #a "real" album (arbitrary decision: "real" albums need to have at least
+ #5 tracks - this is pretty awful but good enough for my 99% of my library)
+ continue
album_name = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ALBUM)
if (album_name not in albums):
albums.append(album_name)
#choose a random album
+ print '%d albums' % (len(albums), )
selected_album = albums[random.randint(0, len(albums) - 1)]
print 'queuing ' + selected_album
@@ -59,17 +66,14 @@
entry = row[0]
album = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ALBUM)
if (album == selected_album):
- song_uri = entry.get_playback_uri()
track_num = self.shell.props.db.entry_get(entry, rhythmdb.PROP_TRACK_NUMBER)
- song_info.append((song_uri, track_num))
+ song_info.append((entry, track_num))
#sort the songs
song_info = sorted(song_info, key=lambda song_info: song_info[1])
+
+ return song_info[0][0]
- #add the songs to the play queue
- for info in song_info:
- self.shell.add_to_queue(info[0])
-
#loads the plugin
def activate(self, shell):
self.shell = shell
@@ -112,21 +116,29 @@
entry = row[0]
song_uri = entry.get_playback_uri()
shell.remove_from_queue(song_uri)
+
+ #make sure to reset browse filters
+ library = shell.props.library_source
+ library.reset_filters()
+
+ #get the first song of a random album
+ entry = self.get_random_album_first_song()
+ #stop music, change source
+ player = shell.props.shell_player
+ player.stop()
+ player.set_playing_source(library)
- #queue a random album
- self.queue_random_album()
-
+ #jump to selected entry
+ view = library.get_entry_view()
+ view.scroll_to_entry(entry)
- #start the music!(well, first stop it, but it'll start up agian.)
- print 'Playing Album'
- player = shell.props.shell_player
- player.stop()
- player.set_playing_source(shell.props.queue_source)
+ #find and activate browse artist & album
+ mainactions = [x for x in shell.get_ui_manager().get_action_groups() if x.get_name() == "BrowserSourceActions"][0]
+ action = mainactions.get_action("BrowserSrcChooseArtist")
+ action.activate()
+ action = mainactions.get_action("BrowserSrcChooseAlbum")
+ action.activate()
+
+ #play :)
player.playpause()
-
- #scroll to song playing
- shell.props.shell_player.play()
- library = shell.props.library_source
- content_viewer = library.get_entry_view()
- #content_viewer.scroll_to_entry(player.get_playing_entry())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment