Refactor wallabag conversion to have simple cli
This commit is contained in:
parent
813327f939
commit
59aaa74d76
8 changed files with 149 additions and 79 deletions
|
|
@ -1,46 +1,25 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from base import Wallabag_Converter
|
||||
from jinja2 import Template
|
||||
|
||||
# USAGE: ./convert.py file exportfile
|
||||
|
||||
if len(sys.argv) < 2 or not Path(sys.argv[1]).is_file():
|
||||
print("Please provide a file to import as the first argument.")
|
||||
sys.exit(1)
|
||||
class Netscape_Converter(Wallabag_Converter):
|
||||
def __init__(self, data: list[object]):
|
||||
self.data = data
|
||||
|
||||
INPUT_FILE = Path(sys.argv[1])
|
||||
OUTPUT_FILE = (
|
||||
Path(sys.argv[2]) if len(sys.argv) > 2 else Path("exported_bookmarks.html")
|
||||
)
|
||||
print(f"[DEBUG]\ninput: {INPUT_FILE}\noutput: {OUTPUT_FILE}")
|
||||
# TODO: Timestamp does not get recognized and instead becomes 1970-01-01 - maybe needs unix ts?
|
||||
def _generate_html(self, data):
|
||||
return Template("""<!DOCTYPE NETSCAPE-Bookmark-file-1>
|
||||
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
|
||||
<TITLE>Bookmarks</TITLE>
|
||||
<H1>Bookmarks</H1>
|
||||
<DL><p>
|
||||
{% for item in data %}
|
||||
<DT>
|
||||
<A HREF="{{ item.url }}" ADD_DATE="{{ item.created_at }}" TAGS="{{ item.tags }}">{{ item.title }}</A>
|
||||
</DT>
|
||||
{% endfor %}
|
||||
</DL><p>
|
||||
""").render(data=data)
|
||||
|
||||
|
||||
# TODO: Timestamp does not get recognized and instead becomes 1970-01-01 - maybe needs unix ts?
|
||||
def generate_html(data):
|
||||
return Template("""<!DOCTYPE NETSCAPE-Bookmark-file-1>
|
||||
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
|
||||
<TITLE>Bookmarks</TITLE>
|
||||
<H1>Bookmarks</H1>
|
||||
<DL><p>
|
||||
{% for item in data %}
|
||||
<DT>
|
||||
<A HREF="{{ item.url }}" ADD_DATE="{{ item.created_at }}" TAGS="{{ item.tags }}">{{ item.title }}</A>
|
||||
</DT>
|
||||
{% endfor %}
|
||||
</DL><p>
|
||||
""").render(data=data)
|
||||
|
||||
|
||||
# Read JSON file
|
||||
with open(INPUT_FILE, "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
html_content = generate_html(data)
|
||||
|
||||
# Save or print HTML content
|
||||
with open(OUTPUT_FILE, "w") as f:
|
||||
f.write(html_content)
|
||||
def convert(self) -> str:
|
||||
return self._generate_html(self.data)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue