From b7354fef23e8a08e8c68d98aa50ce0170fc3e326 Mon Sep 17 00:00:00 2001 From: thetarkus Date: Tue, 14 Aug 2018 22:35:10 -0400 Subject: [PATCH 1/4] gmusic plugin: only authenticate when needed --- beetsplug/gmusic.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/beetsplug/gmusic.py b/beetsplug/gmusic.py index 1d5449ab9..ca2aa3b18 100644 --- a/beetsplug/gmusic.py +++ b/beetsplug/gmusic.py @@ -30,23 +30,13 @@ import gmusicapi.clients class Gmusic(BeetsPlugin): def __init__(self): super(Gmusic, self).__init__() + self.m = Musicmanager() self.config.add({ u'auto': False, u'uploader_id': '', u'uploader_name': '', u'device_id': '', }) - # Checks for OAuth2 credentials, - # if they don't exist - performs authorization - self.m = Musicmanager() - if os.path.isfile(gmusicapi.clients.OAUTH_FILEPATH): - uploader_id = self.config['uploader_id'] - uploader_name = self.config['uploader_name'] - self.m.login(uploader_id=uploader_id.as_str().upper() or None, - uploader_name=uploader_name.as_str() or None) - else: - self.m.perform_oauth() - if self.config['auto']: self.import_stages = [self.autoupload] @@ -67,9 +57,23 @@ class Gmusic(BeetsPlugin): search.func = self.search return [gupload, search] + def authenticate(self): + if self.m.is_authenticated(): + return + # Checks for OAuth2 credentials, + # if they don't exist - performs authorization + if os.path.isfile(gmusicapi.clients.OAUTH_FILEPATH): + uploader_id = self.config['uploader_id'] + uploader_name = self.config['uploader_name'] + self.m.login(uploader_id=uploader_id.as_str().upper() or None, + uploader_name=uploader_name.as_str() or None) + else: + self.m.perform_oauth() + def upload(self, lib, opts, args): items = lib.items(ui.decargs(args)) files = self.getpaths(items) + self.authenticate() ui.print_(u'Uploading your files...') self.m.upload(filepaths=files) ui.print_(u'Your files were successfully added to library') @@ -77,6 +81,7 @@ class Gmusic(BeetsPlugin): def autoupload(self, session, task): items = task.imported_items() files = self.getpaths(items) + self.authenticate() self._log.info(u'Uploading files to Google Play Music...', files) self.m.upload(filepaths=files) self._log.info(u'Your files were successfully added to your ' From abffb29a3fcdda8e55496a2d9e2f7ab5a38969de Mon Sep 17 00:00:00 2001 From: thetarkus Date: Tue, 14 Aug 2018 23:30:54 -0400 Subject: [PATCH 2/4] gmusic plugin addition: add oauth_filepath option, better device_id guessing --- beetsplug/gmusic.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/beetsplug/gmusic.py b/beetsplug/gmusic.py index ca2aa3b18..76a219c61 100644 --- a/beetsplug/gmusic.py +++ b/beetsplug/gmusic.py @@ -36,6 +36,7 @@ class Gmusic(BeetsPlugin): u'uploader_id': '', u'uploader_name': '', u'device_id': '', + u'oauth_filepath': '', }) if self.config['auto']: self.import_stages = [self.autoupload] @@ -46,8 +47,7 @@ class Gmusic(BeetsPlugin): gupload.func = self.upload search = Subcommand('gmusic-songs', - help=u'list of songs in Google Play Music library' - ) + help=u'list of songs in Google Play Music library') search.parser.add_option('-t', '--track', dest='track', action='store_true', help='Search by track name') @@ -62,13 +62,16 @@ class Gmusic(BeetsPlugin): return # Checks for OAuth2 credentials, # if they don't exist - performs authorization - if os.path.isfile(gmusicapi.clients.OAUTH_FILEPATH): + oauth_filepath = (self.config['oauth_filepath'].as_str() + or gmusicapi.clients.OAUTH_FILEPATH) + if os.path.isfile(oauth_filepath): uploader_id = self.config['uploader_id'] uploader_name = self.config['uploader_name'] - self.m.login(uploader_id=uploader_id.as_str().upper() or None, + self.m.login(oauth_credentials=oauth_filepath, + uploader_id=uploader_id.as_str().upper() or None, uploader_name=uploader_name.as_str() or None) else: - self.m.perform_oauth() + self.m.perform_oauth(oauth_filepath) def upload(self, lib, opts, args): items = lib.items(ui.decargs(args)) @@ -93,6 +96,7 @@ class Gmusic(BeetsPlugin): def search(self, lib, opts, args): password = config['gmusic']['password'] email = config['gmusic']['email'] + uploader_id = config['gmusic']['uploader_id'] device_id = config['gmusic']['device_id'] password.redact = True email.redact = True @@ -100,8 +104,10 @@ class Gmusic(BeetsPlugin): # we need to use mobileclient interface mobile = Mobileclient() try: - mobile.login(email.as_str(), password.as_str(), - device_id.as_str() or Mobileclient.FROM_MAC_ADDRESS) + new_device_id = (device_id.as_str() + or uploader_id.as_str().replace(':', '') + or Mobileclient.FROM_MAC_ADDRESS).upper() + mobile.login(email.as_str(), password.as_str(), new_device_id) files = mobile.get_all_songs() except NotLoggedIn: ui.print_( From 971b0718c7a4707009554b71fc6a10d83115b3e6 Mon Sep 17 00:00:00 2001 From: thetarkus Date: Tue, 14 Aug 2018 23:44:03 -0400 Subject: [PATCH 3/4] Update gmusic docs --- docs/plugins/gmusic.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/plugins/gmusic.rst b/docs/plugins/gmusic.rst index e760f73a2..e43508a92 100644 --- a/docs/plugins/gmusic.rst +++ b/docs/plugins/gmusic.rst @@ -27,7 +27,8 @@ Configuration is required before use. Below is an example configuration:: password: seekrit auto: yes uploader_id: 00:11:22:33:AA:BB - device_id: F96AE4C643A5 + device_id: 00112233AABB + oauth_filepath: ~/.config/beets/oauth.cred To upload tracks to Google Play Music, use the ``gmusic-upload`` command:: @@ -67,16 +68,19 @@ The available options are: - **auto**: Set to ``yes`` to automatically upload new imports to Google Play Music. Default: ``no`` -- **uploader_id**: Unique id as a MAC address, eg ``'00:11:22:33:AA:BB'``. +- **uploader_id**: Unique id as a MAC address, eg ``00:11:22:33:AA:BB``. This option should be set before the maximum number of authorized devices is reached. If provided, use the same id for all future runs on this, and other, beets installations as to not reach the maximum number of authorized devices. Default: device's MAC address. -- **device_id**: Unique device ID for authorized devices. +- **device_id**: Unique device ID for authorized devices. It is usually + the same as your MAC address with the colons removed, eg ``00112233AABB``. This option only needs to be set if you receive an `InvalidDeviceId` exception. Below the exception will be a list of valid device IDs. Default: none. +- **oauth_filepath**: Filepath for oauth credentials file. + Default: none. Refer to the `Google Play Music Help `__ From e824132137f8617397c35b6c51aed508e1ad6ba1 Mon Sep 17 00:00:00 2001 From: thetarkus Date: Wed, 15 Aug 2018 11:42:04 -0400 Subject: [PATCH 4/4] Rename oauth_filepath to oauth_file, update gmusic doc --- beetsplug/gmusic.py | 11 +++++------ docs/plugins/gmusic.rst | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/beetsplug/gmusic.py b/beetsplug/gmusic.py index 76a219c61..c2fda19d4 100644 --- a/beetsplug/gmusic.py +++ b/beetsplug/gmusic.py @@ -36,7 +36,7 @@ class Gmusic(BeetsPlugin): u'uploader_id': '', u'uploader_name': '', u'device_id': '', - u'oauth_filepath': '', + u'oauth_file': gmusicapi.clients.OAUTH_FILEPATH, }) if self.config['auto']: self.import_stages = [self.autoupload] @@ -62,16 +62,15 @@ class Gmusic(BeetsPlugin): return # Checks for OAuth2 credentials, # if they don't exist - performs authorization - oauth_filepath = (self.config['oauth_filepath'].as_str() - or gmusicapi.clients.OAUTH_FILEPATH) - if os.path.isfile(oauth_filepath): + oauth_file = self.config['oauth_file'].as_str() + if os.path.isfile(oauth_file): uploader_id = self.config['uploader_id'] uploader_name = self.config['uploader_name'] - self.m.login(oauth_credentials=oauth_filepath, + self.m.login(oauth_credentials=oauth_file, uploader_id=uploader_id.as_str().upper() or None, uploader_name=uploader_name.as_str() or None) else: - self.m.perform_oauth(oauth_filepath) + self.m.perform_oauth(oauth_file) def upload(self, lib, opts, args): items = lib.items(ui.decargs(args)) diff --git a/docs/plugins/gmusic.rst b/docs/plugins/gmusic.rst index e43508a92..a4f4c8e05 100644 --- a/docs/plugins/gmusic.rst +++ b/docs/plugins/gmusic.rst @@ -28,7 +28,7 @@ Configuration is required before use. Below is an example configuration:: auto: yes uploader_id: 00:11:22:33:AA:BB device_id: 00112233AABB - oauth_filepath: ~/.config/beets/oauth.cred + oauth_file: ~/.config/beets/oauth.cred To upload tracks to Google Play Music, use the ``gmusic-upload`` command:: @@ -79,8 +79,8 @@ The available options are: This option only needs to be set if you receive an `InvalidDeviceId` exception. Below the exception will be a list of valid device IDs. Default: none. -- **oauth_filepath**: Filepath for oauth credentials file. - Default: none. +- **oauth_file**: Filepath for oauth credentials file. + Default: `{user_data_dir} `__/gmusicapi/oauth.cred Refer to the `Google Play Music Help `__