diff --git a/fediplay/cli.py b/fediplay/cli.py index 6f889c3..3a0d238 100644 --- a/fediplay/cli.py +++ b/fediplay/cli.py @@ -1,5 +1,7 @@ '''Entry point for command-line interface.''' +options = {'debug': False} + import os path = os.path import sys @@ -49,9 +51,12 @@ def get_client_credentials(instance): ) @click.group() -def cli(): +@click.option('-d', '--debug', is_flag=True, help='Print debug messages.') +def cli(debug): '''A program to play music your friends post on Mastodon.''' + options['debug'] = debug + ensure_dirs() @cli.command() diff --git a/fediplay/mastodon.py b/fediplay/mastodon.py index 09eb5d6..28e2db9 100644 --- a/fediplay/mastodon.py +++ b/fediplay/mastodon.py @@ -7,6 +7,7 @@ from lxml.etree import HTML # pylint: disable=no-name-in-module import mastodon from youtube_dl.utils import DownloadError +from fediplay.cli import options import fediplay.keyring as keyring from fediplay.queue import Queue @@ -25,9 +26,18 @@ class StreamListener(mastodon.StreamListener): self.queue = queue self.users = users + if options['debug']: + print('listener initialized with users={}'.format(self.users)) + def on_update(self, status): + if options['debug']: + print('incoming toot: username={}'.format(status.account.username)) + if self.users and status.account.username not in self.users: + if options['debug']: + print('skipping toot due to username filtering') return + tags = extract_tags(status) if 'fediplay' in tags: links = extract_links(status) @@ -68,7 +78,7 @@ def stream(instance, users, client_id, client_secret, access_token, cache_dir='. '''Stream statuses and add them to a queue.''' client = build_client(instance, client_id, client_secret, access_token) - users = [normalize_username(u, instance) for u in users] + users = [normalize_username(user, instance) for user in users] listener = StreamListener(Queue(cache_dir), users) click.echo('==> Streaming from {}'.format(instance)) client.stream_user(listener) @@ -85,7 +95,6 @@ def normalize_username(user, instance): # remove instance if it is our own instance return user if (len(tmp) == 1 or tmp[1] != instance) else tmp[0] - def link_is_internal(link): '''Determines if a link is internal to the Mastodon instance.''' diff --git a/fediplay/queue.py b/fediplay/queue.py index ffc5d06..6b06805 100644 --- a/fediplay/queue.py +++ b/fediplay/queue.py @@ -8,6 +8,7 @@ from threading import Thread, Lock import click from youtube_dl import YoutubeDL, utils +from fediplay.cli import options import fediplay.env as env @@ -62,7 +63,13 @@ class Getter(object): self.cache_dir = cache_dir def _progress_hook(self, progress): - if progress['status'] == 'downloading' and progress['filename'] not in self.filenames: + if options['debug']: + print('progress hook: status {}, filename {}'.format( + progress['status'], progress['filename'] + )) + + if (progress['status'] == 'downloading' and + progress['filename'] not in self.filenames): self.filenames.append(progress['filename']) def get(self, url):