Skip to content

Commit

Permalink
fix username filtering, cache play (fixes mattieb#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattieb committed Oct 22, 2018
1 parent 1488ceb commit 4f1521f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions fediplay/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ def api_base_url(instance):
class StreamListener(mastodon.StreamListener):
'''Listens to a Mastodon timeline and adds links the given Queue.'''

def __init__(self, queue, users):
def __init__(self, queue, instance, users):
self.queue = queue
self.instance = instance
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))
print('status: {}'.format(repr(status)))
print('incoming toot: username={}'.format(status.account.acct))

if self.users and status.account.username not in self.users:
if self.users and normalize_username(status.account.acct, self.instance) not in self.users:
if options['debug']:
print('skipping toot due to username filtering')
return
Expand Down Expand Up @@ -79,7 +81,7 @@ def stream(instance, users, client_id, client_secret, access_token, cache_dir='.

client = build_client(instance, client_id, client_secret, access_token)
users = [normalize_username(user, instance) for user in users]
listener = StreamListener(Queue(cache_dir), users)
listener = StreamListener(Queue(cache_dir), instance, users)
click.echo('==> Streaming from {}'.format(instance))
client.stream_user(listener)

Expand All @@ -89,11 +91,15 @@ def extract_tags(toot):
return [tag['name'] for tag in toot['tags']]

def normalize_username(user, instance):
# If user was specified with an @ at beggining, remove it
user = user.lstrip('@')
tmp = user.split('@')
# remove instance if it is our own instance
return user if (len(tmp) == 1 or tmp[1] != instance) else tmp[0]
parts = user.split('@')
if options['debug']:
print('parts: {}'.format(repr(parts)))

if len(parts) == 1 or parts[1] == instance:
return parts[0]
else:
return user

def link_is_internal(link):
'''Determines if a link is internal to the Mastodon instance.'''
Expand Down
4 changes: 2 additions & 2 deletions fediplay/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def __init__(self, cache_dir):
def _progress_hook(self, progress):
if options['debug']:
print('progress hook: status {}, filename {}'.format(
progress['status'], progress['filename']
repr(progress['status']), repr(progress['filename'])
))

if (progress['status'] == 'downloading' and
if (progress['status'] in ('downloading', 'finished') and
progress['filename'] not in self.filenames):
self.filenames.append(progress['filename'])

Expand Down

0 comments on commit 4f1521f

Please sign in to comment.