50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
|
"""Linkding API code for FreshRSS2Pocket."""
|
||
|
|
||
|
import requests
|
||
|
import os.path
|
||
|
|
||
|
|
||
|
class Linkding:
|
||
|
"""Linkding API helper class."""
|
||
|
|
||
|
def __init__(self, baseurl, api_key):
|
||
|
self.baseurl = baseurl
|
||
|
self.api_key = api_key
|
||
|
|
||
|
def post_request(self, path, json):
|
||
|
res = requests.post(
|
||
|
f"{self.baseurl}/{path}",
|
||
|
headers={"Authorization": f"Token {self.api_key}"},
|
||
|
json=json,
|
||
|
timeout=15
|
||
|
)
|
||
|
|
||
|
if res.status_code >= 400:
|
||
|
raise ValueError("Response status codes indicates and error!")
|
||
|
|
||
|
return res.json()
|
||
|
|
||
|
def add(self, url, title="", tag_names=""):
|
||
|
"""Add URL to Linkding."""
|
||
|
json = {
|
||
|
"url": url,
|
||
|
"title": "",
|
||
|
"description": "",
|
||
|
"notes": "",
|
||
|
"is_archived": False,
|
||
|
"unread": False,
|
||
|
"shared": False,
|
||
|
"tag_names": []
|
||
|
}
|
||
|
|
||
|
if title:
|
||
|
json['title'] = title
|
||
|
|
||
|
if tag_names:
|
||
|
json['tag_names'] = tag_names.split(",")
|
||
|
|
||
|
self.post_request(
|
||
|
"api/bookmarks/",
|
||
|
json=json
|
||
|
)
|