mirror of
https://github.com/nova-r/fediplug.git
synced 2025-02-02 06:07:27 +01:00
refactor, add debug prints
This commit is contained in:
parent
6fd3e94a4b
commit
a70a3b17d4
2 changed files with 26 additions and 12 deletions
|
@ -1,5 +1,7 @@
|
||||||
'''Mastodon interface.'''
|
'''Mastodon interface.'''
|
||||||
|
|
||||||
|
LISTEN_TO_HASHTAG = 'fediplay'
|
||||||
|
|
||||||
from os import umask
|
from os import umask
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
@ -13,8 +15,6 @@ from fediplay.queue import Queue
|
||||||
|
|
||||||
Mastodon = mastodon.Mastodon
|
Mastodon = mastodon.Mastodon
|
||||||
|
|
||||||
LISTEN_TO_HASHTAG = 'fediplay'
|
|
||||||
|
|
||||||
|
|
||||||
def api_base_url(instance):
|
def api_base_url(instance):
|
||||||
'''Create an API base url from an instance name.'''
|
'''Create an API base url from an instance name.'''
|
||||||
|
@ -30,21 +30,26 @@ class StreamListener(mastodon.StreamListener):
|
||||||
self.users = users
|
self.users = users
|
||||||
|
|
||||||
if options['debug']:
|
if options['debug']:
|
||||||
print('listener initialized with users={}'.format(self.users))
|
print('listener initialized with users={!r}'.format(self.users))
|
||||||
|
|
||||||
def on_update(self, status):
|
def on_update(self, status):
|
||||||
if options['debug']:
|
if options['debug']:
|
||||||
print('status: {}'.format(repr(status)))
|
print('incoming status: acct={!r}'.format(status.account.acct))
|
||||||
print('incoming toot: username={}'.format(status.account.acct))
|
|
||||||
|
|
||||||
if self.users and normalize_username(status.account.acct, self.instance) not in self.users:
|
if self.users and normalize_username(status.account.acct, self.instance) not in self.users:
|
||||||
if options['debug']:
|
if options['debug']:
|
||||||
print('skipping toot due to username filtering')
|
print('skipping status due to username filtering')
|
||||||
return
|
return
|
||||||
|
|
||||||
tags = extract_tags(status)
|
tags = extract_tags(status)
|
||||||
|
if options['debug']:
|
||||||
|
print('expecting: {!r}, extracted tags: {!r}'.format(LISTEN_TO_HASHTAG, tags))
|
||||||
|
|
||||||
if LISTEN_TO_HASHTAG in tags:
|
if LISTEN_TO_HASHTAG in tags:
|
||||||
links = extract_links(status)
|
links = extract_links(status)
|
||||||
|
if options['debug']:
|
||||||
|
print('links: {!r}'.format(links))
|
||||||
|
|
||||||
for link in links:
|
for link in links:
|
||||||
try:
|
try:
|
||||||
click.echo('==> Trying {}'.format(link))
|
click.echo('==> Trying {}'.format(link))
|
||||||
|
@ -84,9 +89,16 @@ def stream(instance, users, client_id, client_secret, access_token, cache_dir='.
|
||||||
client = build_client(instance, client_id, client_secret, access_token)
|
client = build_client(instance, client_id, client_secret, access_token)
|
||||||
users = [normalize_username(user, instance) for user in users]
|
users = [normalize_username(user, instance) for user in users]
|
||||||
listener = StreamListener(Queue(cache_dir), instance, users)
|
listener = StreamListener(Queue(cache_dir), instance, users)
|
||||||
|
|
||||||
|
existing_statuses = client.timeline_hashtag(LISTEN_TO_HASHTAG, limit=1)
|
||||||
|
|
||||||
|
if options['debug']:
|
||||||
|
print('existing_statuses: {!r}'.format(existing_statuses))
|
||||||
|
|
||||||
|
for status in existing_statuses:
|
||||||
|
listener.on_update(status)
|
||||||
|
|
||||||
click.echo('==> Streaming from {}'.format(instance))
|
click.echo('==> Streaming from {}'.format(instance))
|
||||||
for t in client.timeline_hashtag(LISTEN_TO_HASHTAG, limit=1):
|
|
||||||
listener.on_update(t)
|
|
||||||
client.stream_user(listener)
|
client.stream_user(listener)
|
||||||
|
|
||||||
def extract_tags(toot):
|
def extract_tags(toot):
|
||||||
|
@ -98,7 +110,7 @@ def normalize_username(user, instance):
|
||||||
user = user.lstrip('@')
|
user = user.lstrip('@')
|
||||||
parts = user.split('@')
|
parts = user.split('@')
|
||||||
if options['debug']:
|
if options['debug']:
|
||||||
print('parts: {}'.format(repr(parts)))
|
print('parts: {!r}'.format(parts))
|
||||||
|
|
||||||
if len(parts) == 1 or parts[1] == instance:
|
if len(parts) == 1 or parts[1] == instance:
|
||||||
return parts[0]
|
return parts[0]
|
||||||
|
@ -109,6 +121,10 @@ def link_is_internal(link):
|
||||||
'''Determines if a link is internal to the Mastodon instance.'''
|
'''Determines if a link is internal to the Mastodon instance.'''
|
||||||
|
|
||||||
classes = link.attrib.get('class', '').split(' ')
|
classes = link.attrib.get('class', '').split(' ')
|
||||||
|
|
||||||
|
if options['debug']:
|
||||||
|
print('href: {!r}, classes: {!r}'.format(link.attrib['href'], classes))
|
||||||
|
|
||||||
if classes:
|
if classes:
|
||||||
return 'mention' in classes
|
return 'mention' in classes
|
||||||
|
|
||||||
|
|
|
@ -64,9 +64,7 @@ class Getter(object):
|
||||||
|
|
||||||
def _progress_hook(self, progress):
|
def _progress_hook(self, progress):
|
||||||
if options['debug']:
|
if options['debug']:
|
||||||
print('progress hook: status {}, filename {}'.format(
|
print('progress hook: status {!r}, filename {!r}'.format(progress['status'], progress['filename']))
|
||||||
repr(progress['status']), repr(progress['filename'])
|
|
||||||
))
|
|
||||||
|
|
||||||
if (progress['status'] in ('downloading', 'finished') and
|
if (progress['status'] in ('downloading', 'finished') and
|
||||||
progress['filename'] not in self.filenames):
|
progress['filename'] not in self.filenames):
|
||||||
|
|
Loading…
Reference in a new issue