switch to faster base64 implementation

(60 ms savings on https://askiiart.net/blog/marlin-boot-animations.html, though that has very little graphics)
This commit is contained in:
askiiart 2025-01-04 17:33:16 -06:00
parent c541151dff
commit 4edf55cf08
Signed by untrusted user who does not match committer: askiiart
GPG key ID: 6A32977DAF31746A
3 changed files with 13 additions and 4 deletions

View file

@ -16,6 +16,14 @@ And also fixes any relative links to be absolute.
## Usage
Install dependencies first of course
```sh
pip install -r requirements.txt
```
Then run it:
```sh
python3 owug.py 'https://example.com' 'out.html'
```

View file

@ -1,7 +1,7 @@
import sys
from bs4 import BeautifulSoup
import requests
import base64
from pybase64 import b64encode_as_string
def absolute_url(url, base_domain):
@ -33,7 +33,7 @@ for favicon in favicons:
url = absolute_url(favicon.attrs['href'], domain_thing)
mime_type = requests.head(url).headers['Content-Type']
as_base64 = base64.b64encode(requests.get(url).content).decode()
as_base64 = b64encode_as_string(requests.get(url).content)
new_url = f'data:{mime_type};base64,{as_base64}'
favicon.attrs['href'] = new_url
@ -43,7 +43,7 @@ imgs = soup.find_all('img')
for item in imgs:
url = absolute_url(item.attrs['src'], domain_thing)
mime_type = requests.head(url).headers['Content-Type']
as_base64 = base64.b64encode(requests.get(url).content).decode()
as_base64 = b64encode_as_string(requests.get(url).content)
new_url = f'data:{mime_type};base64,{as_base64}'
item.attrs['src'] = new_url

View file

@ -1,3 +1,4 @@
bs4
requests
html5lib
html5lib
pybase64