1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-23 15:37:47 +01:00

Implement fetching posts as a generator

This commit is contained in:
Ivan Habunek 2017-04-21 12:57:34 +02:00
parent 12047cdc92
commit 1c22eaa44f
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95

View File

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
import re
import requests import requests
from future.moves.urllib.parse import urlparse
from requests import Request, Session from requests import Request, Session
from toot import CLIENT_NAME, CLIENT_WEBSITE from toot import CLIENT_NAME, CLIENT_WEBSITE
@ -61,7 +63,7 @@ def _process_response(response):
raise ApiError(error) raise ApiError(error)
return response.json() return response
def _get(app, user, url, params=None): def _get(app, user, url, params=None):
@ -101,7 +103,7 @@ def create_app(instance):
'website': CLIENT_WEBSITE, 'website': CLIENT_WEBSITE,
}) })
return _process_response(response) return _process_response(response).json()
def login(app, username, password): def login(app, username, password):
@ -120,7 +122,7 @@ def login(app, username, password):
if response.is_redirect: if response.is_redirect:
raise AuthenticationError() raise AuthenticationError()
return _process_response(response) return _process_response(response).json()
def post_status(app, user, status, visibility='public', media_ids=None): def post_status(app, user, status, visibility='public', media_ids=None):
@ -128,43 +130,64 @@ def post_status(app, user, status, visibility='public', media_ids=None):
'status': status, 'status': status,
'media_ids[]': media_ids, 'media_ids[]': media_ids,
'visibility': visibility, 'visibility': visibility,
}) }).json()
def timeline_home(app, user): def timeline_home(app, user):
return _get(app, user, '/api/v1/timelines/home') 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)
def upload_media(app, user, file): def upload_media(app, user, file):
return _post(app, user, '/api/v1/media', files={ return _post(app, user, '/api/v1/media', files={
'file': file 'file': file
}) }).json()
def search(app, user, query, resolve): def search(app, user, query, resolve):
return _get(app, user, '/api/v1/search', { return _get(app, user, '/api/v1/search', {
'q': query, 'q': query,
'resolve': resolve, 'resolve': resolve,
}) }).json()
def search_accounts(app, user, query): def search_accounts(app, user, query):
return _get(app, user, '/api/v1/accounts/search', { return _get(app, user, '/api/v1/accounts/search', {
'q': query, 'q': query,
}) }).json()
def follow(app, user, account): def follow(app, user, account):
url = '/api/v1/accounts/%d/follow' % account url = '/api/v1/accounts/%d/follow' % account
return _post(app, user, url) return _post(app, user, url).json()
def unfollow(app, user, account): def unfollow(app, user, account):
url = '/api/v1/accounts/%d/unfollow' % account url = '/api/v1/accounts/%d/unfollow' % account
return _post(app, user, url) return _post(app, user, url).json()
def verify_credentials(app, user): def verify_credentials(app, user):
return _get(app, user, '/api/v1/accounts/verify_credentials') return _get(app, user, '/api/v1/accounts/verify_credentials').json()
def get_notifications(app, user):
return _get(app, user, '/api/v1/notifications').json()