Browse the code

 

Differences between 215 and 216 on /.
Number of edited files: 3 (0 added, 0 deleted and 3 modified)
Revision 216

About 6 months and 1 day ago.

* Improved similar mode: now search for a similar song, check if it hasn't been played before, and then search for a similar artist if it fails
Added file(s) Deleted file(s) Modified file(s)

 

Old New Code
59
59
        self.api_key = b64decode(self.config.lastfm_key)
60
60
        self.api_url = 'http://ws.audioscrobbler.com/2.0/'
61
61

                                        
 
62
    def get_similar_songs(self, song_artist, song_title):
 
63
        """Get a lit of similar songs."""
 
64
        url = (self.api_url +
 
65
               '?method=track.getsimilar&artist={0}&track={1}&api_key={2}'.format(
 
66
                   quote(str(song_artist)),
 
67
                   quote(str(song_title)),
 
68
                   quote(self.api_key)))
 
69

                                    
 
70
        songs_list = []
 
71

                                    
 
72
        tree = self.get_xml(url)
 
73
        xml = tree.find('similartracks')
 
74
        songs = xml.findall('track')
 
75

                                    
 
76
        for song in songs:
 
77
            name = song.find('name')
 
78
            artist = song.find('artist').find('name')
 
79
            songs_list.append([artist.text, name.text])
 
80

                                    
 
81
        return songs_list
 
82

                                    
62
83
    def get_similar_artists(self, artist_name):
63
84
        """Get a list of similar artists."""
64
85
        url = (self.api_url + '?method=artist.getsimilar&artist=%s&api_key=%s' %
65
86

                                        

 

Old New Code
258
258
        if answer > 0:
259
259
            return True
260
260

                                        
 
261
    def song_exists(self, song_artist, song_title):
 
262
        sqlite = SQLite()
 
263
        cursor = sqlite.execute('select count(*) from songs where ' +
 
264
                                'artist=:artist and title=:title',
 
265
                                {'artist': song_artist,
 
266
                                 'title':  song_title})
 
267
        songs = cursor.fetchall()
 
268
        sqlite.close()
 
269

                                    
 
270
        answer = songs[0][0]
 
271
        if answer > 0:
 
272
            return True
 
273

                                    
 
274
    def load_from_artist_and_title(self, artist_name, song_title):
 
275
        sqlite = SQLite()
 
276
        cursor = sqlite.execute('select filename from songs where ' +
 
277
                                'artist=:val1 and title=:val2 ' +
 
278
                                'order by album, title',
 
279
                               {'val1': artist_name, 'val2': song_title})
 
280
        songs = cursor.fetchall()
 
281
        sqlite.close()
 
282

                                    
 
283
        return songs
 
284

                                    
261
285
    def load_from_artist(self, artist_name):
262
286
        sqlite = SQLite()
263
287
        cursor = sqlite.execute('select * from songs where artist=:val ' +
264
288

                                        

 

Old New Code
56
56
        self.lastfm = LastFm()
57
57
        self.playlists_mgmnt = Playlists()
58
58

                                        
 
59
        self.recent_songs = []
59
60
        self.playlist_content = {}
60
61
        self.playlist_identifier = 0
61
62
        self.playlist_current = None
594
595
                    self.extensions.load_event('AskShuffleSong')
595
596
                elif self.config['drunk_mode'] == 'similar':
596
597
                    # Similar mode: seek for a similar song.
 
598
                    if len(self.similar_songs) > 0:
 
599
                        index = randrange(len(self.similar_songs))
 
600
                        song = self.similar_songs[index]
 
601

                                    
 
602
                        mdb = MusicDatabase(None)
 
603
                        songs_list = mdb.load_from_artist_and_title(song[0],
 
604
                                                                    song[1])
 
605

                                    
 
606
                        if len(songs_list) > 0:
 
607
                            index = randrange(len(songs_list))
 
608
                            song = songs_list[index]
 
609

                                    
 
610
                            sng = Song(filename=song[0])
 
611
                            print ('[SIMILAR] Playing the song ' + sng.title)
 
612
                            self.extensions.load_event('OnPlayNewSong', sng)
 
613
                            return
 
614

                                    
 
615
                    # No similar song, seek for a song from a similar artist
597
616
                    if len(self.similar_artists) == 0:
598
617
                        # No similar song founded: seek for any one.
599
618
                        self.extensions.load_event('AskShuffleSong')
628
647

                                        
629
648

                                        
630
649
    def song_started(self, song):
631
 
        # First, try to download a few similar artists names
 
650
        self.recent_songs.append((song.artist, song.title))
 
651
        if len(self.recent_songs) > 50:
 
652
            self.recent_songs.pop(0)
 
653

                                    
 
654
        # First, try to download a few similar songs or artists
632
655
        if self.config['drunk_mode'] == 'similar':
633
656
            def download_similars():
634
657
                threads_enter()
635
 
                art = self.lastfm.get_similar_artists(song.artist)
636
 

                                    
637
 
                self.similar_artists = []
638
658
                mdb = MusicDatabase(None)
639
659

                                        
640
 
                for artist in art:
641
 
                    if mdb.artist_exists(artist):
642
 
                        self.similar_artists.append(artist)
 
660
                # First, check if there is a similar song
 
661
                sng = self.lastfm.get_similar_songs(song.artist, song.title)
643
662

                                        
 
663
                self.similar_songs = []
 
664
                for track in sng:
 
665
                    if (mdb.song_exists(track[0], track[1]) and
 
666
                        (track[0], track[1]) not in self.recent_songs):
 
667
                        self.similar_songs.append((track[0], track[1]))
 
668

                                    
 
669
                if len(self.similar_songs) == 0:
 
670
                    # If we haven't found anything, we search for an artist
 
671
                    art = self.lastfm.get_similar_artists(song.artist)
 
672

                                    
 
673
                    self.similar_artists = []
 
674

                                    
 
675
                    for artist in art:
 
676
                        if mdb.artist_exists(artist):
 
677
                            self.similar_artists.append(artist)
 
678

                                    
644
679
                self.is_in_similar_thread = False
645
680
                threads_leave()
646
681

                                        
647
682