Respect environment variables for httpx
This commit is contained in:
parent
771cfadc23
commit
a282dea03a
1 changed files with 104 additions and 92 deletions
34
server.py
34
server.py
|
@ -52,6 +52,7 @@ app = fastapi.FastAPI(
|
|||
lifespan=startup
|
||||
)
|
||||
lock = Lock()
|
||||
# noinspection PyTypeChecker
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
|
@ -159,7 +160,14 @@ CACHE_FILE.touch(exist_ok=True)
|
|||
logging.debug("Cache file: %r", CACHE_FILE)
|
||||
|
||||
|
||||
def upload_media(domain: str, access_token: str, file: io.BytesIO, filename: str, content_type: str):
|
||||
def upload_media(
|
||||
client: httpx.Client,
|
||||
domain: str,
|
||||
access_token: str,
|
||||
file: io.BytesIO,
|
||||
filename: str,
|
||||
content_type: str
|
||||
):
|
||||
file.seek(0)
|
||||
logging.info(
|
||||
"Creating media at %r called %r with the content type %r and %d bytes",
|
||||
|
@ -170,7 +178,7 @@ def upload_media(domain: str, access_token: str, file: io.BytesIO, filename: str
|
|||
)
|
||||
|
||||
# noinspection PyTypeChecker
|
||||
response = httpx.post(
|
||||
response = client.post(
|
||||
"%s/_matrix/media/r0/upload" % domain,
|
||||
headers={
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
|
@ -243,15 +251,16 @@ def preview_url(
|
|||
|
||||
domain = os.environ.get("PREVIEW_HOMESERVER", "https://" + req.url.hostname)
|
||||
with lock:
|
||||
try:
|
||||
with httpx.Client(
|
||||
headers={
|
||||
# "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"
|
||||
"User-Agent": "TwitterBot/1.0"
|
||||
},
|
||||
timeout=60,
|
||||
follow_redirects=False
|
||||
follow_redirects=False,
|
||||
trust_env=True # for HTTP[S]/ALL_PROXY environment variables.
|
||||
) as client:
|
||||
try:
|
||||
response = client.get(
|
||||
url,
|
||||
)
|
||||
|
@ -303,19 +312,21 @@ def preview_url(
|
|||
logging.debug("%r = %r", tag_name, _url)
|
||||
try:
|
||||
# noinspection PyArgumentList
|
||||
with httpx.stream(
|
||||
with client.stream(
|
||||
url=_url,
|
||||
method="GET",
|
||||
headers={
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"
|
||||
},
|
||||
timeout=60,
|
||||
follow_redirects=True
|
||||
) as response_media:
|
||||
if response_media.status_code not in range(200, 300):
|
||||
logging.warning("Failed to fetch media: %r - HTTP %s", _url, response_media.status_code)
|
||||
logging.warning(
|
||||
"Failed to fetch media: %r - HTTP %s",
|
||||
_url,
|
||||
response_media.status_code
|
||||
)
|
||||
og_tags.pop(tag_name, None)
|
||||
elif not response_media.headers.get("content-type", "").startswith(("image/", "video/", "audio/")):
|
||||
elif not response_media.headers.get("content-type", "").startswith(
|
||||
("image/", "video/", "audio/")
|
||||
):
|
||||
logging.warning("Failed to fetch media: %r - not a media type", _url)
|
||||
og_tags.pop(tag_name, None)
|
||||
else:
|
||||
|
@ -329,6 +340,7 @@ def preview_url(
|
|||
_file.write(response_media.read())
|
||||
_file.seek(0)
|
||||
upload_response = upload_media(
|
||||
client,
|
||||
domain,
|
||||
access_token,
|
||||
_file,
|
||||
|
|
Loading…
Reference in a new issue