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

Adds the option to fetch metadata to imports #979

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bookmarks/services/bookmarks.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be reverted when adding the background task.

Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ def update_bookmark(bookmark: Bookmark, tag_string, current_user: User):
return bookmark


def enhance_with_website_metadata(bookmark: Bookmark):
def enhance_with_website_metadata(bookmark: Bookmark, save: bool = True):
metadata = website_loader.load_website_metadata(bookmark.url)
if not bookmark.title:
bookmark.title = metadata.title or ""

if not bookmark.description:
bookmark.description = metadata.description or ""

bookmark.save()
if save:
bookmark.save()


def archive_bookmark(bookmark: Bookmark):
Expand Down
19 changes: 17 additions & 2 deletions bookmarks/services/importer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from dataclasses import dataclass
from typing import List
from concurrent.futures import ThreadPoolExecutor

from django.contrib.auth.models import User
from django.utils import timezone
Expand All @@ -9,6 +10,7 @@
from bookmarks.services import tasks
from bookmarks.services.parser import parse, NetscapeBookmark
from bookmarks.utils import parse_timestamp
from bookmarks.services.bookmarks import enhance_with_website_metadata

logger = logging.getLogger(__name__)

Expand All @@ -23,6 +25,7 @@ class ImportResult:
@dataclass
class ImportOptions:
map_private_flag: bool = False
get_metadata_flag: bool = False


class TagCache:
Expand Down Expand Up @@ -132,8 +135,8 @@ def _import_batch(
existing_bookmarks = Bookmark.objects.filter(owner=user, url__in=batch_urls)

# Create or update bookmarks from parsed Netscape bookmarks
bookmarks_to_create = []
bookmarks_to_update = []
bookmarks_to_create: List[Bookmark] = []
bookmarks_to_update: List[Bookmark] = []

for netscape_bookmark in netscape_bookmarks:
result.total = result.total + 1
Expand Down Expand Up @@ -169,6 +172,18 @@ def _import_batch(
logging.exception("Error importing bookmark: " + shortened_bookmark_tag_str)
result.failed = result.failed + 1

if options.get_metadata_flag:
bookmarks_missing_metadata = filter(
lambda b: not b.title or not b.description,
bookmarks_to_create + bookmarks_to_update,
)

with ThreadPoolExecutor(5) as executor:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a background task instead, you can check bookmarks/services/tasks.py as a reference for existing ones. So you could add a new task there, then schedule one per bookmark from here.

Tests would be welcome as well, test_bookmarks_tasks.py is used for testing tasks themselves, and test_importer.py could test that the task is called on import.

executor.map(
lambda b: enhance_with_website_metadata(b, save=False),
bookmarks_missing_metadata,
)

# Bulk update bookmarks in DB
Bookmark.objects.bulk_update(
bookmarks_to_update,
Expand Down
7 changes: 7 additions & 0 deletions bookmarks/templates/settings/general.html
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ <h2>Import</h2>
private as shared bookmarks.
Otherwise, all bookmarks will be imported as private bookmarks.
</div>
<label for="import_get_metadata_flag" class="form-checkbox">
<input type="checkbox" id="import_get_metadata_flag" name="get_metadata_flag">
<i class="form-icon"></i> Fetch missing metadata
</label>
<div class="form-input-hint">
Enabling this option will fetch metadata for missing title or description fields for both updated and newly added bookmarks.
</div>
</div>
<div class="form-group">
<div class="input-group width-75 width-md-100">
Expand Down
3 changes: 2 additions & 1 deletion bookmarks/views/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def integrations(request):
def bookmark_import(request):
import_file = request.FILES.get("import_file")
import_options = importer.ImportOptions(
map_private_flag=request.POST.get("map_private_flag") == "on"
map_private_flag=request.POST.get("map_private_flag") == "on",
get_metadata_flag=request.POST.get("get_metadata_flag") == "on",
)

if import_file is None:
Expand Down