shaarli书签导出
2022-04-26 • 预计阅读时间 1 分钟
2022-04-26 • 预计阅读时间 1 分钟
书签的管理一直在用Shaarli,来替代之前的Pinboard.为了稳妥,找了个备份jarun/buku: Personal mini-web in text.
通过 shaarli
提供的接口,获取所有的连接并转换成html
写了段 python
来处理.主要的依赖有 requests
和 pyjwt
import jwt
import requests
import os,time,datetime,json
# shaarli configuration
SHAARLI_SERVER_URL = "{SHAARLI_HOST}"
SHAARLI_API_SECRET = "{SHAARLI_TOKEN}"
# static configuration
SHAARLI_HASH_ALGORITHM = "HS512"
HEADER = """<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<!-- This is an automatically generated file.
It will be read and overwritten.
Do Not Edit! -->
<TITLE>my bookmark</TITLE>
<H1>shaarli bookmark list</H1>
<DL><p>"""
FOOTER = "</DL><p>"
if __name__ == "__main__":
now = int(time.time())
payload = { "iat" : now }
token = jwt.encode(payload, SHAARLI_API_SECRET, algorithm = SHAARLI_HASH_ALGORITHM)
url = SHAARLI_SERVER_URL + "/api/v1/links"
params = { "limit" : "all" }
headers = { "Authorization" : "Bearer " + token }
r = requests.get(url, params = params, headers = headers)
content = r.json()
with open('bookmark.html', 'w',encoding='utf-8') as fo:
fo.write(HEADER)
for i in content:
# TODO better timezone handling
timestamp = int(
datetime.datetime.strptime(
i["created"], "%Y-%m-%dT%H:%M:%S%z"
).timestamp()
)
fo.write(
f"""<DT><A HREF=\"{i['url']}\" ADD_DATE=\"{timestamp}\" PRIVATE=\"{i['private']}\" TAGS=\"{",".join(i['tags'])}\">{i['title']}</A>
<DD>{i['description']}"""
)
fo.write(FOOTER)