-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
165 lines (131 loc) · 6.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import sys
import json
from constants import DEFAULT_PATH_FOR_JSON_FILE
from auto_generated_playlist import AutoGeneratedPlaylistManager, merge_playlists
import traktor
import m3u_playlist_reader
import beets_manager
import import_utils
import scanner
import time
import data
from pathlib import Path
if __name__ == "__main__":
start_time = time.time()
if len(sys.argv) < 2:
config_path = DEFAULT_PATH_FOR_JSON_FILE
else:
config_path = sys.argv[1]
with open(config_path) as json_file:
config = json.load(json_file)
volume = config.get("volume")
tags_configuration_json_file = config.get("tagsConfiguration")
with open(tags_configuration_json_file) as json_file:
tags_configuration_dict = json.load(json_file)
tags_configuration = data.TagsConfiguration.from_dict(tags_configuration_dict)
traktor_collection = config.get("traktor")
beets_db = config.get("beetsLibrary")
playlist_manager = AutoGeneratedPlaylistManager(
tags_configuration.tag_models,
tags_configuration.playlists_to_generate)
# Smart playlist plugin
beet_smart_playlist_path = config.get("m3uPath")
beet_smart_playlist_directory_name = config.get("m3uDirectoryName")
# Custom auto-generated playlists
auto_generated_playlists_directory_name = config.get("generatedPlaylistsDirectoryName")
print(json.dumps(config, indent=4))
traktor_tracks = traktor.get_tracks(
traktor_collection,
volume,
auto_generated_playlists_directory_name,
playlist_manager)
beets_tracks = beets_manager.get_tracks(beets_db, scanner.TAGS_MODEL.keys())
# Utils to import more easily
print("\n===== Starting running utils for beet import ======")
symlink_directory = Path(config.get("temporaryFolderForTracksImportedInTraktorButNotInBeets"))
import_utils.create_links_to_files_imported_in_traktor_but_not_in_beets(
symlink_directory,
traktor_tracks,
beets_tracks
)
import_utils.remove_links_when_imported_in_beets_and_update_traktor_paths(
traktor_collection,
traktor_tracks,
beets_tracks,
volume
)
m3u_playlists = m3u_playlist_reader.list_playlists_at_path(beet_smart_playlist_path)
print("Found %s m3u playlists in %s" %
(len(m3u_playlists), beet_smart_playlist_path))
print("Writing m3u playlists to Traktor %s playlist folder" % beet_smart_playlist_directory_name)
traktor.write_playlists_to_traktor(
traktor_collection,
m3u_playlists,
volume,
beet_smart_playlist_directory_name
)
traktor_tracks = traktor.get_tracks(
traktor_collection,
volume,
auto_generated_playlists_directory_name,
playlist_manager)
print("Found %s ratings in traktor" % len([t for t in traktor_tracks.values() if t.rating is not None]))
print("Found %s tagged tracks in traktor" % len([t for t in traktor_tracks.values() if t.tags]))
beets_tracks = beets_manager.get_tracks(beets_db, scanner.TAGS_MODEL.keys())
print("Found %s ratings in beets" % len([t for t in beets_tracks.values() if t.rating is not None]))
print("=== Exporting ratings and tags to beets ===")
beets_manager.write_tracks_rating_and_tags(beets_db, traktor_tracks)
print("=== Exporting ratings to Traktor ===")
not_in_traktor = list()
for p in beets_tracks:
if beets_tracks.get(p).rating is not None and \
(p not in traktor_tracks or traktor_tracks.get(p).rating is None):
not_in_traktor.append(beets_tracks.get(p))
not_in_beets = list()
for p in traktor_tracks:
if traktor_tracks.get(p).rating is not None and\
(p not in beets_tracks or beets_tracks.get(p).rating is None):
not_in_beets.append(traktor_tracks.get(p))
print("Ratings not in traktor: total %s, extract: %s" %
(len(not_in_traktor), [str(t) for t in not_in_traktor[:10]]))
print("---------------------------")
print("Ratings not in beets: total %s, extract: %s" %
(len(not_in_beets), [str(t) for t in not_in_beets[:10]]))
print("=============")
in_beets_not_in_traktor = [p for p in beets_tracks if p not in traktor_tracks]
in_traktor_not_in_beets = [p for p in traktor_tracks if p not in beets_tracks and "traktor" not in p]
print("Tracks in Traktor not in Beets: total %s, extract: %s" %
(len(in_traktor_not_in_beets), [str(t) for t in in_traktor_not_in_beets[:10]]))
print("-------")
print("Tracks in Beets not in Traktor: total %s, extract: %s" %
(len(in_beets_not_in_traktor), [str(t) for t in in_beets_not_in_traktor[:10]]))
print("=============")
# Refresh beets tracks, tags have possibly been updated after writing traktor->beets
beets_tracks = beets_manager.get_tracks(beets_db, scanner.TAGS_MODEL.keys())
traktor.write_rating_to_traktor_collection(
traktor_collection,
beets_tracks
)
print("Writing comments to Traktor tracks...")
traktor.write_comments_to_traktor_collection(
traktor_collection,
beets_tracks,
scanner.TAGS_MODEL.keys()
)
# Auto-generated playlists
generated_playlists = playlist_manager.playlists_from_tagged_tracks(beets_tracks)
# In case some tracks were added to Traktor playlists but not yet imported in beets, let's keep them by merging
existing_playlists = traktor.list_auto_generated_playlists(
traktor_collection, auto_generated_playlists_directory_name)
merged_playlists = merge_playlists(beets_tracks, generated_playlists, existing_playlists)
print("Found %s auto-generated playlists" % len(generated_playlists))
print("Writing auto-generated playlists to Traktor in folder '%s'" % auto_generated_playlists_directory_name)
traktor.write_playlists_to_traktor(
traktor_collection,
merged_playlists,
volume,
auto_generated_playlists_directory_name
)
print("Done !")
elapsed_time = time.time() - start_time
print('Execution time : %.3f' % elapsed_time)