コード例 #1
0
ファイル: tests.py プロジェクト: bgvladedivac/Projects
class TestPlayList(unittest.TestCase):
    """TestPlayList class"""

    def setUp(self):
        """Setup method that will run before each test"""
        self.first_song = Song(title='Fields of Gold', artist='Sting', album='Greatest Hits', length='3:54')
        self.second_song = Song(title='Desert Rose', artist='Sting', album='Greatest Hits', length='4:12')
        self.third_song = Song(title='Youth of the nation', artist='POD', album='Greatest Hits', length='3:25')
        self.playlist = PlayList(name='Best PlayList Ever', repeat=True)
        self.playlist.add_song(self.first_song)
        self.playlist.add_songs([self.second_song, self.third_song])

    def test_artists(self):
        """Test generating the proper histogram"""
        expected_histogram = { 'Sting':2, 'POD':1 }
        self.assertEqual(expected_histogram, self.playlist.artists())

    def test_next_song_repeat(self):
        """Test whether a playlist in a repeat mode will repeat the song"""
        songs = [self.playlist.next_song() for x in range(4)]
        self.assertEqual(self.first_song, songs[len(songs)-1])

    def test_next_song_shuffle(self):
        """Test whether a playlist in a shuffle mode will shuffle its content"""
        self.playlist.shuffle=True
        self.playlist.repeat=False

        songs = [self.playlist.next_song() for x in range(3)]
        self.assertEqual(3, len(songs))
        self.assertTrue(self.playlist.next_song() in songs)
コード例 #2
0
class MusicCrawler():
    """MusicCrawler class"""

    def __init__(self, directory):
        """Constructor"""
        self.directory = directory
        self.extension = ".mp3"
        self.playlist = PlayList()
   
    def generate_playlist(self):
        """
        Generates the playlist from directory with .mp3 files
        """
        mp3s = self.find_all_extension_files(self.directory, self.extension)
        for mp3 in mp3s:
            dest = self.join_mp3_and_path(self.directory, mp3)
            data = mutagen.File(dest)
            
            tags = self._extract_mp3_targs(data)
            s = Song(tags["title"], tags["artist"], tags["album"], tags["length"], dest)
            self.playlist.add_song(s)

    def join_mp3_and_path(self, dir, mp3):
        """
        Joins a directory and mp3 object into 1 path
        :return: a new path created object
        """
        return os.path.join(dir, mp3)

    def _extract_mp3_targs(self, mp3_object):
        """
        Extract mp3 tags from an mp3 file. 
        The extracted tags are represented as a key-value pairs.
        :param mp3_object: mp3 from which the tags will be extracted
        :return: dictionary
        """
        tags = {}
        try:
            tags["artist"] = mp3_object["TPE1"].text[0]
        except:
            tags["artist"] = "Unknown Artist"
        try:
            tags["album"] = mp3_object["TALB"].text[0]
        except:
            tags["album"] = "Unknown Album"
        try:
            tags["title"] = mp3_object["TIT2"].text[0]
        except:
            tags["title"] = "Unknown Title"
        try:
            m, s = divmod(mp3_object.info.length, 60)
            h, m = divmod(m, 60)
            tags["length"] = "%d:%02d:%02d" % (h, m, s)
        except:
            tags["length"] = "Unknown Length"
        return tags

    def find_all_extension_files(self, directory, extension):
        """
        Finds all mp3 files in a directory
        :param directory: directory where to look for
        :extension: the type of the extension
        :return: list
        """
        return [x for x in os.listdir(directory) if x.endswith(extension)]