-
Notifications
You must be signed in to change notification settings - Fork 0
/
music_manager.py
78 lines (62 loc) · 2.21 KB
/
music_manager.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
import os
from gmusicapi import Musicmanager
from logs.logger import mm_logger
current_dir = os.path.dirname(os.path.realpath(__file__))
def upload(itr: iter, conf: object) -> tuple:
"""
Takes iterable of music files and uploads all files to Google Music in 320 kbps mp3
Args:
itr: iterable of absolute file paths to songs of a single extension (mp3 or flac, etc.)
conf: config object from run.py
Returns:
result: tuple of three dictionaries
(
{'<filepath>': '<new server id>'}, # uploaded
{'<filepath>': '<new server id>'}, # matched
{'<filepath>': '<reason, eg ALREADY_EXISTS>'} # not uploaded
)
Also returns false if ctrl-c hit during upload process.
"""
mm = Musicmanager(debug_logging=False)
oauth_path = conf.get("oauth_file")
is_logged_in = mm.login(oauth_credentials=oauth_path)
if is_logged_in:
mm_logger.info("Uploading (may take awhile)...")
mm_logger.info("Hold Ctrl-c to abort.")
try:
result = mm.upload(itr)
mm_logger.info("Done.")
except KeyboardInterrupt:
# If user ctrl-c's out (aborts upload), return False
mm_logger.warning("Upload cancelled.")
result = False
mm.logout()
return result
def parse_result(result: tuple, conf: object) -> None:
"""
Takes readout from upload payload and parses what was uploaded, matched, and not uploaded
Args:
result: tuple of three dictionaries (see upload function docstring for details)
conf: config object from run.py
"""
failed_uploads = list(conf.get("failed_uploads"))
mm_logger.info("Result:")
mm_logger.info("\tUploaded:")
for uploaded in result[0]:
mm_logger.info("\t\t" + uploaded)
mm_logger.info("\tMatched:")
for matched in result[1]:
mm_logger.info("\t\t" + matched)
mm_logger.warning("\tNot uploaded:")
for n_uploaded in result[2]:
mm_logger.warning("\t\t" + n_uploaded)
reason = result[2][n_uploaded]
if "ALREADY_EXISTS" not in reason:
# Song failed to upload but not because it already existed in google music
# stage for next upload
mm_logger.warning("\t\t\tReason: {}".format(reason))
failed_uploads.append(n_uploaded)
else:
mm_logger.warning("\t\t\tReason: Already Exists")
# Save out failed uploads
conf.set("failed_uploads", failed_uploads)