Skip to content

Instantly share code, notes, and snippets.

@malys
Created January 22, 2025 19:05
Show Gist options
  • Save malys/1c60cb72bcc2d05ed2703edda72d4971 to your computer and use it in GitHub Desktop.
Save malys/1c60cb72bcc2d05ed2703edda72d4971 to your computer and use it in GitHub Desktop.
[NodeRed plublishing] #xonsh #publish #flow #node #autmation
[
{
"id":"b3f710d3656ec0bd",
"type":"exec",
"z":"73f57341a6bdef33",
"command":"rm -f /tmp/cookies.txt",
"addpay":"",
"append":"",
"useSpawn":"false",
"timer":"",
"winHide":false,
"oldrc":false,
"name":"",
"x":1417,
"y":1204,
"wires":[
[
],
[
],
[
"7134a0a01a78b0a4"
]
]
},
{
"id":"3870e85d9e363b6c",
"type":"inject",
"z":"73f57341a6bdef33",
"name":"",
"props":[
{
"p":"module_name",
"v":"@gregoriusrippenstein/node-red-contrib-introspection",
"vt":"str"
}
],
"repeat":"",
"crontab":"",
"once":false,
"onceDelay":0.1,
"topic":"",
"x":1254,
"y":1091,
"wires":[
[
"b3f710d3656ec0bd"
]
]
},
{
"id":"7134a0a01a78b0a4",
"type":"switch",
"z":"73f57341a6bdef33",
"name":"",
"property":"payload.code",
"propertyType":"msg",
"rules":[
{
"t":"eq",
"v":"0",
"vt":"num"
}
],
"checkall":"true",
"repair":false,
"outputs":1,
"x":1556,
"y":1277,
"wires":[
[
"d7bb19775d60ba5f"
]
]
},
{
"id":"d7bb19775d60ba5f",
"type":"change",
"z":"73f57341a6bdef33",
"name":"",
"rules":[
{
"t":"set",
"p":"payload",
"pt":"msg",
"to":"curl -s -c /tmp/cookies.txt -b /tmp/cookies.txt -XGET 'https://flows.nodered.org/add/node'",
"tot":"str"
}
],
"action":"",
"property":"",
"from":"",
"to":"",
"reg":false,
"x":1670,
"y":1332,
"wires":[
[
"329cb745ab148644"
]
]
},
{
"id":"329cb745ab148644",
"type":"exec",
"z":"73f57341a6bdef33",
"command":"",
"addpay":"payload",
"append":"",
"useSpawn":"false",
"timer":"",
"winHide":false,
"oldrc":false,
"name":"",
"x":1776,
"y":1393,
"wires":[
[
"8b4462e71d18d914"
],
[
],
[
]
]
},
{
"id":"8b4462e71d18d914",
"type":"switch",
"z":"73f57341a6bdef33",
"name":"",
"property":"rc.code",
"propertyType":"msg",
"rules":[
{
"t":"eq",
"v":"0",
"vt":"num"
}
],
"checkall":"true",
"repair":false,
"outputs":1,
"x":1928,
"y":1399,
"wires":[
[
"c447b660ffb100b7"
]
]
},
{
"id":"c447b660ffb100b7",
"type":"split",
"z":"73f57341a6bdef33",
"name":"",
"splt":"\\n",
"spltType":"str",
"arraySplt":1,
"arraySpltType":"len",
"stream":false,
"addname":"",
"x":2008,
"y":1343,
"wires":[
[
"24a6bdedc55adb51"
]
]
},
{
"id":"24a6bdedc55adb51",
"type":"switch",
"z":"73f57341a6bdef33",
"name":"",
"property":"payload",
"propertyType":"msg",
"rules":[
{
"t":"regex",
"v":"name=\"_csrf\" type=\"hidden\" value=\"(.+)\">",
"vt":"str",
"case":false
}
],
"checkall":"true",
"repair":false,
"outputs":1,
"x":2077,
"y":1289,
"wires":[
[
"e045b2b86c04bede"
]
]
},
{
"id":"e045b2b86c04bede",
"type":"change",
"z":"73f57341a6bdef33",
"name":"",
"rules":[
{
"t":"set",
"p":"payload",
"pt":"msg",
"to":"$substringBefore( $substringAfter($$.payload, \"value=\\\"\"), \"\\\">\")",
"tot":"jsonata"
}
],
"action":"",
"property":"",
"from":"",
"to":"",
"reg":false,
"x":2134,
"y":1235,
"wires":[
[
"5096c688782c5d5b"
]
]
},
{
"id":"5096c688782c5d5b",
"type":"change",
"z":"73f57341a6bdef33",
"name":"",
"rules":[
{
"t":"set",
"p":"data_string",
"pt":"msg",
"to":"\"module=\" & $$.module_name & \"&_csrf=\" & $$.payload",
"tot":"jsonata"
},
{
"t":"set",
"p":"payload",
"pt":"msg",
"to":"\"curl -v -XPOST 'https://flows.nodered.org/add/node' -c /tmp/cookies.txt -b /tmp/cookies.txt -H 'referer: https://flows.nodered.org/node/\" & $$.module_name & \"' -H 'Content-Type: application/x-www-form-urlencoded' -H 'x-requested-with: XMLHttpRequest' -d '\" & $$.data_string & \"'\"",
"tot":"jsonata"
}
],
"action":"",
"property":"",
"from":"",
"to":"",
"reg":false,
"x":2345,
"y":1325,
"wires":[
[
"4260147961b82058"
]
]
},
{
"id":"4260147961b82058",
"type":"exec",
"z":"73f57341a6bdef33",
"command":"",
"addpay":"payload",
"append":"",
"useSpawn":"false",
"timer":"",
"winHide":false,
"oldrc":false,
"name":"",
"x":2465,
"y":1455,
"wires":[
[
"f4dc513b63d2486a"
],
[
"3210b2328892fb83"
],
[
"2782388af7e4a3e3"
]
]
},
{
"id":"f4dc513b63d2486a",
"type":"debug",
"z":"73f57341a6bdef33",
"name":"debug 345",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"payload",
"targetType":"msg",
"statusVal":"",
"statusType":"auto",
"x":2639,
"y":1404,
"wires":[
]
},
{
"id":"3210b2328892fb83",
"type":"debug",
"z":"73f57341a6bdef33",
"name":"debug 346",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"true",
"targetType":"full",
"statusVal":"",
"statusType":"auto",
"x":2642,
"y":1455,
"wires":[
]
},
{
"id":"2782388af7e4a3e3",
"type":"debug",
"z":"73f57341a6bdef33",
"name":"debug 347",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"true",
"targetType":"full",
"statusVal":"",
"statusType":"auto",
"x":2639,
"y":1511,
"wires":[
]
}
]
# Automatic publish to nodered registry
# ex: xonsh publishToNodered.xsh @malysus/node-red-contrib-mqtt-auto
import re
def publishNode(moduleName,token):
print(f"Publishing node {moduleName}")
module=f"module={moduleName}&_csrf={token}"
result=$(curl -s -XPOST "https://flows.nodered.org/add/node" -c @(COOKIES_FILE) -b @(COOKIES_FILE) -H "referer: https://flows.nodered.org/add/node" -H "Content-Type: application/x-www-form-urlencoded" -H "x-requested-with: XMLHttpRequest" -d @(module))
print(result)
def publishFlow(moduleName):
print(f"Publishing flow {moduleName}")
base=f"https://flows.nodered.org/flow/{moduleName}"
url=f"{base}/refresh"
referrer=f"referer: {base}"
result=$(curl -v -s -XPOST @(url) -c @(COOKIES_FILE) -b @(COOKIES_FILE) -H @(referrer) -H "x-requested-with: XMLHttpRequest" -H 'accept: */*' -H 'accept-language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7' -H 'content-length: 0' -H 'origin: https://flows.nodered.org' -H 'priority: u=1, i' -H 'sec-ch-ua: "Chromium";v="131", "Not_A Brand";v="24"' -H 'sec-ch-ua-mobile: ?0' -H 'sec-ch-ua-platform: "Windows"' -H 'sec-fetch-dest: empty' -H 'sec-fetch-mode: cors' -H 'sec-fetch-site: same-origin' -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36')
print(result)
def getToken():
csfr=$(curl -s -c @(COOKIES_FILE) -b @(COOKIES_FILE) -XGET 'https://flows.nodered.org/add/node')
regex = r"_csrf\" type=\"hidden\" value=\"([^\"]+)\""
matches = list(re.finditer(regex, csfr, re.MULTILINE))
try:
token=matches[0].group(1)
except Exception as e:
print(e)
print(f"Token was found to be {token}")
return token
print("Publishing to nodered...")
MODULE_NAME=$ARG1
if "/" in MODULE_NAME:
COOKIES_FILE="cookies.txt"
touch @(COOKIES_FILE)
rm @(COOKIES_FILE)
publishNode(MODULE_NAME,getToken())
rm @(COOKIES_FILE)
else:
# TODO not working, How to manage cookies
COOKIES_FILE="COOKIES_WEB.txt"
publishFlow(MODULE_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment