gmusicupload.py
· 1.4 KiB · Python
Raw
#!/usr/bin/env python3
from gmusicapi import Musicmanager # pip install gmusicapi
from glob import glob
from argparse import ArgumentParser
from sys import stderr
from urllib.parse import quote_plus
m = Musicmanager()
if not m.login():
m.perform_oauth()
m.login()
def upload(path):
uploaded,matched,not_uploaded = m.upload(path)
completed_ids = set()
for fn,_id in uploaded.items():
completed_ids.add(_id)
for fn,_id in matched.items():
completed_ids.add(_id)
for fn,_id in not_uploaded.items():
print("{} not uploaded: {}".format(fn,_id), file=stderr)
return completed_ids
def main():
parser = ArgumentParser()
parser.add_argument("files", nargs="+", help="Files to upload")
args = parser.parse_args()
completed_ids = set()
for file in args.files:
if '*' in file:
for subfile in glob(file):
completed_ids.update(upload(subfile))
else:
completed_ids.update(upload(file))
all_songs = {song["id"]:song for song in m.get_uploaded_songs() if song["id"] in completed_ids}
albums = set()
for _id in completed_ids:
if _id[0] == "T": # gmusic track
print("https://play.google.com/music/m/{}".format(_id))
else:
albums.add("https://play.google.com/music/listen#/album///" + quote_plus(all_songs[_id]["album"]))
print("\n".join(albums))
if __name__ == "__main__":
main()
| 1 | #!/usr/bin/env python3 |
| 2 | from gmusicapi import Musicmanager # pip install gmusicapi |
| 3 | from glob import glob |
| 4 | from argparse import ArgumentParser |
| 5 | from sys import stderr |
| 6 | from urllib.parse import quote_plus |
| 7 | |
| 8 | m = Musicmanager() |
| 9 | if not m.login(): |
| 10 | m.perform_oauth() |
| 11 | m.login() |
| 12 | |
| 13 | def upload(path): |
| 14 | uploaded,matched,not_uploaded = m.upload(path) |
| 15 | completed_ids = set() |
| 16 | for fn,_id in uploaded.items(): |
| 17 | completed_ids.add(_id) |
| 18 | for fn,_id in matched.items(): |
| 19 | completed_ids.add(_id) |
| 20 | for fn,_id in not_uploaded.items(): |
| 21 | print("{} not uploaded: {}".format(fn,_id), file=stderr) |
| 22 | return completed_ids |
| 23 | |
| 24 | def main(): |
| 25 | parser = ArgumentParser() |
| 26 | parser.add_argument("files", nargs="+", help="Files to upload") |
| 27 | args = parser.parse_args() |
| 28 | completed_ids = set() |
| 29 | for file in args.files: |
| 30 | if '*' in file: |
| 31 | for subfile in glob(file): |
| 32 | completed_ids.update(upload(subfile)) |
| 33 | else: |
| 34 | completed_ids.update(upload(file)) |
| 35 | all_songs = {song["id"]:song for song in m.get_uploaded_songs() if song["id"] in completed_ids} |
| 36 | albums = set() |
| 37 | for _id in completed_ids: |
| 38 | if _id[0] == "T": # gmusic track |
| 39 | print("https://play.google.com/music/m/{}".format(_id)) |
| 40 | else: |
| 41 | albums.add("https://play.google.com/music/listen#/album///" + quote_plus(all_songs[_id]["album"])) |
| 42 | print("\n".join(albums)) |
| 43 | |
| 44 | if __name__ == "__main__": |
| 45 | main() |