Implement basic importer
This commit is contained in:
parent
451a049d46
commit
9eefd479c6
1
.gitignore
vendored
1
.gitignore
vendored
@ -159,3 +159,4 @@ venv.bak/
|
||||
.mypy_cache/
|
||||
|
||||
polls
|
||||
tmp
|
||||
|
12
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
12
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,12 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredErrors">
|
||||
<list>
|
||||
<option value="E402" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
21
bookmarks/management/commands/import_netscape.py
Normal file
21
bookmarks/management/commands/import_netscape.py
Normal file
@ -0,0 +1,21 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from bookmarks.services.importer import import_netscape_html
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Import Netscape HTML bookmark file'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('file', type=str, help='Path to file')
|
||||
parser.add_argument('user', type=str, help='Name of the user for which to import')
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
filepath = kwargs['file']
|
||||
username = kwargs['user']
|
||||
with open(filepath) as html_file:
|
||||
html = html_file.read()
|
||||
user = User.objects.get(username=username)
|
||||
|
||||
import_netscape_html(html, user)
|
42
bookmarks/services/importer.py
Normal file
42
bookmarks/services/importer.py
Normal file
@ -0,0 +1,42 @@
|
||||
from datetime import datetime
|
||||
|
||||
from bs4 import BeautifulSoup, Tag
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from bookmarks.models import Bookmark
|
||||
|
||||
|
||||
def import_netscape_html(html: str, user: User):
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
|
||||
bookmark_tags = soup.find_all('dt')
|
||||
|
||||
for bookmark_tag in bookmark_tags:
|
||||
_import_bookmark_tag(bookmark_tag, user)
|
||||
|
||||
|
||||
def _import_bookmark_tag(bookmark_tag: Tag, user: User):
|
||||
link_tag = bookmark_tag.a
|
||||
|
||||
if link_tag is None:
|
||||
return
|
||||
|
||||
# Either modify existing bookmark for the URL or create new one
|
||||
url = link_tag['href']
|
||||
bookmark = _get_or_create_bookmark(url, user)
|
||||
|
||||
bookmark.url = url
|
||||
bookmark.date_added = datetime.utcfromtimestamp(int(link_tag['add_date']))
|
||||
bookmark.date_modified = bookmark.date_added
|
||||
bookmark.unread = link_tag['toread'] == '1'
|
||||
bookmark.title = link_tag.string
|
||||
bookmark.owner = user
|
||||
|
||||
bookmark.save()
|
||||
|
||||
|
||||
def _get_or_create_bookmark(url: str, user: User):
|
||||
try:
|
||||
return Bookmark.objects.get(url=url, owner=user)
|
||||
except Bookmark.DoesNotExist:
|
||||
return Bookmark()
|
Loading…
Reference in New Issue
Block a user