set user agent on mastodon.py requests

This commit is contained in:
codl 2019-07-11 22:31:29 +02:00
parent 9e3e68de3a
commit da7d64acbd
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 18 additions and 0 deletions

View File

@ -6,9 +6,11 @@ from mastodon.Mastodon import MastodonAPIError,\
MastodonUnauthorizedError MastodonUnauthorizedError
from model import MastodonApp, Account, OAuthToken, Post, MastodonInstance from model import MastodonApp, Account, OAuthToken, Post, MastodonInstance
from requests import head from requests import head
import requests
from app import db, sentry from app import db, sentry
from libforget.exceptions import TemporaryError from libforget.exceptions import TemporaryError
from functools import lru_cache from functools import lru_cache
from libforget.session import make_session
def get_or_create_app(instance_url, callback, website): def get_or_create_app(instance_url, callback, website):
@ -37,11 +39,13 @@ def get_or_create_app(instance_url, callback, website):
return app return app
def anonymous_api(app): def anonymous_api(app):
return Mastodon( return Mastodon(
app.client_id, app.client_id,
client_secret=app.client_secret, client_secret=app.client_secret,
api_base_url='{}://{}'.format(app.protocol, app.instance), 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), api_base_url='{}://{}'.format(app.protocol, app.instance),
access_token=token.token, access_token=token.token,
ratelimit_method='throw', ratelimit_method='throw',
session=make_session(),
) )
try: try:
# api.verify_credentials() # api.verify_credentials()

13
libforget/session.py Normal file
View File

@ -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