-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (178 loc) · 6.06 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Create a spotify playlist based on the mp3 files in a directory.
"""
import argparse
import logging
import colorlog
import os
import sys
from pathlib import Path
from uuid import uuid4
# my modules
from mltk.genres import clean_tags, scrape_genres
from mltk.spotiply import (
get_liked_songs,
get_playlist_items,
create_spotify_playlist,
generate_credentials_json,
spotify_connect,
get_spotify_track_id,
music_dir_to_json,
rbox_to_json,
)
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(THIS_DIR, "data/")
def configure_logger(log_to_screen=False):
"""Setup the logger"""
# initialise logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# setup formatters
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s')
color_formatter = colorlog.ColoredFormatter(
"%(green)s%(asctime)s%(reset)s %(light_black)s%(name)s%(reset)s %(log_color)s%(levelname)s:%(reset)s %(message)s",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
)
# add file handler
fh = logging.FileHandler("mltk.log", mode="w")
fh.setFormatter(formatter)
fh.setLevel(logging.INFO)
logger.addHandler(fh)
# add console handler
if log_to_screen:
ch = colorlog.StreamHandler()
ch.setFormatter(color_formatter)
# ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
# other loggers
loggers = [
"mltk.spotiply",
"mltk.genres",
]
for logger_name in loggers:
_logger = logging.getLogger(logger_name)
_logger.setLevel(logging.DEBUG)
_logger.addHandler(fh)
if log_to_screen:
_logger.addHandler(ch)
# logger from other libraries
logging.getLogger("eyed3").setLevel(logging.ERROR)
return logger
def parse_args():
parser = argparse.ArgumentParser(description="Music Library Toolkit")
subparsers = parser.add_subparsers(dest="command", required=True)
spotiply = subparsers.add_parser(
"spotiply",
help="Create a spotify playlist based on the mp3 files in a directory",
)
group1 = spotiply.add_mutually_exclusive_group(required=True)
group1.add_argument(
"-cp",
dest="create_playlist",
metavar="MUSIC_DIR",
help="Create spotify playlist based on the mp3 files in MUSIC_DIR",
)
group1.add_argument(
"-j",
dest="use_json",
metavar="JSON_FILE",
help="Create spotify playlist using the json_file passed, instead of a music directory.",
)
group1.add_argument(
"-rb",
dest="use_rb",
metavar="REKORDBOX_TXT",
help="Create spotify playlist using the rekordbox playlist file REKORDBOX_TXT instead of a music directory.",
)
group1.add_argument(
"-c",
"--credentials",
action="store_true",
help="Generate credentials.json file",
)
group1.add_argument(
"-ls",
"--liked-songs",
action="store_true",
help="Generate json of your liked songs.",
)
group1.add_argument(
"--playlist-songs",
metavar="PLAYLIST_URL",
help="Generate txt file of the songs from the given spotify playlist PLAYLIST_URL",
)
group2 = spotiply.add_argument_group("To be used with --create-playlist")
group2.add_argument(
"-dp",
"--disable-playlist",
action="store_true",
help="Will disable creating the actual spotify playlist.",
)
group2.add_argument(
"-p",
dest="playlist_name",
type=str,
help="Name of the playlist you want to create. If not provided will use a uuid.",
)
tag_utils = subparsers.add_parser("tag_utils", help="Utilities to clean mp3 tags")
group3 = tag_utils.add_mutually_exclusive_group(required=True)
group3.add_argument(
"-g",
dest="clean_genres",
metavar="MUSIC_DIR",
help="Clean the genres and other tag information for the mp3 files in MUSIC_DIR.",
)
group3.add_argument(
"-ga",
dest="clean_genres2",
metavar="MUSIC_DIR",
help="Clean the genres and other tag information (using artist to determine the genre) for the mp3 files in MUSIC_DIR.",
)
group3.add_argument(
"-ug",
dest="update_genres",
action="store_true",
help="Updates the genre mapping json.",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
logger = configure_logger(log_to_screen=True)
# create data dir if doesnt exist
Path(DATA_DIR).mkdir(parents=True, exist_ok=True)
if args.command == "spotiply":
sp = spotify_connect()
if args.credentials:
generate_credentials_json()
elif args.liked_songs:
get_liked_songs(sp)
elif args.playlist_songs:
get_playlist_items(sp, args.playlist_songs)
elif args.create_playlist or args.use_json or args.use_rb:
playlist_name = args.playlist_name if args.playlist_name else uuid4().hex
json_file = os.path.join(DATA_DIR, playlist_name + ".json")
if args.use_json:
playlist_name = Path(args.use_json).stem
create_spotify_playlist(sp, playlist_name, args.use_json)
else:
if args.use_rb:
rbox_to_json(args.use_rb, json_file)
else:
music_dir_to_json(args.create_playlist, json_file)
get_spotify_track_id(sp, json_file)
if not args.disable_playlist:
create_spotify_playlist(sp, playlist_name, json_file)
elif args.command == "tag_utils":
if args.clean_genres:
clean_tags(args.clean_genres)
elif args.clean_genres2:
clean_tags(args.clean_genres2, use_artist_genre=True)
elif args.update_genres:
scrape_genres()