mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
Merge pull request 'Fetching remote actors' (#104) from freamon/pyfedi:16b into main
Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/104
This commit is contained in:
commit
9d560fdf1e
2 changed files with 14 additions and 3 deletions
|
@ -200,6 +200,7 @@ def instance_allowed(host: str) -> bool:
|
||||||
|
|
||||||
|
|
||||||
def find_actor_or_create(actor: str, create_if_not_found=True, community_only=False) -> Union[User, Community, None]:
|
def find_actor_or_create(actor: str, create_if_not_found=True, community_only=False) -> Union[User, Community, None]:
|
||||||
|
actor_url = actor.strip()
|
||||||
actor = actor.strip().lower()
|
actor = actor.strip().lower()
|
||||||
user = None
|
user = None
|
||||||
# actor parameter must be formatted as https://server/u/actor or https://server/c/actor
|
# actor parameter must be formatted as https://server/u/actor or https://server/c/actor
|
||||||
|
@ -244,10 +245,15 @@ def find_actor_or_create(actor: str, create_if_not_found=True, community_only=Fa
|
||||||
if create_if_not_found:
|
if create_if_not_found:
|
||||||
if actor.startswith('https://'):
|
if actor.startswith('https://'):
|
||||||
try:
|
try:
|
||||||
actor_data = get_request(actor, headers={'Accept': 'application/activity+json'})
|
actor_data = get_request(actor_url, headers={'Accept': 'application/activity+json'})
|
||||||
except requests.exceptions.ReadTimeout:
|
except requests.exceptions.ReadTimeout:
|
||||||
time.sleep(randint(3, 10))
|
time.sleep(randint(3, 10))
|
||||||
actor_data = get_request(actor, headers={'Accept': 'application/activity+json'})
|
try:
|
||||||
|
actor_data = get_request(actor_url, headers={'Accept': 'application/activity+json'})
|
||||||
|
except requests.exceptions.ReadTimeout:
|
||||||
|
return None
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
return None
|
||||||
if actor_data.status_code == 200:
|
if actor_data.status_code == 200:
|
||||||
actor_json = actor_data.json()
|
actor_json = actor_data.json()
|
||||||
actor_data.close()
|
actor_data.close()
|
||||||
|
|
|
@ -13,7 +13,9 @@ import math
|
||||||
from urllib.parse import urlparse, parse_qs, urlencode
|
from urllib.parse import urlparse, parse_qs, urlencode
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import flask
|
import flask
|
||||||
from bs4 import BeautifulSoup, NavigableString
|
from bs4 import BeautifulSoup, NavigableString, MarkupResemblesLocatorWarning
|
||||||
|
import warnings
|
||||||
|
warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning)
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
from flask import current_app, json, redirect, url_for, request, make_response, Response, g
|
from flask import current_app, json, redirect, url_for, request, make_response, Response, g
|
||||||
|
@ -91,6 +93,9 @@ def get_request(uri, params=None, headers=None) -> requests.Response:
|
||||||
except requests.exceptions.ReadTimeout as read_timeout:
|
except requests.exceptions.ReadTimeout as read_timeout:
|
||||||
current_app.logger.info(f"{uri} {read_timeout}")
|
current_app.logger.info(f"{uri} {read_timeout}")
|
||||||
raise requests.exceptions.ReadTimeout from read_timeout
|
raise requests.exceptions.ReadTimeout from read_timeout
|
||||||
|
except requests.exceptions.ConnectionError as connection_error:
|
||||||
|
current_app.logger.info(f"{uri} {connection_error}")
|
||||||
|
raise requests.exceptions.ConnectionError from connection_error
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue