Skip to content

Commit

Permalink
Merge pull request #3004 from thetarkus/gmusic-additions-2
Browse files Browse the repository at this point in the history
gmusic plugin fixes and additions
  • Loading branch information
sampsyo authored Aug 15, 2018
2 parents cab6910 + e824132 commit 890ba85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
40 changes: 25 additions & 15 deletions beetsplug/gmusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,14 @@
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]

Expand All @@ -56,8 +47,7 @@ def commands(self):
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')
Expand All @@ -67,16 +57,33 @@ def commands(self):
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')

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 '
Expand All @@ -88,15 +95,18 @@ def getpaths(self, items):
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
# Since Musicmanager doesn't support library management
# 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_(
Expand Down
10 changes: 7 additions & 3 deletions docs/plugins/gmusic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -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} <https://pypi.org/project/appdirs/>`__/gmusicapi/oauth.cred

Refer to the `Google Play Music Help
<https://support.google.com/googleplaymusic/answer/3139562?hl=en>`__
Expand Down

0 comments on commit 890ba85

Please sign in to comment.