-
Notifications
You must be signed in to change notification settings - Fork 348
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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__) | ||
|
||
|
@@ -23,6 +25,7 @@ class ImportResult: | |
@dataclass | ||
class ImportOptions: | ||
map_private_flag: bool = False | ||
get_metadata_flag: bool = False | ||
|
||
|
||
class TagCache: | ||
|
@@ -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 | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a background task instead, you can check Tests would be welcome as well, |
||
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, | ||
|
There was a problem hiding this comment.
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.