Skip to content

Commit

Permalink
Update npf-renderer to 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
syeopite committed Dec 13, 2023
1 parent 1bf3520 commit 7858484
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions assets/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ li.control-bar-action:hover {
transform: translateX(-50%);
overflow: hidden;
width: fit-content;
z-index: 1;
}

.control-bar-dropdown-menu li {
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ idna==3.4
Jinja2==3.1.2
MarkupSafe==2.1.3
multidict==6.0.4
npf-renderer==0.11.1
npf-renderer==0.12.0
orjson==3.8.0
PyYAML==6.0.1
sanic==23.3.0
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def url_handler(raw_url):
return f"/tblr/media/44{url.path}"
elif hostname.endswith("static.tumblr.com"):
return f"/tblr/static/{url.path}"
elif hostname.startswith("va.media"):
return f"/tblr/media/va/{url.path}"
else:
# Check for subdomain blog
sub_domains = hostname.split(".")
Expand Down
16 changes: 14 additions & 2 deletions src/routes/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
media = sanic.Blueprint("TumblrMedia", url_prefix="/tblr")


async def get_media(request, client, path_to_request):
async with client.stream("GET", path_to_request) as tumblr_response:
async def get_media(request, client, path_to_request, additional_headers = None):
async with client.stream("GET", path_to_request, headers=additional_headers) as tumblr_response:
# Sanitize the headers given by Tumblr
priviblur_response_headers = {}
for header_key, header_value in tumblr_response.headers.items():
Expand All @@ -26,6 +26,8 @@ async def get_media(request, client, path_to_request):
async for chunk in tumblr_response.aiter_raw():
await priviblur_response.send(chunk)

await priviblur_response.eof()


@media.get(r"/media/64/<path:path>")
async def _64_media(request: sanic.Request, path: str):
Expand All @@ -45,6 +47,16 @@ async def _44_media(request: sanic.Request, path: str):
return await get_media(request, request.app.ctx.Media44Client, path)


@media.get(r"/media/va/<path:path>")
async def _va_media(request: sanic.Request, path: str):
"""Proxies the requested media from va.media.tumblr.com"""
additional_headers={
"accept": "video/webm,video/ogg,video/*;q=0.9," \
"application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5"
}
return await get_media(request, request.app.ctx.MediaVaClient, path, additional_headers=additional_headers)


@media.get(r"/assets/<path:path>")
async def _tb_assets(request: sanic.Request, path: str):
"""Proxies the requested media from assets.tumblr.com"""
Expand Down
6 changes: 5 additions & 1 deletion src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def initialize(app):
def create_image_client(url, timeout):
media_headers = copy.copy(media_request_headers)
media_headers["host"] = url
return httpx.AsyncClient(base_url=url, headers=media_request_headers, http2=True, timeout=timeout)
return httpx.AsyncClient(base_url=url, headers=media_request_headers, http1=True, timeout=timeout)

app.ctx.Media64Client = create_image_client(
"https://64.media.tumblr.com", priviblur_backend.image_response_timeout
Expand All @@ -91,6 +91,10 @@ def create_image_client(url, timeout):
"https://44.media.tumblr.com", priviblur_backend.image_response_timeout
)

app.ctx.MediaVaClient = create_image_client(
"https://va.media.tumblr.com", priviblur_backend.image_response_timeout
)

app.ctx.TumblrAssetClient = create_image_client(
"https://assets.tumblr.com", priviblur_backend.image_response_timeout
)
Expand Down
5 changes: 2 additions & 3 deletions src/templates/components/post.jinja
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% from 'macros/post_header.jinja' import create_post_header %}
<div class="post">
{%- if element.content -%}
{% set contains_errors, content_tag = format_npf(element.content, element.layout, url_handler=url_handler, skip_cropped_images=True) -%}
{% set contains_errors, content_tag = format_npf(element.content, element.layout, url_handler=url_handler, skip_cropped_images=True, reserve_space_for_images=True, forbid_external_iframes=True) -%}
{%- endif -%}

{{ create_post_header(element) }}
Expand All @@ -15,7 +15,7 @@
{% for element in element.trail %}
<div class="trail-post">
{{create_post_header(element)}}
{% set trail_contains_errors, trail_content_tag = format_npf(element.content, element.layout, url_handler=url_handler, skip_cropped_images=True) -%}
{% set trail_contains_errors, trail_content_tag = format_npf(element.content, element.layout, url_handler=url_handler, skip_cropped_images=True, reserve_space_for_images=True, forbid_external_iframes=True) -%}
{{- trail_content_tag -}}
</div>

Expand All @@ -28,7 +28,6 @@
{% if element.trail and element.content %}
<div class="trail-post">
{{create_post_header(element, skip_reblog=True)}}
{% set trail_contains_errors, trail_content_tag = format_npf(element.content, element.layout, url_handler=url_handler, skip_cropped_images=True) -%}
{{- content_tag -}}
</div>
{% else %}
Expand Down

0 comments on commit 7858484

Please sign in to comment.