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.
198 lines
11 KiB
198 lines
11 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "markdown", |
|
"id": "5a2a0654", |
|
"metadata": {}, |
|
"source": [ |
|
"curl --request GET \\\n", |
|
" --url https://demo.linkace.org/api/v1/links \\\n", |
|
" --header 'Authorization: Bearer undefined' \\\n", |
|
" --header 'Content-Type: application/json' \\\n", |
|
" --header 'accept: application/json'" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "60990b52", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "ebef409e-ddc1-4cc4-bd4d-a4839ba62d66", |
|
"metadata": {}, |
|
"source": [ |
|
"## Pull links from Linkace" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 6, |
|
"id": "02d6d963-538a-47c1-957f-06c7ea7c7497", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"import json\n", |
|
"import os\n", |
|
"import pprint\n", |
|
"import urllib.parse\n", |
|
"import sys\n", |
|
"import requests\n", |
|
"\n", |
|
"my_headers = {\n", |
|
" \"Authorization\": os.env(LINKACE_TOKEN),\n", |
|
" \"Content-Type\": \"application/json\",\n", |
|
" \"Accept\": \"application/json\",\n", |
|
"}\n", |
|
"\n", |
|
"response = requests.get(\n", |
|
" \"http://linkace/api/v1/links\",\n", |
|
" headers=my_headers,\n", |
|
")\n", |
|
"\n", |
|
"initial = response.json()\n", |
|
"\n", |
|
"last = initial['last_page']\n", |
|
"\n", |
|
"links = []\n", |
|
"\n", |
|
"for x in range(1, last + 1):\n", |
|
" response = requests.get(\n", |
|
" \"http://linkace/api/v1/links?page=\" + str(x),\n", |
|
" headers=my_headers,\n", |
|
" )\n", |
|
" result = response.json()\n", |
|
" data = result['data']\n", |
|
" links = links + data\n", |
|
" \n", |
|
"out_file = open(\"links.json\", \"w\")\n", |
|
"json.dump(links, out_file, indent = 2)\n", |
|
"out_file.close()" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "e6a27401-cc7d-4ffe-834d-2f051dce7025", |
|
"metadata": {}, |
|
"source": [ |
|
"## Update Links in Directus" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 7, |
|
"id": "f2b300a9-f7c0-4863-9696-29e92e6cedc8", |
|
"metadata": { |
|
"scrolled": true, |
|
"tags": [] |
|
}, |
|
"outputs": [ |
|
{ |
|
"ename": "JSONDecodeError", |
|
"evalue": "[Errno Expecting value] : 0", |
|
"output_type": "error", |
|
"traceback": [ |
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
|
"\u001b[0;31mJSONDecodeError\u001b[0m Traceback (most recent call last)", |
|
"File \u001b[0;32m/opt/conda/lib/python3.9/site-packages/requests/models.py:910\u001b[0m, in \u001b[0;36mResponse.json\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 909\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 910\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcomplexjson\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloads\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 911\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m JSONDecodeError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 912\u001b[0m \u001b[38;5;66;03m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[1;32m 913\u001b[0m \u001b[38;5;66;03m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n", |
|
"File \u001b[0;32m/opt/conda/lib/python3.9/json/__init__.py:346\u001b[0m, in \u001b[0;36mloads\u001b[0;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[1;32m 343\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m 344\u001b[0m parse_int \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m parse_float \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m 345\u001b[0m parse_constant \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_pairs_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kw):\n\u001b[0;32m--> 346\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_default_decoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 347\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", |
|
"File \u001b[0;32m/opt/conda/lib/python3.9/json/decoder.py:337\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[0;34m(self, s, _w)\u001b[0m\n\u001b[1;32m 333\u001b[0m \u001b[38;5;124;03m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001b[39;00m\n\u001b[1;32m 334\u001b[0m \u001b[38;5;124;03mcontaining a JSON document).\u001b[39;00m\n\u001b[1;32m 335\u001b[0m \n\u001b[1;32m 336\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m--> 337\u001b[0m obj, end \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraw_decode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43midx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_w\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mend\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 338\u001b[0m end \u001b[38;5;241m=\u001b[39m _w(s, end)\u001b[38;5;241m.\u001b[39mend()\n", |
|
"File \u001b[0;32m/opt/conda/lib/python3.9/json/decoder.py:355\u001b[0m, in \u001b[0;36mJSONDecoder.raw_decode\u001b[0;34m(self, s, idx)\u001b[0m\n\u001b[1;32m 354\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m--> 355\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m JSONDecodeError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExpecting value\u001b[39m\u001b[38;5;124m\"\u001b[39m, s, err\u001b[38;5;241m.\u001b[39mvalue) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n\u001b[1;32m 356\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m obj, end\n", |
|
"\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)", |
|
"\nDuring handling of the above exception, another exception occurred:\n", |
|
"\u001b[0;31mJSONDecodeError\u001b[0m Traceback (most recent call last)", |
|
"Input \u001b[0;32mIn [7]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m headers \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 9\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAuthorization\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mos.env(directus_token)\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 10\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mContent-Type\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapplication/json\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 11\u001b[0m }\n\u001b[1;32m 12\u001b[0m response \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mpatch(\n\u001b[1;32m 13\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttp://directus:8055/items/bookmarks/\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mstr\u001b[39m(myid),\n\u001b[1;32m 14\u001b[0m headers\u001b[38;5;241m=\u001b[39mheaders,\n\u001b[1;32m 15\u001b[0m json\u001b[38;5;241m=\u001b[39m(link),\n\u001b[1;32m 16\u001b[0m )\n\u001b[0;32m---> 17\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjson\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n", |
|
"File \u001b[0;32m/opt/conda/lib/python3.9/site-packages/requests/models.py:917\u001b[0m, in \u001b[0;36mResponse.json\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 915\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m RequestsJSONDecodeError(e\u001b[38;5;241m.\u001b[39mmessage)\n\u001b[1;32m 916\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 917\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m RequestsJSONDecodeError(e\u001b[38;5;241m.\u001b[39mmsg, e\u001b[38;5;241m.\u001b[39mdoc, e\u001b[38;5;241m.\u001b[39mpos)\n", |
|
"\u001b[0;31mJSONDecodeError\u001b[0m: [Errno Expecting value] : 0" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"for link in links:\n", |
|
" url = link['url']\n", |
|
" myid = link.pop(\"id\")\n", |
|
" parsed_url = urllib.parse.urlparse(url)\n", |
|
" domain = parsed_url.netloc\n", |
|
" pdom = domain.replace(\"www.\", \"\")\n", |
|
" link['domain'] = pdom\n", |
|
" headers = {\n", |
|
" \"Authorization\": \"os.env(directus_token)\",\n", |
|
" \"Content-Type\": \"application/json\",\n", |
|
" }\n", |
|
" response = requests.patch(\n", |
|
" \"http://directus:8055/items/bookmarks/\" + str(myid),\n", |
|
" headers=headers,\n", |
|
" json=(link),\n", |
|
" )\n", |
|
" print(response.json())" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "b9d5bb9f-be1e-4db2-b0a3-d0d179e6b6a6", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"import urllib.parse\n", |
|
"import sys\n", |
|
"\n", |
|
"link = links[1]\n", |
|
"url = link['url']\n", |
|
"myid = link.pop(\"id\")\n", |
|
"print(link)\n", |
|
"parsed_url = urllib.parse.urlparse(url)\n", |
|
"domain = parsed_url.netloc\n", |
|
"pdom = domain.replace(\"www.\", \"\")\n", |
|
"print(pdom)\n", |
|
"link['domain'] = pdom\n", |
|
"\n", |
|
"\n", |
|
"print(link)\n", |
|
"\n", |
|
"print(myid)" |
|
] |
|
} |
|
], |
|
"metadata": { |
|
"kernelspec": { |
|
"display_name": "Python 3.8.9 64-bit", |
|
"language": "python", |
|
"name": "python3" |
|
}, |
|
"language_info": { |
|
"codemirror_mode": { |
|
"name": "ipython", |
|
"version": 3 |
|
}, |
|
"file_extension": ".py", |
|
"mimetype": "text/x-python", |
|
"name": "python", |
|
"nbconvert_exporter": "python", |
|
"pygments_lexer": "ipython3", |
|
"version": "3.10.4" |
|
}, |
|
"toc-autonumbering": false, |
|
"toc-showcode": false, |
|
"toc-showtags": false, |
|
"vscode": { |
|
"interpreter": { |
|
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" |
|
} |
|
}, |
|
"widgets": { |
|
"application/vnd.jupyter.widget-state+json": { |
|
"state": {}, |
|
"version_major": 2, |
|
"version_minor": 0 |
|
} |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 5 |
|
}
|
|
|