2024-03-08 20:33:36 +00:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
linkwarden_url = os.environ.get('LINKWARDEN_URL')
|
|
|
|
linkwarden_token = os.environ.get('LINKWARDEN_TOKEN')
|
|
|
|
|
2024-07-02 13:42:26 +00:00
|
|
|
def get_links_part(cursor):
|
2024-03-08 20:33:36 +00:00
|
|
|
request = urllib.request.Request(linkwarden_url + "/api/v1/links?sort=0&cursor=" + str(cursor), headers={
|
|
|
|
'Authorization': 'Bearer ' + linkwarden_token
|
|
|
|
})
|
|
|
|
response = urllib.request.urlopen(request)
|
|
|
|
data = response.read().decode("utf-8")
|
|
|
|
data = data.replace("\\n", " ").replace("\\r", " ").replace("\\t", " ")
|
|
|
|
data = ' '.join(data.split())
|
|
|
|
return json.loads(data).get("response", [])
|
|
|
|
|
2024-07-02 13:42:26 +00:00
|
|
|
def get_links():
|
2024-03-08 20:33:36 +00:00
|
|
|
cursor = 0
|
2024-07-02 13:42:26 +00:00
|
|
|
links = []
|
2024-03-08 20:33:36 +00:00
|
|
|
|
|
|
|
while True:
|
2024-07-02 13:42:26 +00:00
|
|
|
new_links = get_links_part(cursor)
|
2024-03-08 20:33:36 +00:00
|
|
|
links += new_links
|
|
|
|
if len(new_links):
|
|
|
|
cursor = new_links[-1]["id"]
|
|
|
|
else:
|
2024-07-02 13:42:26 +00:00
|
|
|
return links
|
|
|
|
|
|
|
|
def format_links(links):
|
|
|
|
formatted_links = []
|
|
|
|
for link in links:
|
|
|
|
url = link.get("url")
|
|
|
|
if not url:
|
|
|
|
continue
|
|
|
|
|
|
|
|
title = link.get("name")
|
|
|
|
if not title:
|
|
|
|
title = link.get("description")
|
|
|
|
if not title:
|
|
|
|
title = "<untitled>"
|
|
|
|
|
|
|
|
collection = link.get("collection", {}).get("name")
|
|
|
|
if not collection:
|
|
|
|
collection = "<uncategorized>"
|
|
|
|
|
|
|
|
formatted_links.append(f"{url} {collection}/{title}")
|
|
|
|
return formatted_links
|
2024-03-08 20:33:36 +00:00
|
|
|
|
2024-07-02 13:42:26 +00:00
|
|
|
def init():
|
|
|
|
if linkwarden_url is None or linkwarden_token is None:
|
|
|
|
return
|
2024-03-08 20:33:36 +00:00
|
|
|
|
2024-07-02 13:42:26 +00:00
|
|
|
links = format_links(get_links())
|
2024-03-08 20:33:36 +00:00
|
|
|
|
2024-07-02 13:42:26 +00:00
|
|
|
with open(os.path.expanduser("~/.config/qutebrowser/bookmarks/urls"), "w+") as f:
|
|
|
|
f.write("\n".join(links))
|
2024-03-08 20:33:36 +00:00
|
|
|
f.close()
|