class DBUSRemoteSession(dbus.service.Object): def __init__(self): # Application data: self._session = EasySession() # DBUS data: bus = dbus.SessionBus() bus_name = dbus.service.BusName(MUNIN_BUS_NAME, bus=bus) dbus.service.Object.__init__(self, bus_name, '/org/libmunin') ##################### # Rebuild methods # ##################### @dbus.service.signal(dbus_interface=MUNIN_INTERFACE) def rebuild_finished(self): LOGGER.warning('Server: triggerred rebuild finished') @dbus.service.signal(dbus_interface=MUNIN_INTERFACE) def rebuild_started(self): LOGGER.warning('Server: rebuild was started') @dbus.service.method(MUNIN_BUS_NAME) def rebuild(self, strategy='full'): self.rebuild_started() time.sleep(2) self.rebuild_finished() @dbus.service.method(MUNIN_BUS_NAME) def fix_graph(self): self._session.fix_graph() ############# # Mapping # ############# @dbus.service.method(MUNIN_BUS_NAME, out_signature='a{uu}') def mapping(self): return dict(self._session.mapping) @dbus.service.method(MUNIN_BUS_NAME, out_signature='a{uu}') def inverse_mapping(self): return dict(~self._session.mapping) @dbus.service.method(MUNIN_BUS_NAME, out_signature='u') def forward_lookup(self, munin_id): return self._session.mapping[munin_id:] @dbus.service.method(MUNIN_BUS_NAME, out_signature='u') def inverse_lookup(self, user_id): return self._session.mapping[:user_id] ###################### # Song modifcation # ###################### @dbus.service.method(MUNIN_BUS_NAME, in_signature='su') def add(self, json_mapping, user_id): uid = self._session.add(json.loads(json_mapping)) self._session.mapping[uid] = user_id @dbus.service.method(MUNIN_BUS_NAME, in_signature='su') def insert(self, json_mapping, user_id): uid = self._session.insert(json.loads(json_mapping)) self._session.mapping[uid] = user_id @dbus.service.method(MUNIN_BUS_NAME, in_signature='su') def remove(self, json_mapping, user_id): uid = self._session.remove(json.loads(json_mapping)) del self._session.mapping[uid] ##################### # Recommendations # ##################### @dbus.service.method(MUNIN_BUS_NAME, in_signature='uu') def recommendations_from_seed(self, song_id, number): uid = self._session.mapping[:song_id] self._session.recommendations_from_seed(uid, number) # TODO #################### # Misc Functions # #################### @dbus.service.method(MUNIN_BUS_NAME) def name(self): return self._session.name
MY_DATABASE = [ # Artist: Album: Title: Genre: ('Akrea' , 'Lebenslinie' , 'Trugbild' , 'death metal'), ('Vogelfrey' , 'Wiegenfest' , 'Heldentod' , 'folk metal'), ('Letzte Instanz' , 'Götter auf Abruf' , 'Salve te' , 'folk rock'), ('Debauchery' , 'Continue to Kill' , 'Apostle of War' , 'brutal death') ] session = EasySession() with session.transaction(): for idx, (artist, album, title, genre) in enumerate(MY_DATABASE): session.mapping[session.add({ 'artist': artist, 'album': album, 'title': title, 'genre': genre })] = idx print('2 Recommendations to: {}'.format(MY_DATABASE[0])) for munin_song in session.recommend_from_seed(session[0], 2): print(' ', MY_DATABASE[munin_song.uid]) print('3 Recommendations to: {}'.format(MY_DATABASE[1])) for munin_song in session.recommend_from_seed(session[1], 3): print(' ', MY_DATABASE[munin_song.uid]) if '--plot' in sys.argv:
from munin.easy import EasySession MY_DATABASE = [ # Artist: Album: Title: Genre: ('Akrea', 'Lebenslinie', 'Trugbild', 'death metal'), ('Vogelfrey', 'Wiegenfest', 'Heldentod', 'folk metal'), ('Letzte Instanz', 'Götter auf Abruf', 'Salve te', 'folk rock'), ('Debauchery', 'Continue to Kill', 'Apostle of War', 'brutal death') ] session = EasySession() with session.transaction(): for idx, (artist, album, title, genre) in enumerate(MY_DATABASE): session.mapping[session.add({ 'artist': artist, 'album': album, 'title': title, 'genre': genre })] = idx print('2 Recommendations to: {}'.format(MY_DATABASE[0])) for munin_song in session.recommend_from_seed(session[0], 2): print(' ', MY_DATABASE[munin_song.uid]) print('3 Recommendations to: {}'.format(MY_DATABASE[1])) for munin_song in session.recommend_from_seed(session[1], 3): print(' ', MY_DATABASE[munin_song.uid]) if '--plot' in sys.argv: print('Now rendering a plot of the relation graph...') vx = {idx: row[0] for idx, row in enumerate(MY_DATABASE)} session.database.plot(width=300, height=300, vx_mapping=vx)
g.client.connect(port=6601) moosecat.boot.boot_metadata() moosecat.boot.boot_store() # Fetch the whole database into entries: entries = [] with g.client.store.query('*', queue_only=False) as playlist: for song in playlist: entries.append(make_entry(song)) # Instance a new EasySession and fill in the values. session = EasySession() with session.transaction(): for uri, entry in entries: try: print('Processing:', entry['bpm']) session.mapping[session.add(entry)] = uri except: import traceback traceback.print_exc() # Save the Session to disk (~/.cache/libmunin/EasySession.gz) session.save() # Plot if desired. if '--plot' in sys.argv: session.database.plot() # Close the connection to MPD, save cached database moosecat.boot.shutdown_application()