fediplug/fediplay/mastodon.py

92 lines
3 KiB
Python
Raw Normal View History

2018-03-17 23:06:43 +01:00
'''Mastodon interface.'''
from os import umask
2018-04-03 02:09:25 +02:00
import click
2018-03-17 23:06:43 +01:00
from lxml.etree import HTML # pylint: disable=no-name-in-module
import mastodon
from youtube_dl.utils import DownloadError
2018-07-23 02:13:10 +02:00
import fediplay.keyring as keyring
2018-03-17 23:06:43 +01:00
from fediplay.queue import Queue
Mastodon = mastodon.Mastodon
2018-07-20 17:25:41 +02:00
def api_base_url(instance):
'''Create an API base url from an instance name.'''
return 'https://' + instance
2018-03-17 23:06:43 +01:00
class StreamListener(mastodon.StreamListener):
'''Listens to a Mastodon timeline and adds links the given Queue.'''
def __init__(self, queue):
self.queue = queue
def on_update(self, status):
tags = extract_tags(status)
if 'fediplay' in tags:
links = extract_links(status)
for link in links:
try:
2018-04-03 02:09:25 +02:00
click.echo('==> Trying {}'.format(link))
2018-03-17 23:06:43 +01:00
self.queue.add(link)
return
except DownloadError:
pass
2018-07-23 02:13:10 +02:00
def register(instance):
2018-03-17 23:06:43 +01:00
'''Register fediplay to a Mastodon server and save the client credentials.'''
2018-07-23 02:13:10 +02:00
client_id, client_secret = Mastodon.create_app('fediplay', scopes=['read'], api_base_url=api_base_url(instance))
keyring.set_credential(instance, keyring.CREDENTIAL_CLIENT_ID, client_id)
keyring.set_credential(instance, keyring.CREDENTIAL_CLIENT_SECRET, client_secret)
2018-07-20 17:25:41 +02:00
2018-07-23 02:13:10 +02:00
def build_client(instance, client_id, client_secret, access_token=None):
2018-07-20 17:25:41 +02:00
'''Builds a Mastodon client.'''
2018-07-23 02:13:10 +02:00
return Mastodon(api_base_url=api_base_url(instance),
client_id=client_id, client_secret=client_secret, access_token=access_token)
2018-07-20 17:25:41 +02:00
2018-07-23 02:13:10 +02:00
def get_auth_request_url(instance, client_id, client_secret):
2018-07-20 17:25:41 +02:00
'''Gets an authorization request URL from a Mastodon instance.'''
2018-07-23 02:13:10 +02:00
return build_client(instance, client_id, client_secret).auth_request_url(scopes=['read'])
2018-03-17 23:06:43 +01:00
2018-07-23 02:13:10 +02:00
def login(instance, client_id, client_secret, grant_code):
2018-03-17 23:06:43 +01:00
'''Log in to a Mastodon server and save the user credentials.'''
2018-07-23 02:13:10 +02:00
client = build_client(instance, client_id, client_secret)
access_token = client.log_in(code=grant_code, scopes=['read'])
keyring.set_credential(instance, keyring.CREDENTIAL_ACCESS_TOKEN, access_token)
2018-03-17 23:06:43 +01:00
2018-07-23 02:13:10 +02:00
def stream(instance, client_id, client_secret, access_token, cache_dir='.'):
2018-03-17 23:06:43 +01:00
'''Stream statuses and add them to a queue.'''
2018-07-23 02:13:10 +02:00
client = build_client(instance, client_id, client_secret, access_token)
2018-07-20 18:06:11 +02:00
listener = StreamListener(Queue(cache_dir))
2018-07-20 17:57:00 +02:00
click.echo('==> Streaming from {}'.format(instance))
2018-03-17 23:06:43 +01:00
client.stream_user(listener)
def extract_tags(toot):
'''Extract tags from a toot.'''
return [tag['name'] for tag in toot['tags']]
def link_is_internal(link):
'''Determines if a link is internal to the Mastodon instance.'''
classes = link.attrib.get('class', '').split(' ')
if classes:
return 'mention' in classes
return False
def extract_links(toot):
'''Extract all external links from a toot.'''
html = HTML(toot['content'])
all_links = html.cssselect('a')
return [link.attrib['href'] for link in all_links if not link_is_internal(link)]