From b7354fef23e8a08e8c68d98aa50ce0170fc3e326 Mon Sep 17 00:00:00 2001 From: thetarkus Date: Tue, 14 Aug 2018 22:35:10 -0400 Subject: [PATCH] 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 '