Skip to content

Commit

Permalink
Merge pull request #42 from erik/add-verbose
Browse files Browse the repository at this point in the history
Add --verbose flag.
  • Loading branch information
erik authored Mar 30, 2018
2 parents f52c77b + 933bec8 commit 39001f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
66 changes: 38 additions & 28 deletions lastcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import os.path
import sys
import time
import traceback

import click
import pylast
import pychromecast
import toml

from pychromecast.error import PyChromecastError
import pylast
import toml


logging.basicConfig()
logger = logging.getLogger(__name__)

# Default set of apps to scrobble from.
APP_WHITELIST = ['Spotify', 'Google Play Music', 'SoundCloud', 'Plex']
Expand Down Expand Up @@ -81,7 +79,7 @@ def poll(self):
# This could happen due to network hiccups, Chromecast
# restarting, race conditions, etc...
except (PyChromecastError, pylast.NetworkError):
traceback.print_exc()
logger.info('poll(%s) failed', self.cast_name, exc_info=True)
self.cast = None

def _poll(self):
Expand Down Expand Up @@ -193,8 +191,12 @@ def _log_now_playing(self, track_meta):
for scrobbler in self.scrobblers:
try:
scrobbler.update_now_playing(**track_meta)
except (pylast.NetworkError, pylast.MalformedResponseError):
logging.exception('update_now_playing failed for %s', scrobbler.name)
except (pylast.NetworkError, pylast.MalformedResponseError) as exc:
click.echo('Failed to update now playing for {}: {}'.format(
scrobbler.name, str(exc)))

logger.info('_log_now_playing(%s) failed', scrobbler.name,
exc_info=True)

# First time this track has been seen, so reset the estimated
# current time if we're using the spotify hack
Expand All @@ -211,8 +213,12 @@ def _log_scrobble(self, track_meta):
for scrobbler in self.scrobblers:
try:
scrobbler.scrobble(timestamp=int(time.time()), **track_meta)
except (pylast.NetworkError, pylast.MalformedResponseError):
logging.exception('scrobble failed for %s', scrobbler.name)
except (pylast.NetworkError, pylast.MalformedResponseError) as exc:
click.echo('Failed to scrobble to {}: {}'.format(
scrobbler.name, str(exc)))

logger.info('_log_scrobble(%s) failed', scrobbler.name,
exc_info=True)

self.last_scrobbled = track_meta

Expand Down Expand Up @@ -250,21 +256,17 @@ def config_wizard():

config['lastfm'] = {
key: click.prompt(key, type=str, hide_input=hidden)
for (key, hidden) in [
('user_name', False),
('password', True),
('api_key', False),
('api_secret', True)
]
for (key, hidden) in [('user_name', False),
('password', True),
('api_key', False),
('api_secret', True)]
}

if click.confirm('Set up Libre.fm account?'):
libre_conf = {
key: click.prompt(key, type=str, hide_input=hidden)
for (key, hidden) in [
('user_name', False),
('password', True)
]
for (key, hidden) in [('user_name', False),
('password', True)]
}

libre = pylast.LibreFMNetwork(
Expand Down Expand Up @@ -303,7 +305,8 @@ def config_wizard():
click.echo('\n\nDefault chromecast apps to scrobble from: %s' %
', '.join(APP_WHITELIST))

apps = click.prompt('Comma separated apps [blank for default]')
apps = click.prompt('Comma separated apps [blank for default]',
default='', show_default=False)
apps = [app.strip() for app in apps.split(',') if app.strip() != '']

if apps:
Expand Down Expand Up @@ -338,7 +341,14 @@ def connect_to_devices(config, device_names, available):
@click.command()
@click.option('--config', required=False, help='Config file location')
@click.option('--wizard', is_flag=True, help='Generate a lastcast config.')
def main(config, wizard):
@click.option('--verbose', is_flag=True, help='Enable debug logging.')
def main(config, wizard, verbose):
if verbose:
logger.setLevel('DEBUG')
else:
# pychromecast is by default pretty noisy about caught exceptions
logging.getLogger('pychromecast').setLevel('CRITICAL')

if wizard:
return config_wizard()

Expand Down Expand Up @@ -369,16 +379,16 @@ def main(config, wizard):
listeners, missing = connect_to_devices(config, device_names, available)

retry_missing = cast_config.get('retry_missing', False)
if cast_config.get('ignore_missing', False):
if cast_config.get('ignore_missing', False) and missing:
click.echo('Continuing without missing devices: %s' % ', '.join(missing))
missing = []

if missing and not retry_missing:
click.echo('Failed to connect to %s. Exiting' % ', '.join(missing))
click.echo('Available devices: %s' % ', '.join([
d.device.friendly_name for d in available
]))
sys.exit(1)
click.echo('Failed to connect to %s. Exiting' % ', '.join(missing))
click.echo('Available devices: %s' % ', '.join([
d.device.friendly_name for d in available
]))
sys.exit(1)

for i in itertools.count():
for listener in listeners:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='lastcast',
version='1.0.1',
version='1.1.0',
description='Scrobble music to last.fm from Chromecast.',
author='Erik Price',
url='https://github.com/erik/lastcast',
Expand Down

0 comments on commit 39001f2

Please sign in to comment.