-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.py
executable file
·131 lines (104 loc) · 4.86 KB
/
upload.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
#!/usr/bin/env python3
# coding=utf-8
"""
An upload script for Google Music using https://github.com/simon-weber/gmusicapi.
More information at https://github.com/thebigmunch/gmusicapi-scripts.
Usage:
gmupload (-h | --help)
gmupload [-e PATTERN]... [-f FILTER]... [-F FILTER]... [options] [<input>]...
Arguments:
input Files, directories, or glob patterns to upload.
Defaults to current directory.
Options:
-h, --help Display help message.
-c CRED, --cred CRED Specify oauth credential file name to use/create. [Default: oauth]
-U ID --uploader-id ID A unique id given as a MAC address (e.g. '00:11:22:33:AA:BB').
This should only be provided when the default does not work.
-l, --log Enable gmusicapi logging.
-m, --match Enable scan and match.
-d, --dry-run Output list of songs that would be uploaded.
-q, --quiet Don't output status messages.
With -l,--log will display gmusicapi warnings.
With -d,--dry-run will display song list.
--delete-on-success Delete successfully uploaded local files.
-R, --no-recursion Disable recursion when scanning for local files.
This is equivalent to setting --max-depth to 0.
--max-depth DEPTH Set maximum depth of recursion when scanning for local files.
Default is infinite recursion.
Has no effect when -R, --no-recursion set.
-e PATTERN, --exclude PATTERN Exclude file paths matching a Python regex pattern.
-f FILTER, --include-filter FILTER Include local songs by field:pattern filter (e.g. "artist:Muse").
Songs can match any filter criteria.
This option can be set multiple times.
-F FILTER, --exclude-filter FILTER Exclude local songs by field:pattern filter (e.g. "artist:Muse").
Songs can match any filter criteria.
This option can be set multiple times.
-a, --all-includes Songs must match all include filter criteria to be included.
-A, --all-excludes Songs must match all exclude filter criteria to be excluded.
Patterns can be any valid Python regex patterns.
"""
import logging
import os
import sys
from docopt import docopt
from gmusicapi_wrapper import MusicManagerWrapper
QUIET = 25
logging.addLevelName(25, "QUIET")
logger = logging.getLogger('gmusicapi_wrapper')
sh = logging.StreamHandler()
logger.addHandler(sh)
def main():
cli = dict((key.lstrip("-<").rstrip(">"), value) for key, value in docopt(__doc__).items())
if cli['no-recursion']:
cli['max-depth'] = 0
else:
cli['max-depth'] = int(cli['max-depth']) if cli['max-depth'] else float('inf')
if cli['quiet']:
logger.setLevel(QUIET)
else:
logger.setLevel(logging.INFO)
if not cli['input']:
cli['input'] = [os.getcwd()]
mmw = MusicManagerWrapper(enable_logging=cli['log'])
mmw.login(oauth_filename=cli['cred'], uploader_id=cli['uploader-id'])
if not mmw.is_authenticated:
sys.exit()
include_filters = [tuple(filt.split(':', 1)) for filt in cli['include-filter']]
exclude_filters = [tuple(filt.split(':', 1)) for filt in cli['exclude-filter']]
songs_to_upload, songs_to_filter, songs_to_exclude = mmw.get_local_songs(
cli['input'], include_filters=include_filters, exclude_filters=exclude_filters,
all_includes=cli['all-includes'], all_excludes=cli['all-excludes'],
exclude_patterns=cli['exclude'], max_depth=cli['max-depth']
)
songs_to_upload.sort()
songs_to_exclude.sort()
if cli['dry-run']:
logger.info("\nFound {0} song(s) to upload".format(len(songs_to_upload)))
if songs_to_upload:
logger.info("\nSongs to upload:\n")
for song in songs_to_upload:
logger.log(QUIET, song)
else:
logger.info("\nNo songs to upload")
if songs_to_filter:
logger.info("\nSongs to filter:\n")
for song in songs_to_filter:
logger.log(QUIET, song)
else:
logger.info("\nNo songs to filter")
if songs_to_exclude:
logger.info("\nSongs to exclude:\n")
for song in songs_to_exclude:
logger.log(QUIET, song)
else:
logger.info("\nNo songs to exclude")
else:
if songs_to_upload:
logger.info("\nUploading {0} song(s) to Google Music\n".format(len(songs_to_upload)))
mmw.upload(songs_to_upload, enable_matching=cli['match'], delete_on_success=cli['delete-on-success'])
else:
logger.info("\nNo songs to upload")
mmw.logout()
logger.info("\nAll done!")
if __name__ == '__main__':
main()