2017-04-16 14:14:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import logging
|
2017-04-21 12:57:34 +02:00
|
|
|
import re
|
2017-04-16 14:14:33 +02:00
|
|
|
import requests
|
|
|
|
|
2017-12-12 11:36:08 +01:00
|
|
|
from urllib.parse import urlparse, urlencode
|
2017-04-16 14:14:33 +02:00
|
|
|
from requests import Request, Session
|
|
|
|
|
2017-04-19 14:47:30 +02:00
|
|
|
from toot import CLIENT_NAME, CLIENT_WEBSITE
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
SCOPES = 'read write follow'
|
|
|
|
|
|
|
|
logger = logging.getLogger('toot')
|
|
|
|
|
|
|
|
|
2017-04-16 17:15:05 +02:00
|
|
|
class ApiError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NotFoundError(ApiError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-04-18 16:16:24 +02:00
|
|
|
class AuthenticationError(ApiError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
def _log_request(request):
|
2017-04-16 14:14:33 +02:00
|
|
|
logger.debug(">>> \033[32m{} {}\033[0m".format(request.method, request.url))
|
|
|
|
logger.debug(">>> HEADERS: \033[33m{}\033[0m".format(request.headers))
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
if request.data:
|
|
|
|
logger.debug(">>> DATA: \033[33m{}\033[0m".format(request.data))
|
|
|
|
|
|
|
|
if request.files:
|
|
|
|
logger.debug(">>> FILES: \033[33m{}\033[0m".format(request.files))
|
|
|
|
|
|
|
|
if request.params:
|
|
|
|
logger.debug(">>> PARAMS: \033[33m{}\033[0m".format(request.params))
|
|
|
|
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
def _log_response(response):
|
2017-04-16 17:15:05 +02:00
|
|
|
if response.ok:
|
|
|
|
logger.debug("<<< \033[32m{}\033[0m".format(response))
|
|
|
|
logger.debug("<<< \033[33m{}\033[0m".format(response.json()))
|
|
|
|
else:
|
|
|
|
logger.debug("<<< \033[31m{}\033[0m".format(response))
|
|
|
|
logger.debug("<<< \033[31m{}\033[0m".format(response.content))
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
def _process_response(response):
|
|
|
|
_log_response(response)
|
|
|
|
|
|
|
|
if not response.ok:
|
2017-08-26 14:39:53 +02:00
|
|
|
error = "Unknown error"
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
try:
|
2017-08-26 14:39:53 +02:00
|
|
|
data = response.json()
|
|
|
|
if "error_description" in data:
|
|
|
|
error = data['error_description']
|
|
|
|
elif "error" in data:
|
|
|
|
error = data['error']
|
2017-04-17 11:10:57 +02:00
|
|
|
except:
|
2017-08-26 14:39:53 +02:00
|
|
|
pass
|
2017-04-17 11:10:57 +02:00
|
|
|
|
|
|
|
if response.status_code == 404:
|
|
|
|
raise NotFoundError(error)
|
|
|
|
|
|
|
|
raise ApiError(error)
|
|
|
|
|
2017-04-21 12:57:34 +02:00
|
|
|
return response
|
2017-04-17 11:10:57 +02:00
|
|
|
|
|
|
|
|
2017-04-16 14:14:33 +02:00
|
|
|
def _get(app, user, url, params=None):
|
|
|
|
url = app.base_url + url
|
|
|
|
headers = {"Authorization": "Bearer " + user.access_token}
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
_log_request(Request('GET', url, headers, params=params))
|
|
|
|
|
2017-04-16 14:14:33 +02:00
|
|
|
response = requests.get(url, params, headers=headers)
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
return _process_response(response)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _post(app, user, url, data=None, files=None):
|
|
|
|
url = app.base_url + url
|
|
|
|
headers = {"Authorization": "Bearer " + user.access_token}
|
|
|
|
|
|
|
|
session = Session()
|
|
|
|
request = Request('POST', url, headers, files, data)
|
|
|
|
prepared_request = request.prepare()
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
_log_request(request)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
response = session.send(prepared_request)
|
|
|
|
|
2017-04-17 11:10:57 +02:00
|
|
|
return _process_response(response)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return _post(app, user, url).json()
|
|
|
|
|
|
|
|
|
2017-04-18 16:16:24 +02:00
|
|
|
def create_app(instance):
|
|
|
|
base_url = 'https://' + instance
|
2017-04-16 14:14:33 +02:00
|
|
|
url = base_url + '/api/v1/apps'
|
|
|
|
|
|
|
|
response = requests.post(url, {
|
|
|
|
'client_name': CLIENT_NAME,
|
|
|
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
'scopes': SCOPES,
|
|
|
|
'website': CLIENT_WEBSITE,
|
|
|
|
})
|
|
|
|
|
2017-04-21 12:57:34 +02:00
|
|
|
return _process_response(response).json()
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def login(app, username, password):
|
|
|
|
url = app.base_url + '/oauth/token'
|
|
|
|
|
|
|
|
response = requests.post(url, {
|
|
|
|
'grant_type': 'password',
|
|
|
|
'client_id': app.client_id,
|
|
|
|
'client_secret': app.client_secret,
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
'scope': SCOPES,
|
2017-04-18 16:16:24 +02:00
|
|
|
}, 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
|
|
|
|
2017-04-21 12:57:34 +02:00
|
|
|
return _process_response(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",
|
|
|
|
"scope": "read write follow",
|
|
|
|
"client_id": app.client_id,
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
def request_access_token(app, authorization_code):
|
|
|
|
url = app.base_url + '/oauth/token'
|
|
|
|
|
|
|
|
response = requests.post(url, {
|
|
|
|
'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',
|
|
|
|
}, allow_redirects=False)
|
|
|
|
|
|
|
|
return _process_response(response).json()
|
|
|
|
|
|
|
|
|
2017-04-16 14:14:33 +02:00
|
|
|
def post_status(app, user, status, visibility='public', media_ids=None):
|
|
|
|
return _post(app, user, '/api/v1/statuses', {
|
|
|
|
'status': status,
|
|
|
|
'media_ids[]': media_ids,
|
|
|
|
'visibility': visibility,
|
2017-04-21 12:57:34 +02:00
|
|
|
}).json()
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def timeline_home(app, user):
|
2017-04-21 12:57:34 +02:00
|
|
|
return _get(app, user, '/api/v1/timelines/home').json()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_next_path(headers):
|
|
|
|
links = headers.get('Link', '')
|
|
|
|
matches = re.match('<([^>]+)>; rel="next"', links)
|
|
|
|
if matches:
|
|
|
|
url = matches.group(1)
|
|
|
|
return urlparse(url).path
|
|
|
|
|
|
|
|
|
|
|
|
def timeline_generator(app, user):
|
|
|
|
next_path = '/api/v1/timelines/home'
|
|
|
|
|
|
|
|
while next_path:
|
|
|
|
response = _get(app, user, next_path)
|
|
|
|
yield response.json()
|
|
|
|
next_path = _get_next_path(response.headers)
|
2017-04-16 14:14:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def upload_media(app, user, file):
|
|
|
|
return _post(app, user, '/api/v1/media', files={
|
|
|
|
'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):
|
|
|
|
return _get(app, user, '/api/v1/search', {
|
|
|
|
'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):
|
|
|
|
return _get(app, user, '/api/v1/accounts/search', {
|
|
|
|
'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-04-21 12:57:34 +02:00
|
|
|
return _get(app, user, '/api/v1/accounts/verify_credentials').json()
|
|
|
|
|
|
|
|
|
|
|
|
def get_notifications(app, user):
|
|
|
|
return _get(app, user, '/api/v1/notifications').json()
|