a mastodon api prototyping area
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 lines
3.0 KiB

#!/usr/bin/python3
from mastodon import Mastodon, StreamListener
from pprint import pprint
# This is a test environment of Mastodon.py :)
# https://mastodonpy.readthedocs.io/en/stable/
# https://github.com/halcy/Mastodon.py
# Install Mastodon.py through pip:
# $ pip3 install Mastodon.py
# ---
# Register app - only once!
# Mastodon.create_app(
# 'reader',
# api_base_url = 'https://post.lurk.org',
# to_file = 'api_clientcred.secret',
# scopes = 'read'
# )
# --- only api_clientcred.secret is needed ---
# Log in - either every time, or use persisted (how does persisted work ... ?)
mastodon = Mastodon(
client_id = 'api_clientcred.secret',
api_base_url = 'https://post.lurk.org'
)
# Bits of information about my instance.
instance = mastodon.instance()
# pprint(instance)
instance_peers = mastodon.instance_peers()
# pprint(instance_peers)
# print(len(instance_peers))
instance_activity = mastodon.instance_activity()
# pprint(instance_activity)
timeline_local = mastodon.timeline_local(max_id=None, since_id=0, limit=100)
# All endpoints that are paginated have three parameters: since_id, max_id and limit. since_id allows you to specify the smallest id you want in the returned data. max_id, similarly, allows you to specify the largest. By specifying either one (generally, only one, not both) of them you can go through pages forwards and backwards.
# https://mastodonpy.readthedocs.io/en/stable/#a-note-about-pagination
# pprint(timeline_local)
# print('\nLength of the returned timeline:', len(timeline_local))
# 40 seems to be the max for post.lurk.org
# for toot in timeline_local:
# print(toot.content)
timeline_public = mastodon.timeline_public(max_id=None, since_id=None, limit=100, only_media=False)
# pprint(timeline_public)
# print('\nLength of the returned timeline:', len(timeline_public))
# 40 seems to be the max for post.lurk.org
next_page = mastodon.fetch_next(timeline_public)
# Fetches the next page of results of a paginated request.
# pprint(next_page)
# print('\nLength of the returned timeline:', len(next_page))
# --- api_usercred.secret is needed ---
# This is only needed once, to create the api_usercred.secret
# mastodon.log_in(
# 'mail@manettaberends.nl',
# 'password',
# to_file = 'api_usercred.secret'
# )
# Log in with api_usercred.secret
mastodon = Mastodon(
access_token = 'api_usercred.secret',
api_base_url = 'https://post.lurk.org'
)
# Toot from python
# mastodon.toot('Tooting from python using #mastodonpy !')
# Account details
account = mastodon.account_verify_credentials()
# pprint(account)
search = mastodon.search("Tooting")
# You can only search when you're logged in
# What is the range of this search?
# Is it limited to all the instances that crossed the local instance?
# It does not really pick up statutes ... Also not recent ones.
# pprint(search)
# --- streaming.api ---
class listener(StreamListener):
def on_update(self, status):
print(status.content)
# mastodon.stream_public(listener(), run_async=False, timeout=300, reconnect_async=False, reconnect_async_wait_sec=5)