From da7d64acbd6f4cba2e2da6a860d90d2e11dfb69d Mon Sep 17 00:00:00 2001 From: codl Date: Thu, 11 Jul 2019 22:31:29 +0200 Subject: [PATCH] set user agent on mastodon.py requests --- libforget/mastodon.py | 5 +++++ libforget/session.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 libforget/session.py diff --git a/libforget/mastodon.py b/libforget/mastodon.py index 1665f2e..8f99ce0 100644 --- a/libforget/mastodon.py +++ b/libforget/mastodon.py @@ -6,9 +6,11 @@ from mastodon.Mastodon import MastodonAPIError,\ MastodonUnauthorizedError from model import MastodonApp, Account, OAuthToken, Post, MastodonInstance from requests import head +import requests from app import db, sentry from libforget.exceptions import TemporaryError from functools import lru_cache +from libforget.session import make_session def get_or_create_app(instance_url, callback, website): @@ -37,11 +39,13 @@ def get_or_create_app(instance_url, callback, website): return app + def anonymous_api(app): return Mastodon( app.client_id, client_secret=app.client_secret, api_base_url='{}://{}'.format(app.protocol, app.instance), + session=make_session(), ) @@ -80,6 +84,7 @@ def get_api_for_acc(account): api_base_url='{}://{}'.format(app.protocol, app.instance), access_token=token.token, ratelimit_method='throw', + session=make_session(), ) try: # api.verify_credentials() diff --git a/libforget/session.py b/libforget/session.py new file mode 100644 index 0000000..cbccebf --- /dev/null +++ b/libforget/session.py @@ -0,0 +1,13 @@ +from requests import Session + + +def make_session(): + s = requests.Session() + s.headers.update( + { + "user-agent": "Forget/{version} +https://forget.codl.fr".format( + version=version.get_versions()["version"] + ) + } + ) + return s