Provide a User Agent when querying the Mastodon API

This commit is contained in:
Michael Thomas 2023-03-10 12:30:28 +00:00
parent 8f5fff289d
commit 5ed5637173

View file

@ -9,7 +9,6 @@ import sys
import requests import requests
import time import time
def pull_context( def pull_context(
server, server,
access_token, access_token,
@ -83,7 +82,10 @@ def get_timeline(server, access_token, max):
def get_toots(url, access_token): def get_toots(url, access_token):
response = requests.get( response = requests.get(
url, headers={"Authorization": f"Bearer {access_token}"}, timeout=5 url, headers={
"Authorization": f"Bearer {access_token}",
'User-Agent': 'mastodon_get_replies (https://paste.thms.uk/mastodon_get_replies.md/markup)'
}, timeout=5
) )
if response.status_code == 200: if response.status_code == 200:
@ -109,7 +111,10 @@ def get_active_user_ids(server, access_token, reply_interval_hours):
since = datetime.now() - timedelta(days=reply_interval_hours / 24 + 1) since = datetime.now() - timedelta(days=reply_interval_hours / 24 + 1)
url = f"https://{server}/api/v1/admin/accounts" url = f"https://{server}/api/v1/admin/accounts"
resp = requests.get( resp = requests.get(
url, headers={"Authorization": f"Bearer {access_token}"}, timeout=5 url, headers={
"Authorization": f"Bearer {access_token}",
'User-Agent': 'mastodon_get_replies (https://paste.thms.uk/mastodon_get_replies.md/markup)'
}, timeout=5
) )
if resp.status_code == 200: if resp.status_code == 200:
for user in resp.json(): for user in resp.json():
@ -158,7 +163,10 @@ def get_reply_toots(user_id, server, access_token, seen_urls, reply_since):
try: try:
resp = requests.get( resp = requests.get(
url, headers={"Authorization": f"Bearer {access_token}"}, timeout=5 url, headers={
"Authorization": f"Bearer {access_token}",
'User-Agent': 'mastodon_get_replies (https://paste.thms.uk/mastodon_get_replies.md/markup)'
}, timeout=5
) )
except Exception as ex: except Exception as ex:
print( print(
@ -305,7 +313,9 @@ def parse_pleroma_url(url):
def get_redirect_url(url): def get_redirect_url(url):
"""get the URL given URL redirects to""" """get the URL given URL redirects to"""
try: try:
resp = requests.head(url, allow_redirects=False, timeout=5) resp = requests.head(url, allow_redirects=False, timeout=5,headers={
'User-Agent': 'mastodon_get_replies (https://paste.thms.uk/mastodon_get_replies.md/markup)'
})
except Exception as ex: except Exception as ex:
print(f"Error getting redirect URL for URL {url}. Exception: {ex}") print(f"Error getting redirect URL for URL {url}. Exception: {ex}")
return None return None
@ -338,7 +348,9 @@ def get_toot_context(server, toot_id, toot_url):
"""get the URLs of the context toots of the given toot""" """get the URLs of the context toots of the given toot"""
url = f"https://{server}/api/v1/statuses/{toot_id}/context" url = f"https://{server}/api/v1/statuses/{toot_id}/context"
try: try:
resp = requests.get(url, timeout=5) resp = requests.get(url, timeout=5,headers={
'User-Agent': 'mastodon_get_replies (https://paste.thms.uk/mastodon_get_replies.md/markup)'
})
except Exception as ex: except Exception as ex:
print(f"Error getting context for toot {toot_url}. Exception: {ex}") print(f"Error getting context for toot {toot_url}. Exception: {ex}")
return [] return []
@ -386,7 +398,10 @@ def add_context_url(url, server, access_token):
try: try:
resp = requests.get( resp = requests.get(
search_url, search_url,
headers={"Authorization": f"Bearer {access_token}"}, headers={
"Authorization": f"Bearer {access_token}",
'User-Agent': 'mastodon_get_replies (https://paste.thms.uk/mastodon_get_replies.md/markup)'
},
timeout=5, timeout=5,
) )
except Exception as ex: except Exception as ex: