2017-04-16 12:14:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-04-21 10:57:34 +00:00
|
|
|
import re
|
2017-04-16 12:14:33 +00:00
|
|
|
|
2017-12-29 13:26:40 +00:00
|
|
|
from urllib.parse import urlparse, urlencode
|
2017-04-16 12:14:33 +00:00
|
|
|
|
2017-12-30 13:15:51 +00:00
|
|
|
from toot import http, CLIENT_NAME, CLIENT_WEBSITE
|
2017-12-30 15:30:35 +00:00
|
|
|
from toot.exceptions import AuthenticationError
|
2017-04-16 12:14:33 +00:00
|
|
|
|
|
|
|
SCOPES = 'read write follow'
|
|
|
|
|
|
|
|
|
2017-04-26 09:49:21 +00:00
|
|
|
def _account_action(app, user, account, action):
|
2017-11-14 07:23:10 +00:00
|
|
|
url = '/api/v1/accounts/{}/{}'.format(account, action)
|
2017-04-26 09:49:21 +00:00
|
|
|
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.post(app, user, url).json()
|
2017-04-26 09:49:21 +00:00
|
|
|
|
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
def create_app(domain):
|
2017-12-30 16:54:22 +00:00
|
|
|
url = 'https://{}/api/v1/apps'.format(domain)
|
2017-04-16 12:14:33 +00:00
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
data = {
|
2017-04-16 12:14:33 +00:00
|
|
|
'client_name': CLIENT_NAME,
|
|
|
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
'scopes': SCOPES,
|
|
|
|
'website': CLIENT_WEBSITE,
|
2017-12-30 15:30:35 +00:00
|
|
|
}
|
2017-04-16 12:14:33 +00:00
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
return http.anon_post(url, data).json()
|
2017-04-16 12:14:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login(app, username, password):
|
|
|
|
url = app.base_url + '/oauth/token'
|
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
data = {
|
2017-04-16 12:14:33 +00:00
|
|
|
'grant_type': 'password',
|
|
|
|
'client_id': app.client_id,
|
|
|
|
'client_secret': app.client_secret,
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
'scope': SCOPES,
|
2017-12-30 15:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response = http.anon_post(url, data, allow_redirects=False)
|
2017-04-16 12:14:33 +00:00
|
|
|
|
2017-04-18 14:16:24 +00:00
|
|
|
# If auth fails, it redirects to the login page
|
|
|
|
if response.is_redirect:
|
2017-04-18 14:40:26 +00:00
|
|
|
raise AuthenticationError()
|
2017-04-16 12:14:33 +00:00
|
|
|
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.process_response(response).json()
|
2017-04-16 12:14:33 +00:00
|
|
|
|
|
|
|
|
2017-08-26 12:39:53 +00: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",
|
|
|
|
"scope": "read write follow",
|
|
|
|
"client_id": app.client_id,
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
def request_access_token(app, authorization_code):
|
|
|
|
url = app.base_url + '/oauth/token'
|
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
data = {
|
2017-08-26 12:39:53 +00: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 15:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response = http.anon_post(url, data, allow_redirects=False)
|
2017-08-26 12:39:53 +00:00
|
|
|
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.process_response(response).json()
|
2017-08-26 12:39:53 +00:00
|
|
|
|
|
|
|
|
2017-04-16 12:14:33 +00:00
|
|
|
def post_status(app, user, status, visibility='public', media_ids=None):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.post(app, user, '/api/v1/statuses', {
|
2017-04-16 12:14:33 +00:00
|
|
|
'status': status,
|
|
|
|
'media_ids[]': media_ids,
|
|
|
|
'visibility': visibility,
|
2017-04-21 10:57:34 +00:00
|
|
|
}).json()
|
2017-04-16 12:14:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def timeline_home(app, user):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.get(app, user, '/api/v1/timelines/home').json()
|
2017-04-21 10:57:34 +00:00
|
|
|
|
|
|
|
|
2018-01-06 10:25:05 +00:00
|
|
|
def get_next_path(headers):
|
|
|
|
"""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 13:16:52 +00:00
|
|
|
|
|
|
|
|
2018-01-06 10:25:05 +00:00
|
|
|
def _timeline_generator(app, user, path, limit=20):
|
2018-01-04 13:16:52 +00:00
|
|
|
while path:
|
|
|
|
response = http.get(app, user, path)
|
2017-04-21 10:57:34 +00:00
|
|
|
yield response.json()
|
2018-01-04 13:16:52 +00:00
|
|
|
path = get_next_path(response.headers)
|
2017-04-16 12:14:33 +00:00
|
|
|
|
|
|
|
|
2018-01-06 10:25:05 +00:00
|
|
|
def _anon_timeline_generator(instance, path, limit=20):
|
|
|
|
while path:
|
|
|
|
url = "https://{}{}".format(instance, path)
|
|
|
|
response = http.anon_get(url, path)
|
|
|
|
yield response.json()
|
|
|
|
path = get_next_path(response.headers)
|
|
|
|
|
|
|
|
|
|
|
|
def home_timeline_generator(app, user, limit=20):
|
|
|
|
path = '/api/v1/timelines/home?limit={}'.format(limit)
|
|
|
|
return _timeline_generator(app, user, path)
|
|
|
|
|
|
|
|
|
|
|
|
def public_timeline_generator(instance, limit=20):
|
|
|
|
path = '/api/v1/timelines/public?limit={}'.format(limit)
|
|
|
|
return _anon_timeline_generator(instance, path)
|
|
|
|
|
|
|
|
|
2017-04-16 12:14:33 +00:00
|
|
|
def upload_media(app, user, file):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.post(app, user, '/api/v1/media', files={
|
2017-04-16 12:14:33 +00:00
|
|
|
'file': file
|
2017-04-21 10:57:34 +00:00
|
|
|
}).json()
|
2017-04-16 13:07:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def search(app, user, query, resolve):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.get(app, user, '/api/v1/search', {
|
2017-04-16 13:07:27 +00:00
|
|
|
'q': query,
|
|
|
|
'resolve': resolve,
|
2017-04-21 10:57:34 +00:00
|
|
|
}).json()
|
2017-04-16 15:15:05 +00:00
|
|
|
|
|
|
|
|
2017-04-17 09:10:57 +00:00
|
|
|
def search_accounts(app, user, query):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.get(app, user, '/api/v1/accounts/search', {
|
2017-04-17 09:10:57 +00:00
|
|
|
'q': query,
|
2017-04-21 10:57:34 +00:00
|
|
|
}).json()
|
2017-04-17 09:10:57 +00:00
|
|
|
|
|
|
|
|
2017-04-16 15:15:05 +00:00
|
|
|
def follow(app, user, account):
|
2017-04-26 09:49:21 +00:00
|
|
|
return _account_action(app, user, account, 'follow')
|
2017-04-16 15:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unfollow(app, user, account):
|
2017-04-26 09:49:21 +00:00
|
|
|
return _account_action(app, user, account, 'unfollow')
|
2017-04-16 15:15:05 +00:00
|
|
|
|
2017-04-26 09:49:21 +00: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 15:52:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def verify_credentials(app, user):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.get(app, user, '/api/v1/accounts/verify_credentials').json()
|
2017-04-21 10:57:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_notifications(app, user):
|
2017-12-30 13:15:51 +00:00
|
|
|
return http.get(app, user, '/api/v1/notifications').json()
|
2017-12-29 13:26:40 +00:00
|
|
|
|
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
def get_instance(domain):
|
2017-12-29 13:26:40 +00:00
|
|
|
url = "http://{}/api/v1/instance".format(domain)
|
2017-12-30 15:30:35 +00:00
|
|
|
return http.anon_get(url).json()
|