Preview website title + description in bookmark form

Fix unnecessary selects when rendering bookmarks
This commit is contained in:
Sascha Ißbrücker
2019-07-02 01:28:02 +02:00
parent 0e872c754b
commit e07da529f1
9 changed files with 145 additions and 59 deletions

View File

@@ -0,0 +1,37 @@
from dataclasses import dataclass
import requests
from bs4 import BeautifulSoup
@dataclass
class WebsiteMetadata:
url: str
title: str
description: str
def to_dict(self):
return {
'url': self.url,
'title': self.title,
'description': self.description,
}
def load_website_metadata(url: str):
title = None
description = None
try:
page_text = load_page(url)
soup = BeautifulSoup(page_text, 'html.parser')
title = soup.title.string if soup.title is not None else None
description_tag = soup.find('meta', attrs={'name': 'description'})
description = description_tag['content'] if description_tag is not None else None
finally:
return WebsiteMetadata(url=url, title=title, description=description)
def load_page(url: str):
r = requests.get(url)
return r.text