Introduction
Introduction Statistics Contact Development Disclaimer Help
Implement fetching posts as a generator - toot - Unnamed repository; edit this …
Log
Files
Refs
LICENSE
---
commit 1c22eaa44fe49379caaf8b8dac1cfe6c66b3d255
parent 12047cdc92b4d12f3d51b13395f749516caf9640
Author: Ivan Habunek <[email protected]>
Date: Fri, 21 Apr 2017 12:57:34 +0200
Implement fetching posts as a generator
Diffstat:
toot/api.py | 45 +++++++++++++++++++++++--------
1 file changed, 34 insertions(+), 11 deletions(-)
---
diff --git a/toot/api.py b/toot/api.py
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import logging
+import re
import requests
+from future.moves.urllib.parse import urlparse
from requests import Request, Session
from toot import CLIENT_NAME, CLIENT_WEBSITE
@@ -61,7 +63,7 @@ def _process_response(response):
raise ApiError(error)
- return response.json()
+ return response
def _get(app, user, url, params=None):
@@ -101,7 +103,7 @@ def create_app(instance):
'website': CLIENT_WEBSITE,
})
- return _process_response(response)
+ return _process_response(response).json()
def login(app, username, password):
@@ -120,7 +122,7 @@ def login(app, username, password):
if response.is_redirect:
raise AuthenticationError()
- return _process_response(response)
+ return _process_response(response).json()
def post_status(app, user, status, visibility='public', media_ids=None):
@@ -128,43 +130,64 @@ def post_status(app, user, status, visibility='public', m…
'status': status,
'media_ids[]': media_ids,
'visibility': visibility,
- })
+ }).json()
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):
return _post(app, user, '/api/v1/media', files={
'file': file
- })
+ }).json()
def search(app, user, query, resolve):
return _get(app, user, '/api/v1/search', {
'q': query,
'resolve': resolve,
- })
+ }).json()
def search_accounts(app, user, query):
return _get(app, user, '/api/v1/accounts/search', {
'q': query,
- })
+ }).json()
def follow(app, user, account):
url = '/api/v1/accounts/%d/follow' % account
- return _post(app, user, url)
+ return _post(app, user, url).json()
def unfollow(app, user, account):
url = '/api/v1/accounts/%d/unfollow' % account
- return _post(app, user, url)
+ return _post(app, user, url).json()
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()
You are viewing proxied material from vernunftzentrum.de. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.