You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
13 KiB
13 KiB
<html>
<head>
</head>
</html>
curl --request GET \ --url https://demo.linkace.org/api/v1/links \ --header 'Authorization: Bearer undefined' \ --header 'Content-Type: application/json' \ --header 'accept: application/json'
In [ ]:
Pull links from Linkace¶
In [1]:
import json
import os
import pprint
import urllib.parse
import sys
import requests
my_headers = {
"Authorization": os.env(LINKACE_TOKEN),
"Content-Type": "application/json",
"Accept": "application/json",
}
response = requests.get(
"http://linkace/api/v1/links",
headers=my_headers,
)
initial = response.json()
last = initial['last_page']
links = []
for x in range(1, last + 1):
response = requests.get(
"http://linkace/api/v1/links?page=" + str(x),
headers=my_headers,
)
result = response.json()
data = result['data']
links = links + data
out_file = open("links.json", "w")
json.dump(links, out_file, indent = 2)
out_file.close()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [1], in <cell line: 9>() 5 import sys 6 import requests 8 my_headers = { ----> 9 "Authorization": os.env(LINKACE_TOKEN), 10 "Content-Type": "application/json", 11 "Accept": "application/json", 12 } 14 response = requests.get( 15 "http://linkace/api/v1/links", 16 headers=my_headers, 17 ) 19 initial = response.json() AttributeError: module 'os' has no attribute 'env'
Update Links in Directus¶
In [7]:
for link in links:
url = link['url']
myid = link.pop("id")
parsed_url = urllib.parse.urlparse(url)
domain = parsed_url.netloc
pdom = domain.replace("www.", "")
link['domain'] = pdom
headers = {
"Authorization": "os.env(directus_token)",
"Content-Type": "application/json",
}
response = requests.patch(
"http://directus:8055/items/bookmarks/" + str(myid),
headers=headers,
json=(link),
)
print(response.json())
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) File /opt/conda/lib/python3.9/site-packages/requests/models.py:910, in Response.json(self, **kwargs) 909 try: --> 910 return complexjson.loads(self.text, **kwargs) 911 except JSONDecodeError as e: 912 # Catch JSON-related errors and raise as requests.JSONDecodeError 913 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError File /opt/conda/lib/python3.9/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 343 if (cls is None and object_hook is None and 344 parse_int is None and parse_float is None and 345 parse_constant is None and object_pairs_hook is None and not kw): --> 346 return _default_decoder.decode(s) 347 if cls is None: File /opt/conda/lib/python3.9/json/decoder.py:337, in JSONDecoder.decode(self, s, _w) 333 """Return the Python representation of ``s`` (a ``str`` instance 334 containing a JSON document). 335 336 """ --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end() File /opt/conda/lib/python3.9/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx) 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: JSONDecodeError Traceback (most recent call last) Input In [7], in <cell line: 1>() 8 headers = { 9 "Authorization": "os.env(directus_token)", 10 "Content-Type": "application/json", 11 } 12 response = requests.patch( 13 "http://directus:8055/items/bookmarks/" + str(myid), 14 headers=headers, 15 json=(link), 16 ) ---> 17 print(response.json()) File /opt/conda/lib/python3.9/site-packages/requests/models.py:917, in Response.json(self, **kwargs) 915 raise RequestsJSONDecodeError(e.message) 916 else: --> 917 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: [Errno Expecting value] : 0
In [ ]:
import urllib.parse
import sys
link = links[1]
url = link['url']
myid = link.pop("id")
print(link)
parsed_url = urllib.parse.urlparse(url)
domain = parsed_url.netloc
pdom = domain.replace("www.", "")
print(pdom)
link['domain'] = pdom
print(link)
print(myid)