Skip to content

Commit

Permalink
debug support
Browse files Browse the repository at this point in the history
  • Loading branch information
mattieb committed Oct 22, 2018
1 parent 4f1ecbd commit 1488ceb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
7 changes: 6 additions & 1 deletion fediplay/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'''Entry point for command-line interface.'''

options = {'debug': False}

import os
path = os.path
import sys
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 11 additions & 2 deletions fediplay/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mastodon
from youtube_dl.utils import DownloadError

from fediplay.cli import options
import fediplay.keyring as keyring
from fediplay.queue import Queue

Expand All @@ -25,9 +26,18 @@ def __init__(self, queue, users):
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)
Expand Down Expand Up @@ -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)
Expand All @@ -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.'''

Expand Down
9 changes: 8 additions & 1 deletion fediplay/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import click
from youtube_dl import YoutubeDL, utils

from fediplay.cli import options
import fediplay.env as env


Expand Down Expand Up @@ -62,7 +63,13 @@ def __init__(self, cache_dir):
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):
Expand Down

0 comments on commit 1488ceb

Please sign in to comment.