Skip to content

Commit

Permalink
Use the keyring package for password storage
Browse files Browse the repository at this point in the history
Fixes GH #11.
  • Loading branch information
malept committed Jun 14, 2014
1 parent 6c8a432 commit 28213d3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
20 changes: 15 additions & 5 deletions INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ The contents of the file will look like this:
.. code-block:: python
GACCOUNT_EMAIL = '[email protected]'
GACCOUNT_PASSWORD = 'my-password'
Then run the following (lines that start with ``#`` are comments, not commands):

Expand All @@ -69,12 +68,23 @@ Then run the following (lines that start with ``#`` are comments, not commands):
# Only run the next line if you wish to use libsass-python instead of the
# Ruby version of Sass:
(venv)user@host:gmusicprocurator$ pip install libsass
(venv)user@host:gmusicprocurator$ python -m gmusicprocurator set_password
The last command will activate an interactive prompt that will store your
Google account password (or, if your account has two-factor authentication
enabled, your application-specific password) into the operating system's
password storage service.

Once your password is set, you will need to associate GMusicProcurator with one
of your mobile devices. Run the following command to list the devices:

.. code-block:: shell-session
(venv)user@host:gmusicprocurator$ python -m gmusicprocurator list_devices --no-desktop
The last command will print out a list of mobile devices that are registered
with Google Music. Select one of them and add the following to the config file
from above (substituting ``REPLACE_ME`` with the ID, which is after the colon
in the device ID printout):
Select one of them and add the following to the config file from above
(substituting ``REPLACE_ME`` with the ID, which is after the colon in the
device ID printout):

.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gmusicprocurator",
"version": "1.0dev3",
"version": "1.0dev4",
"homepage": "https://gmusicprocurator.readthedocs.org/",
"authors": [
"Mark Lee"
Expand Down
6 changes: 0 additions & 6 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ Google

The Google Account email address that has access to Google Music.

.. attribute:: GACCOUNT_PASSWORD

The Google Account password associated with the email address specified in
:attr:`GACCOUNT_EMAIL`. If you use two-factor authentiation, this is an
app-specific password.

.. attribute:: GACCOUNT_DEVICE_ID

The mobile device ID to use to access Google Music. See :doc:`the
Expand Down
35 changes: 32 additions & 3 deletions gmusicprocurator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from flask.ext.script import Manager
from functools import partial
from getpass import getpass
from gmusicprocurator.app import app, heapy
import sys

Expand All @@ -44,6 +45,11 @@ def dump_memory_usage(signum, frame):
from gmusicprocurator.app import assets
manager.add_command('assets', ManageAssets(assets))


def error(msg):
"""Print an error message (prepended by ``ERROR: ``) to stderr."""
print('ERROR: {0}'.format(msg), file=sys.stderr)

no_bool_option = partial(manager.option, action='store_false', default=True)


Expand All @@ -56,11 +62,16 @@ def list_devices(show_desktop, show_mobile):
Defaults to showing both desktop and mobile IDs.
"""
from gmusicapi.clients import Webclient
from keyring import get_password
webclient = Webclient()
success = webclient.login(app.config['GACCOUNT_EMAIL'],
app.config['GACCOUNT_PASSWORD'])
email = app.config['GACCOUNT_EMAIL']
password = get_password('gmusicprocurator', email)
if password is None:
error('Password not set. Please run the set_password subcommand.')
return
success = webclient.login(email, password)
if not success:
print('Login failed.', file=sys.stderr)
error('Login failed.')
return

for device in webclient.get_registered_devices():
Expand All @@ -72,6 +83,24 @@ def list_devices(show_desktop, show_mobile):
print(u'* {dname} ({type}): {id}'.format(dname=dname, **device))


@manager.command
def set_password():
"""Set the Google account password."""
import keyring
password = None
repeated = None
while password is None or password != repeated:
password = getpass('Please enter your password: ')
repeated = getpass('Please verify your password: ')
if password == repeated:
keyring.set_password('gmusicprocurator',
app.config['GACCOUNT_EMAIL'],
password)
print('Password set successfully.')
else:
error('Passwords do not match.')


def run():
"""Flask-Script convenience runner."""
manager.run()
Expand Down
9 changes: 7 additions & 2 deletions gmusicprocurator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@
music = None
else:
from gmusicapi import Mobileclient
from keyring import get_password
music = Mobileclient()
music.login(app.config['GACCOUNT_EMAIL'],
app.config['GACCOUNT_PASSWORD'])
email = app.config['GACCOUNT_EMAIL']
password = get_password('gmusicprocurator', email)
if password is None:
music = None
else:
music.login(email, password)

if app.debug and app.config['GMP_MEMORY_PROFILER']:
from guppy import hpy
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Flask
Flask-Script
gmusicapi
keyring
# xspf
git+https://github.com/alastair/xspf.git#egg=xspf

0 comments on commit 28213d3

Please sign in to comment.