Beispiel #1
0
def test_update(nml_file, tmpdir):
    nml_file = copy(os.path.join(dir_path, 'fixtures', 'collection.nml'),
                    tmpdir)
    collection = TraktorCollection(nml_file)
    entry = collection.entries[0]
    entry.volume = 'blablu'
    entry.save()
    assert 'blablu' in open(nml_file).read()
Beispiel #2
0
def test_collection(nml_file):
    nml_file = os.path.join(dir_path, 'fixtures', 'collection.nml')
    collection = TraktorCollection(os.path.join(dir_path, nml_file))
    assert collection.entries
    entry = collection.entries[0]
    assert entry.dir == \
        '/:Library/:Application Support/:Native Instruments/:Traktor 2/:Factory Sounds/:'
    assert entry.bitrate == 189720
    assert entry.bpm == 139.999924
    assert entry.volume == 'osx'
Beispiel #3
0
def write_playlists_to_traktor(
        collection_nml_path: str,
        playlists: List[Playlist],
        volume: str,
        folder_name: str):
    collection = TraktorCollection(Path(collection_nml_path))
    init_playlists_root_node(collection)
    delete_playlist_node(collection, folder_name)
    auto_generated_playlists_node = create_playlist_directory(collection.nml.playlists.node, folder_name)
    for p in playlists:
        directory_for_playlist = auto_generated_playlists_node
        if p.tag_keys and p.folder_index:
            subnode_name_prefix = "%02d" % p.folder_index
            subnode_name = subnode_name_prefix + " - " + ", ".join(p.tag_keys)
            directory_for_playlist = create_playlist_directory(auto_generated_playlists_node, subnode_name)

        create_playlist(directory_for_playlist, p.name, volume, p.tracks)

    collection.save()
Beispiel #4
0
def test_history(nml_file):
    collection = TraktorCollection(nml_file)
    assert collection.history
    history_entry = collection.history[0]
    assert history_entry.deck == 1
    assert history_entry.duration == 241.700394
    assert history_entry.played_public
    assert history_entry.startdate == 132319762
    assert history_entry.starttime == 68839
    assert history_entry.filename == \
        'Transcend/:DJing/:beatport_steve/:09 Change (Like Mix Extended Instrumental).mp3'
Beispiel #5
0
def test_cuepoints(nml_file):
    collection = TraktorCollection(nml_file)
    entry = collection.entries[0]
    assert len(entry.cuepoints) == 3
    cuepoint = entry.cuepoints[0]
    assert cuepoint.name == 'AutoGrid'
    assert cuepoint.cuepoint_type == 4
    assert cuepoint.start == 52.315876
    assert cuepoint.length == 0
    assert cuepoint.repeats == -1
    assert cuepoint.hotcue == 0
Beispiel #6
0
def write_comments_to_traktor_collection(
        collection_nml: str,
        tracks: Dict[str, Track],
        tags_list: List[str]):
    collection = TraktorCollection(Path(collection_nml))
    for t in collection.nml.collection.entry:
        path = str(traktor_path_to_pathlib_path(t.location.dir, t.location.file)).lower()

        if path in tracks:
            t.info.comment = _tags_to_comment(tracks[path].tags, tags_list)

    _save_collection(collection)
Beispiel #7
0
def import_nml(path: Path):
    print(f"parsing file: {path.name}")

    if is_history_file(path):
        history = TraktorHistory(path=path)
        entry = HistoryEntry()
        entry.file = history.nml.playlists.node.subnodes.node.playlist.entry[
            0].primarykey.key
        print(entry.file)
        entry.save()
    else:
        collection = TraktorCollection(path=path)
        bar = Bar(f"Processing file: {path.name}",
                  max=len(collection.nml.collection.entry))

        for nml_entry in collection.nml.collection.entry:
            db_entry = CollectionEntry()
            nml_file = NMLFile(file=path.name)
            nml_file.save()
            db_entry.nml_file = nml_file
            db_entry.artist = nml_entry.artist
            db_entry.title = nml_entry.title
            db_entry.audio_id = nml_entry.audio_id
            if nml_entry.tempo and nml_entry.tempo.bpm:
                db_entry.bpm = int(nml_entry.tempo.bpm)
            if nml_entry.album:
                db_entry.album = nml_entry.album.title
            if nml_entry.modified_date:
                db_entry.modified_date = datetime.datetime.strptime(
                    nml_entry.modified_date, "%Y/%m/%d")
            if nml_entry.info:
                db_entry.ranking = nml_entry.info.ranking
                db_entry.bitrate = nml_entry.info.bitrate
                db_entry.genre = nml_entry.info.genre
                db_entry.playtime = nml_entry.info.playtime_float
                db_entry.playcount = nml_entry.info.playcount
                db_entry.comment = nml_entry.info.comment
                db_entry.comment2 = nml_entry.info.rating
                if nml_entry.info.last_played:
                    db_entry.last_played = datetime.datetime.strptime(
                        nml_entry.info.last_played, "%Y/%m/%d")
                db_entry.key = nml_entry.info.key
                if nml_entry.info.release_date:
                    db_entry.release_date = datetime.datetime.strptime(
                        nml_entry.info.release_date, "%Y/%m/%d")
                if nml_entry.info.import_date:
                    db_entry.import_date = datetime.datetime.strptime(
                        nml_entry.info.import_date, "%Y/%m/%d")
            db_entry.file = nml_entry.location.dir + nml_entry.location.file
            db_entry.save()
            bar.next()
        bar.finish()
Beispiel #8
0
def test_playlists(nml_file):
    collection = TraktorCollection(nml_file)
    assert collection.playlists
    assert len(collection.playlists) == 3
    playlist = collection.playlists[0]
    assert playlist.name == 'Preparation'
    assert len(playlist.entries) == 1
    playlist = collection.playlists[1]
    assert playlist.name == 'Foo/Preparation2'
    assert len(playlist.entries) == 1
    playlist = collection.playlists[2]
    assert playlist.name == 'Foo/Bar/Preparation3'
    assert len(playlist.entries) == 1
def traktor_import(nml):
    """NML import from file or directory."""
    p = Path(nml)
    if p.is_dir():
        nml_files = p.glob('**/*.nml')
    else:
        nml_files = [p]

    for nml_file in nml_files:
        print(f"Importing NML: {nml_file}")
        if is_history_file(nml_file):
            TraktorHistory(nml_file)
        else:
            TraktorCollection(nml_file)
Beispiel #10
0
def write_rating_to_traktor_collection(
        collection_nml: str,
        tracks: Dict[str, Track]):
    collection = TraktorCollection(Path(collection_nml))
    for t in collection.nml.collection.entry:
        path = str(traktor_path_to_pathlib_path(t.location.dir, t.location.file)).lower()

        # print(":".join("{:02x}".format(ord(c)) for c in path))

        if path in tracks and tracks[path].rating is not None:
            t.info.ranking = 51 * tracks[path].rating
            if t.info.ranking == 0:
                t.info.ranking = 51

    _save_collection(collection)
Beispiel #11
0
def update_tracks_locations(
        collection_nml: str,
        old_to_new_locations: Dict[Path, Path],
        volume: str):

    collection = TraktorCollection(Path(collection_nml))
    count = 0
    for t in collection.nml.collection.entry:
        path = Path(str(traktor_path_to_pathlib_path(t.location.dir, t.location.file)).lower())
        if path in old_to_new_locations:
            new_path = old_to_new_locations[path]
            t.location.dir, t.location.file = pathlib_path_to_traktor_dir_and_file_couple(new_path)
            count += 1
            print("TRAKTOR: Replaced %s by %s" % (path, new_path))

    if collection.nml.playlists and collection.nml.playlists.node:
        update_tracks_locations_in_playlists(collection.nml.playlists.node, old_to_new_locations, volume)


    _save_collection(collection)
    print("Relocated %s tracks in Traktor" % count)
Beispiel #12
0
def get_tracks(
        collection_nml: str,
        volume: str,
        auto_generated_playlists_folder: str,
        playlist_manager: AutoGeneratedPlaylistManager) -> Dict[str, Track]:
    result = dict()
    collection = TraktorCollection(Path(collection_nml))

    auto_generated_playlists = list_playlists_in_collection(collection, auto_generated_playlists_folder)

    tagged_tracks_not_flattened = playlist_manager.tagged_tracks_from_playlists(auto_generated_playlists)

    for t in collection.nml.collection.entry:
        if t.location.volume != volume:
            continue

        path = str(traktor_path_to_pathlib_path(t.location.dir, t.location.file)).lower()
        tags = dict()
        comment = None
        if t.info and t.info.comment:
            comment = t.info.comment
        if path in tagged_tracks_not_flattened:
            for tagged_track in tagged_tracks_not_flattened[path]:
                for tag_key, tag_value in tagged_track.tags.items():
                    if tag_key not in tags:
                        tags[tag_key] = tag_value
                    elif tags[tag_key] != tag_value:
                        # Conflict between playlists, this means the user has placed the track in multiple playlists
                        # To see which tags are the previous/new values, let's check the comment
                        if tags[tag_key] in comment and \
                                (tag_value not in comment or len(tag_value) < len(tags[tag_key])):
                            tags[tag_key] = tag_value

        track = Track(path=Path(path), tags=tags, rating=None, comment=comment)
        if t.info.ranking is not None and int(t.info.ranking) >= 51:
            track.rating = t.info.ranking / 51
        if t.album is not None and t.album.title:
            track.album = t.album.title
        result[path] = track
    return result
def test_playlists(nml_file):
    collection = TraktorCollection(nml_file)
    assert collection.playlists
    playlist = collection.playlists[0]
    assert playlist.name == 'Preparation'
    assert len(playlist.entries) == 1
Beispiel #14
0
def test_add_entry_to_collection(tmpdir):
    with tempfile.TemporaryFile() as fp:
        collection_file = Path(os.path.join(dir_path, "fixtures", "collection.nml"))
        temp_collection = tmpdir.join("collection.nml")
    shutil.copy(src=collection_file, dst=temp_collection)
    print(temp_collection)

    path = Path(os.path.join(dir_path, "fixtures", "collection.nml"))
    collection = TraktorCollection(path)
    entry = Entrytype(
        location=Locationtype(
            value=None,
            dir="/:Library/:Application Support/:Native Instruments/:Traktor 2/:Factory Sounds/:",
            file="Loopmasters_Dubstep1.mp3",
            volume="osx",
            volumeid="osx",
        ),
        album=None,
        modification_info=ModificationInfotype(value=None, author_type="user"),
        info=Infotype(
            value=None,
            bitrate=189720,
            genre="Dubstep",
            comment="Tracks by www.loopmasters.com",
            coverartid="113/R1PI3ZDLWQMLAAASJ4B2AQZXI1ZD",
            key="10m",
            playtime=193,
            playtime_float=192.078369,
            import_date="2010/8/16",
            release_date="2010/1/1",
            flags=28,
            filesize=5040,
            label=None,
            key_lyrics=None,
            catalog_no=None,
            playcount=None,
            ranking=None,
            last_played=None,
            remixer=None,
            rating=None,
            producer=None,
            mix=None,
        ),
        tempo=Tempotype(
            value=None, bpm=139.999924, bpm_quality=100.0, bpm_transientcoherence=None
        ),
        loudness=Loudnesstype(
            value=None, peak_db=-2.78208, perceived_db=0.0, analyzed_db=-2.0
        ),
        musical_key=MusicalKeytype(value=None, value_attribute=12),
        loopinfo=None,
        cue_v2=[
            CueV2Type(
                value=None,
                name="AutoGrid",
                displ_order=0,
                type=4,
                start=52.315876,
                len=0.0,
                repeats=-1,
                hotcue=0,
            ),
            CueV2Type(
                value=None,
                name="n.n.",
                displ_order=0,
                type=0,
                start=52.315876,
                len=0.0,
                repeats=-1,
                hotcue=7,
            ),
            CueV2Type(
                value=None,
                name="n.n.",
                displ_order=0,
                type=0,
                start=52.315876,
                len=0.0,
                repeats=-1,
                hotcue=6,
            ),
        ],
        stems=None,
        primarykey=None,
        modified_date="2019/10/19",
        modified_time=13047,
        lock=1,
        lock_modification_time="2019-08-23T21:27:21",
        title="Dubstep 1",
        artist="Loopmasters",
    )
    collection.nml.collection.entry.append(entry)
    collection.save()
    assert len(TraktorCollection(path).nml.collection.entry) == 2
Beispiel #15
0
def test_collection_with_indexing():
    path = Path(os.path.join(dir_path, "fixtures", "collection_indexing.nml"))
    collection = TraktorCollection(path)
    assert len(collection.nml.collection.entry) == 1
Beispiel #16
0
def list_auto_generated_playlists(
        collection_nml: str,
        auto_generated_playlists_folder: str) -> List[Playlist]:
    collection = TraktorCollection(Path(collection_nml))
    return list_playlists_in_collection(collection, auto_generated_playlists_folder)