Last active 1551993911

A script to upload music to Google Play Music

Revision e9b1006f1f9b09694f8598c7b3333d89501152bd

gmusicupload.py Raw
1#!/usr/bin/env python3
2from gmusicapi import Musicmanager # pip install gmusicapi
3from glob import glob
4from argparse import ArgumentParser
5from sys import stderr
6from urllib.parse import quote_plus
7
8m = Musicmanager()
9if not m.login():
10 m.perform_oauth()
11 m.login()
12
13def 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
24def 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
44if __name__ == "__main__":
45 main()