Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim website metadata title and description #383

Merged
merged 4 commits into from
Jan 12, 2023
Merged

Trim website metadata title and description #383

merged 4 commits into from
Jan 12, 2023

Conversation

luca1197
Copy link
Contributor

Some sites seem to return metadata titles / descriptions with trailing or leading whitespaces, which should be unwanted in most if not all cases. This PR adds a .trim method call to the fetched values, which will remove these whitespaces.

The whitespaces are not shown in the bookmark list after it is saved, but they are being stored, so they are also still there when editing the bookmark.

One example would be Humble Bundle pages, e.g. this one: https://www.humblebundle.com/software/unreal-engine-ultimate-game-dev-assets-software

image

@sissbruecker
Copy link
Owner

sissbruecker commented Dec 28, 2022

Thanks, however the change here only affects the visual appearance of the placeholder, while the data returned from the scaper, and the data saved to the database, still contains the whitespace. It would make more sense to apply this fix to the scaper:

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)

@luca1197
Copy link
Contributor Author

Thank you for the feedback, I did not expect it would also fetch the data in the backend again. I implemented the trimming in the scraper (thank you for the snippet!) and tested it. Seems to work great.

bookmarks/services/website_loader.py Outdated Show resolved Hide resolved
bookmarks/templates/bookmarks/form.html Outdated Show resolved Hide resolved
@sissbruecker
Copy link
Owner

Added a test for the new trim behavior.

@sissbruecker sissbruecker changed the title Trim fetched metadata placeholders Trim website metadata title and description Jan 6, 2023
@sissbruecker sissbruecker merged commit c2d8cde into sissbruecker:master Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants