2017-04-16 14:14:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-04-21 12:57:34 +02:00
|
|
|
import re
|
2018-06-13 13:22:52 +02:00
|
|
|
import uuid
|
2017-04-16 14:14:33 +02:00
|
|
|
|
2018-06-12 10:40:36 +02:00
|
|
|
from urllib.parse import urlparse, urlencode, quote
|
2017-04-16 14:14:33 +02:00
|
|
|
|
2017-12-30 14:15:51 +01:00
|
|
|
from toot import http, CLIENT_NAME, CLIENT_WEBSITE
|
2017-12-30 16:30:35 +01:00
|
|
|
from toot.exceptions import AuthenticationError
|
2019-01-24 11:18:28 +01:00
|
|
|
from toot.utils import str_bool
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
SCOPES = 'read write follow'
|
|
|
|
|
|
|
|
|
2017-04-26 11:49:21 +02:00
|
|
|
def _account_action(app, user, account, action):
|
2017-11-14 08:23:10 +01:00
|
|
|
url = '/api/v1/accounts/{}/{}'.format(account, action)
|
2017-04-26 11:49:21 +02:00
|
|
|
|
2017-12-30 14:15:51 +01:00
|
|
|
return http.post(app, user, url).json()
|
2017-04-26 11:49:21 +02:00
|
|
|
|
|
|
|
|
2019-01-21 17:25:20 +01:00
|
|
|
def _status_action(app, user, status_id, action):
|
|
|
|
url = '/api/v1/statuses/{}/{}'.format(status_id, action)
|
2019-01-02 12:24:38 +01:00
|
|
|
|
2019-01-21 17:25:20 +01:00
|
|
|
return http.post(app, user, url).json()
|
2019-01-02 12:24:38 +01:00
|
|
|
|
|
|
|
|
2018-12-25 02:20:30 +01:00
|
|
|
def create_app(domain, scheme='https'):
|
|
|
|
url = '{}://{}/api/v1/apps'.format(scheme, domain)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
data = {
|
2017-04-16 14:14:33 +02:00
|
|
|
'client_name': CLIENT_NAME,
|
|
|
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
'scopes': SCOPES,
|
|
|
|
'website': CLIENT_WEBSITE,
|
2017-12-30 16:30:35 +01:00
|
|
|
}
|
2017-04-16 14:14:33 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
return http.anon_post(url, data).json()
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def login(app, username, password):
|
|
|
|
url = app.base_url + '/oauth/token'
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
data = {
|
2017-04-16 14:14:33 +02:00
|
|
|
'grant_type': 'password',
|
|
|
|
'client_id': app.client_id,
|
|
|
|
'client_secret': app.client_secret,
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
'scope': SCOPES,
|
2017-12-30 16:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response = http.anon_post(url, data, allow_redirects=False)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
2017-04-18 16:16:24 +02:00
|
|
|
# If auth fails, it redirects to the login page
|
|
|
|
if response.is_redirect:
|
2017-04-18 16:40:26 +02:00
|
|
|
raise AuthenticationError()
|
2017-04-16 14:14:33 +02:00
|
|
|
|
2019-09-03 08:27:16 +02:00
|
|
|
return response.json()
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
2017-08-26 14:39:53 +02:00
|
|
|
def get_browser_login_url(app):
|
|
|
|
"""Returns the URL for manual log in via browser"""
|
|
|
|
return "{}/oauth/authorize/?{}".format(app.base_url, urlencode({
|
|
|
|
"response_type": "code",
|
|
|
|
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
|
2018-03-05 16:10:34 +01:00
|
|
|
"scope": SCOPES,
|
2017-08-26 14:39:53 +02:00
|
|
|
"client_id": app.client_id,
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
def request_access_token(app, authorization_code):
|
|
|
|
url = app.base_url + '/oauth/token'
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
data = {
|
2017-08-26 14:39:53 +02:00
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
'client_id': app.client_id,
|
|
|
|
'client_secret': app.client_secret,
|
|
|
|
'code': authorization_code,
|
|
|
|
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
|
2017-12-30 16:30:35 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 08:27:16 +02:00
|
|
|
return http.anon_post(url, data, allow_redirects=False).json()
|
2017-08-26 14:39:53 +02:00
|
|
|
|
|
|
|
|
2018-06-13 12:43:31 +02:00
|
|
|
def post_status(
|
|
|
|
app,
|
|
|
|
user,
|
|
|
|
status,
|
|
|
|
visibility='public',
|
|
|
|
media_ids=None,
|
|
|
|
sensitive=False,
|
|
|
|
spoiler_text=None,
|
2019-07-30 16:13:29 +02:00
|
|
|
in_reply_to_id=None,
|
|
|
|
language=None,
|
2018-06-13 12:43:31 +02:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Posts a new status.
|
|
|
|
https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#posting-a-new-status
|
|
|
|
"""
|
2018-06-13 13:22:52 +02:00
|
|
|
|
|
|
|
# Idempotency key assures the same status is not posted multiple times
|
|
|
|
# if the request is retried.
|
|
|
|
headers = {"Idempotency-Key": uuid.uuid4().hex}
|
|
|
|
|
2017-12-30 14:15:51 +01:00
|
|
|
return http.post(app, user, '/api/v1/statuses', {
|
2017-04-16 14:14:33 +02:00
|
|
|
'status': status,
|
|
|
|
'media_ids[]': media_ids,
|
|
|
|
'visibility': visibility,
|
2019-01-24 11:18:28 +01:00
|
|
|
'sensitive': str_bool(sensitive),
|
2018-06-07 10:04:50 +02:00
|
|
|
'spoiler_text': spoiler_text,
|
2018-06-13 12:43:31 +02:00
|
|
|
'in_reply_to_id': in_reply_to_id,
|
2019-07-30 16:13:29 +02:00
|
|
|
'language': language,
|
2018-06-13 13:22:52 +02:00
|
|
|
}, headers=headers).json()
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
2018-06-14 10:40:16 +02:00
|
|
|
def delete_status(app, user, status_id):
|
|
|
|
"""
|
|
|
|
Deletes a status with given ID.
|
|
|
|
https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#deleting-a-status
|
|
|
|
"""
|
|
|
|
return http.delete(app, user, '/api/v1/statuses/{}'.format(status_id))
|
|
|
|
|
|
|
|
|
2019-01-02 12:24:38 +01:00
|
|
|
def favourite(app, user, status_id):
|
|
|
|
return _status_action(app, user, status_id, 'favourite')
|
|
|
|
|
|
|
|
|
|
|
|
def unfavourite(app, user, status_id):
|
|
|
|
return _status_action(app, user, status_id, 'unfavourite')
|
|
|
|
|
|
|
|
|
|
|
|
def reblog(app, user, status_id):
|
|
|
|
return _status_action(app, user, status_id, 'reblog')
|
|
|
|
|
|
|
|
|
|
|
|
def unreblog(app, user, status_id):
|
|
|
|
return _status_action(app, user, status_id, 'unreblog')
|
|
|
|
|
|
|
|
|
|
|
|
def pin(app, user, status_id):
|
|
|
|
return _status_action(app, user, status_id, 'pin')
|
|
|
|
|
|
|
|
|
|
|
|
def unpin(app, user, status_id):
|
|
|
|
return _status_action(app, user, status_id, 'unpin')
|
|
|
|
|
2019-01-21 17:25:20 +01:00
|
|
|
|
2019-01-19 18:38:17 +01:00
|
|
|
def context(app, user, status_id):
|
2019-01-21 17:25:20 +01:00
|
|
|
url = '/api/v1/statuses/{}/context'.format(status_id)
|
2019-01-24 09:36:25 +01:00
|
|
|
|
|
|
|
return http.get(app, user, url).json()
|
|
|
|
|
|
|
|
|
|
|
|
def reblogged_by(app, user, status_id):
|
|
|
|
url = '/api/v1/statuses/{}/reblogged_by'.format(status_id)
|
2019-01-21 17:25:20 +01:00
|
|
|
|
|
|
|
return http.get(app, user, url).json()
|
|
|
|
|
2019-01-02 12:24:38 +01:00
|
|
|
|
2019-02-13 14:19:27 +01:00
|
|
|
def _get_next_path(headers):
|
2018-01-06 11:25:05 +01:00
|
|
|
"""Given timeline response headers, returns the path to the next batch"""
|
|
|
|
links = headers.get('Link', '')
|
|
|
|
matches = re.match('<([^>]+)>; rel="next"', links)
|
|
|
|
if matches:
|
|
|
|
parsed = urlparse(matches.group(1))
|
|
|
|
return "?".join([parsed.path, parsed.query])
|
2018-01-04 14:16:52 +01:00
|
|
|
|
|
|
|
|
2019-01-03 05:36:40 +01:00
|
|
|
def _timeline_generator(app, user, path, params=None):
|
2018-01-04 14:16:52 +01:00
|
|
|
while path:
|
2019-01-03 05:36:40 +01:00
|
|
|
response = http.get(app, user, path, params)
|
2017-04-21 12:57:34 +02:00
|
|
|
yield response.json()
|
2019-02-13 14:19:27 +01:00
|
|
|
path = _get_next_path(response.headers)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
2019-01-03 05:36:40 +01:00
|
|
|
def _anon_timeline_generator(instance, path, params=None):
|
2018-01-06 11:25:05 +01:00
|
|
|
while path:
|
|
|
|
url = "https://{}{}".format(instance, path)
|
2019-01-03 05:36:40 +01:00
|
|
|
response = http.anon_get(url, params)
|
2018-01-06 11:25:05 +01:00
|
|
|
yield response.json()
|
2019-02-13 14:19:27 +01:00
|
|
|
path = _get_next_path(response.headers)
|
2018-01-06 11:25:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def home_timeline_generator(app, user, limit=20):
|
|
|
|
path = '/api/v1/timelines/home?limit={}'.format(limit)
|
|
|
|
return _timeline_generator(app, user, path)
|
|
|
|
|
|
|
|
|
2019-01-03 05:36:40 +01:00
|
|
|
def public_timeline_generator(instance, local=False, limit=20):
|
|
|
|
path = '/api/v1/timelines/public'
|
2019-01-24 11:18:28 +01:00
|
|
|
params = {'local': str_bool(local), 'limit': limit}
|
2019-01-03 05:36:40 +01:00
|
|
|
return _anon_timeline_generator(instance, path, params)
|
|
|
|
|
|
|
|
|
2019-02-15 14:06:47 +01:00
|
|
|
def tag_timeline_generator(instance, hashtag, local=False, limit=20):
|
2019-01-03 05:36:40 +01:00
|
|
|
path = '/api/v1/timelines/tag/{}'.format(hashtag)
|
2019-01-24 11:18:28 +01:00
|
|
|
params = {'local': str_bool(local), 'limit': limit}
|
2019-02-15 14:06:47 +01:00
|
|
|
return _anon_timeline_generator(instance, path, params)
|
2018-01-06 11:25:05 +01:00
|
|
|
|
|
|
|
|
2019-02-13 14:15:47 +01:00
|
|
|
def timeline_list_generator(app, user, list_id, limit=20):
|
|
|
|
path = '/api/v1/timelines/list/{}'.format(list_id)
|
|
|
|
return _timeline_generator(app, user, path, {'limit': limit})
|
|
|
|
|
|
|
|
|
2017-04-16 14:14:33 +02:00
|
|
|
def upload_media(app, user, file):
|
2017-12-30 14:15:51 +01:00
|
|
|
return http.post(app, user, '/api/v1/media', files={
|
2017-04-16 14:14:33 +02:00
|
|
|
'file': file
|
2017-04-21 12:57:34 +02:00
|
|
|
}).json()
|
2017-04-16 15:07:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def search(app, user, query, resolve):
|
2019-09-22 13:04:58 +02:00
|
|
|
return http.get(app, user, '/api/v2/search', {
|
2017-04-16 15:07:27 +02:00
|
|
|
'q': query,
|
|
|
|
'resolve': resolve,
|
2017-04-21 12:57:34 +02:00
|
|
|
}).json()
|
2017-04-16 17:15:05 +02:00
|
|
|
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
def search_accounts(app, user, query):
|
2017-12-30 14:15:51 +01:00
|
|
|
return http.get(app, user, '/api/v1/accounts/search', {
|
2017-04-17 11:10:57 +02:00
|
|
|
'q': query,
|
2017-04-21 12:57:34 +02:00
|
|
|
}).json()
|
2017-04-17 11:10:57 +02:00
|
|
|
|
|
|
|
|
2017-04-16 17:15:05 +02:00
|
|
|
def follow(app, user, account):
|
2017-04-26 11:49:21 +02:00
|
|
|
return _account_action(app, user, account, 'follow')
|
2017-04-16 17:15:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
def unfollow(app, user, account):
|
2017-04-26 11:49:21 +02:00
|
|
|
return _account_action(app, user, account, 'unfollow')
|
2017-04-16 17:15:05 +02:00
|
|
|
|
2017-04-26 11:49:21 +02:00
|
|
|
|
|
|
|
def mute(app, user, account):
|
|
|
|
return _account_action(app, user, account, 'mute')
|
|
|
|
|
|
|
|
|
|
|
|
def unmute(app, user, account):
|
|
|
|
return _account_action(app, user, account, 'unmute')
|
|
|
|
|
|
|
|
|
|
|
|
def block(app, user, account):
|
|
|
|
return _account_action(app, user, account, 'block')
|
|
|
|
|
|
|
|
|
|
|
|
def unblock(app, user, account):
|
|
|
|
return _account_action(app, user, account, 'unblock')
|
2017-04-16 17:52:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
def verify_credentials(app, user):
|
2017-12-30 14:15:51 +01:00
|
|
|
return http.get(app, user, '/api/v1/accounts/verify_credentials').json()
|
2017-04-21 12:57:34 +02:00
|
|
|
|
|
|
|
|
2019-01-19 18:38:17 +01:00
|
|
|
def single_status(app, user, status_id):
|
2019-01-21 17:25:20 +01:00
|
|
|
url = '/api/v1/statuses/{}'.format(status_id)
|
|
|
|
|
|
|
|
return http.get(app, user, url).json()
|
|
|
|
|
2019-01-19 18:38:17 +01:00
|
|
|
|
2017-04-21 12:57:34 +02:00
|
|
|
def get_notifications(app, user):
|
2017-12-30 14:15:51 +01:00
|
|
|
return http.get(app, user, '/api/v1/notifications').json()
|
2017-12-29 14:26:40 +01:00
|
|
|
|
|
|
|
|
2019-02-17 14:18:51 +01:00
|
|
|
def clear_notifications(app, user):
|
|
|
|
http.post(app, user, '/api/v1/notifications/clear')
|
|
|
|
|
|
|
|
|
2018-12-30 09:53:12 +01:00
|
|
|
def get_instance(domain, scheme="https"):
|
|
|
|
url = "{}://{}/api/v1/instance".format(scheme, domain)
|
2017-12-30 16:30:35 +01:00
|
|
|
return http.anon_get(url).json()
|