Exemplo n.º 1
0
    def test_document_keys_should_be_uppercase(self):
        reader = TagReader()

        document = reader.readfile(self.filename)

        {value: self.assertTrue(value.isupper(), 'Expecting key to be upper but was {0}'.format(value)) for value in
         document}
Exemplo n.º 2
0
    def test_should_read_files_in_folder(self):
        reader = TagReader()

        documents = reader.readfolder(self.foldername)

        import glob
        expectedCount = 5
        self.assertIs(expectedCount, len(documents))
        {document: self.assertTrue(document, 'Expecting document not to be empty') for document in documents}
Exemplo n.º 3
0
    def test_should_read_file_content(self):
        reader = TagReader()
        tags = flac.FLAC(self.filename).tags

        document = reader.readfile(self.filename)

        [(tagName, self._getTagName(tagName, tagValue)) for (tagName, tagValue) in tags]
        self.assertEqual(self.expectedDocument['ALBUM'], document['ALBUM'])
        self.assertEqual(self.expectedDocument['ARTIST'], document['ARTIST'])
Exemplo n.º 4
0
 def test_should_not_override_existing_values(self):
     reader = TagReader()
     title = reader.readfile(self.filename)['TITLE']
     writer = TagWriter()
     from time import time
     now = str(time() * 1000)
     tags = {
         'ALBUM': now
     }
     writer.write(tags, self.filename)
     self.assertEqual(title, reader.readfile(self.filename)['TITLE'])
Exemplo n.º 5
0
 def test_should_write_tags(self):
     writer = TagWriter()
     from time import time
     now = str(time() * 1000)
     tags = {
         'ALBUM': now
     }
     writer.write(tags, self.filename)
     reader = TagReader()
     document = reader.readfile(self.filename)
     self.assertEqual(document['ALBUM'], now)
Exemplo n.º 6
0
    def test_documents_should_not_be_empty(self):
        reader = TagReader()

        documents = reader.readfolder(self.foldername)

        self.assertTrue(documents)
Exemplo n.º 7
0
    def test_documents_should_not_be_null(self):
        reader = TagReader()

        documents = reader.readfolder(self.foldername)

        self.assertIsNotNone(documents, 'Expecting documents not to be null')
Exemplo n.º 8
0
    def test_document_should_contain_file_name(self):
        reader = TagReader()

        document = reader.readfile(self.filename)

        self.assertTrue(document['PATH'])
Exemplo n.º 9
0
 def test_document_should_not_be_empty(self):
     reader = TagReader()
     document = reader.readfile(self.filename)
     self.assertTrue(document, 'Expecting not empty document')
Exemplo n.º 10
0
 def test_document_should_not_be_null(self):
     reader = TagReader()
     document = reader.readfile(self.filename)
     self.assertIsNotNone(document, 'Expecting tag document not to be null')
Exemplo n.º 11
0
 def __init__(self, library_name):
     self.library_name = library_name
     self.client = MongoClient()
     self.collection = {}
     self.tag_reader = TagReader()
     self.tag_writer = TagWriter()
Exemplo n.º 12
0
class Library:
    def __init__(self, library_name):
        self.library_name = library_name
        self.client = MongoClient()
        self.collection = {}
        self.tag_reader = TagReader()
        self.tag_writer = TagWriter()

    def restore(self):
        db = self.client[self.library_name]
        self.collection = db.tags
        return self

    def create(self, folder_name, listener=NullListener()):
        db = self.client[self.library_name]
        db.drop_collection('tags')
        self.collection = db.tags
        documents = self.tag_reader.readfolder(folder_name, listener).items()
        for key, tag in documents:
            self.collection.insert_one(tag)
        return self

    def track_details(self, track_id):
        return self.collection.find_one({'_id': track_id})

    def load_single_picture(self, track):
        return self.tag_reader.read_pictures(track['PATH'])[0]

    def replace_cover_art(self, raw_data, track):
        print("Saving cover art in {}".format(track['PATH']))
        self.tag_writer.replace_picture(raw_data, track['PATH'])

    def view(self):
        return LibraryView(self.collection)

    def aggregate(self, match, group_by):
        if group_by == 'TITLE':
            return [track['TITLE'] for track in self.find_tracks(match)]
        pipeline = [
            {"$match": match},
            {"$group": {"_id": "$" + group_by}}
        ]
        cur = self.collection.aggregate(pipeline)
        return sorted([item['_id'] for item in cur])

    def find_tracks(self, match):
        return list(self.collection.find(match).sort('TRACKNUMBER'))

    def store(self, track_id, tags):
        file_name = tags['PATH']
        self.collection.replace_one({'_id': track_id}, tags)
        self.tag_writer.write(tags, file_name)

    def rename(self, tracks, base_folder):
        for track in tracks:
            composer = track['COMPOSER']
            genre = track['GENRE']
            album = track['ALBUM']
            artist = track['ARTIST']
            year = track['DATE']
            track_number = track['TRACKNUMBER']
            new_folder = "{}\\{}\\{}\\{}\\{}, {}".format(base_folder, composer, genre, album, artist, year)
            import os
            os.makedirs(new_folder, exist_ok=True)
            new_path = "{}\\{}.flac".format(new_folder, track_number.zfill(2))
            # print(new_path)
            existing_path = track['PATH']
            print(existing_path)
            if new_path != existing_path:
            # from shutil import rename
                import shutil
                shutil.move(existing_path, new_path)

    def __str__(self, *args, **kwargs):
        return "Library {}".format(self.library_name)