-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Davide Arcuri
committed
Oct 31, 2024
1 parent
ddb8e46
commit 2217811
Showing
8 changed files
with
347 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"import getpass\n", | ||
"from requests import Session\n", | ||
"from pprint import pprint\n", | ||
"import urllib3\n", | ||
"urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n", | ||
"\n", | ||
"url = \"https://localhost\"\n", | ||
"user = input()\n", | ||
"password = getpass.getpass()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# LOGIN" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"session = Session()\n", | ||
"\n", | ||
"first = session.get(f\"{url}\", verify=False)\n", | ||
"csrftoken = first.cookies[\"csrftoken\"]\n", | ||
"\n", | ||
"data = json.dumps(\n", | ||
" {\"username\": user, \"password\": password, \"csrfmiddlewaretoken\": csrftoken}\n", | ||
")\n", | ||
"\n", | ||
"headers = {\n", | ||
" \"X-CSRFToken\": first.headers[\"Set-Cookie\"].split(\"=\")[1].split(\";\")[0],\n", | ||
" \"Referer\": url,\n", | ||
" \"X-Requested-With\": \"XMLHttpRequest\",\n", | ||
"}\n", | ||
"\n", | ||
"req = session.post(\n", | ||
" f\"{url}/api/auth/\", data=data, cookies=first.cookies, headers=headers, verify=False\n", | ||
")\n", | ||
"if req.status_code != 200:\n", | ||
" print(req.text)\n", | ||
" exit(1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# GET CUSTOM RULE LIST" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"rules = session.get(f\"{url}/api/customrules/?start=0&length=1&draw=0\", verify=False).json()\n", | ||
"print(f\"{len(rules['data'])} rules returned of {rules['recordsTotal']}\")\n", | ||
"if len(rules['data']) > 0: \n", | ||
" pprint(rules['data'][0])\n", | ||
" rule_pk = rules['data'][0]['id']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# CREATE CUSTOM RULE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data = {\n", | ||
" \"rule_ids\": [2, 4], # Rule Id to be merged in custom rule \n", | ||
" \"rulename\": \"combined_rule\"\n", | ||
"}\n", | ||
"res = session.post(f\"{url}/api/rules/build\", json=data, cookies=first.cookies, headers=headers, verify=False)\n", | ||
"if res.status_code == 200:\n", | ||
" pprint(res.json())\n", | ||
"else:\n", | ||
" print(res.status_code, res.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# EDIT CUSTOM RULE (make default)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"res = session.post(f\"{url}/api/customrules/{rule_pk}/default\", cookies=first.cookies, headers=headers, verify=False)\n", | ||
"if res.status_code == 200:\n", | ||
" pprint(res.json())\n", | ||
"else:\n", | ||
" print(res.status_code, res.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# PUBLISH CUSTOM RULE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data = {\n", | ||
" \"rule_ids\": [rule_pk], \n", | ||
" \"action\": \"Publish\"\n", | ||
"}\n", | ||
"res = session.post(f\"{url}/api/customrules/publish\", json=data, cookies=first.cookies, headers=headers, verify=False)\n", | ||
"if res.status_code == 200:\n", | ||
" pprint(res.json())\n", | ||
"else:\n", | ||
" print(res.status_code, res.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# DOWNLOAD RULE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"res = session.get(f\"{url}/api/customrules/{rule_pk}/download\", cookies=first.cookies, headers=headers, verify=False)\n", | ||
"if res.status_code == 200:\n", | ||
" pprint(res.text)\n", | ||
"else:\n", | ||
" print(res.status_code, res.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# DELETE CUSTOM RULE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data = {\n", | ||
" \"rule_ids\": [rule_pk]\n", | ||
"}\n", | ||
"res = session.delete(f\"{url}/api/customrules/\", json=data, cookies=first.cookies, headers=headers, verify=False)\n", | ||
"print(res.status_code, res.text) " | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"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.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.