diff --git a/beetsplug/gmusic.py b/beetsplug/gmusic.py index 1d5449ab9..c2fda19d4 100644 --- a/beetsplug/gmusic.py +++ b/beetsplug/gmusic.py @@ -30,23 +30,14 @@ 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': '', + u'oauth_file': gmusicapi.clients.OAUTH_FILEPATH, }) - # 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] @@ -56,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') @@ -67,9 +57,25 @@ 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 + 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_file, + uploader_id=uploader_id.as_str().upper() or None, + uploader_name=uploader_name.as_str() or None) + else: + self.m.perform_oauth(oauth_file) + 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 +83,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 ' @@ -88,6 +95,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 @@ -95,8 +103,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_( diff --git a/docs/plugins/gmusic.rst b/docs/plugins/gmusic.rst index e760f73a2..a4f4c8e05 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_file: ~/.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_file**: Filepath for oauth credentials file. + Default: `{user_data_dir} `__/gmusicapi/oauth.cred Refer to the `Google Play Music Help `__