Skip to content

Instantly share code, notes, and snippets.

@mdegat01
Last active December 26, 2021 07:30
Show Gist options
  • Save mdegat01/15e8ea5c00e824a63825ee563412a5c5 to your computer and use it in GitHub Desktop.
Save mdegat01/15e8ea5c00e824a63825ee563412a5c5 to your computer and use it in GitHub Desktop.
Flow to download youtube video
[
{
"id": "8e4aba9fe8f50732",
"type": "tab",
"label": "DL YT",
"disabled": false,
"info": "",
"env": []
},
{
"id": "6404f0ff6a765c9a",
"type": "http request",
"z": "8e4aba9fe8f50732",
"name": "Get Formats",
"method": "POST",
"ret": "obj",
"paytoqs": "ignore",
"url": "https://yt1s.com/api/ajaxSearch/index",
"tls": "3844a92b.fde956",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 510,
"y": 180,
"wires": [
[
"941ccf148cd8b3a8"
]
]
},
{
"id": "a8418d300a596c8a",
"type": "inject",
"z": "8e4aba9fe8f50732",
"name": "",
"props": [
{
"p": "yt_url",
"v": "https://www.youtube.com/watch?v=6oAeSCikow4",
"vt": "str"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 170,
"y": 180,
"wires": [
[
"1058166f472370e4"
]
]
},
{
"id": "1058166f472370e4",
"type": "change",
"z": "8e4aba9fe8f50732",
"name": "Set payload",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "'vt=home&q=' & $encodeUrlComponent(yt_url)",
"tot": "jsonata"
},
{
"t": "set",
"p": "headers",
"pt": "msg",
"to": "{\"Content-Type\":\"application/x-www-form-urlencoded; charset=UTF-8\"}",
"tot": "json"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 330,
"y": 180,
"wires": [
[
"6404f0ff6a765c9a"
]
]
},
{
"id": "941ccf148cd8b3a8",
"type": "function",
"z": "8e4aba9fe8f50732",
"name": "Find format",
"func": "const SIZE_PARSE = /^(\\d+(?:\\.\\d+)?) ([KM]B)$/;\nconst TIMESTAMP = (new Date()).toLocaleString(\"en-US\",\n { month: \"short\", day: \"numeric\", hour12: false, hour: \"numeric\", minute: \"numeric\" }\n);\n\nif (msg.statusCode >= 400){\n node.status({fill:\"red\",shape:\"ring\",text:`Error ${msg.statusCode} at ${TIMESTAMP}`});\n return [null, msg];\n}\n\nif (!msg.payload.links.mp4){\n node.status({ fill: \"red\", shape: \"ring\", text: `No mp4 formats at ${TIMESTAMP}`});\n return [null, msg];\n}\n\n// Find the largest mp4 format that is below 20MB max\nmsg.format = Object.values(msg.payload.links.mp4)\n .filter(function(value){\n if(value.size.endsWith('KB')){\n return true;\n }\n if(value.size.endsWith('MB')){\n let mbs = value.size.replace(SIZE_PARSE, '$1');\n return +(mbs) <= 20;\n }\n })\n .reduce(function(prev, curr){\n if(!prev){\n return curr;\n }\n let prevSize = SIZE_PARSE.exec(prev.size);\n let currSize = SIZE_PARSE.exec(curr.size);\n if(prevSize[2] !== currSize[2]){\n return prevSize[2] === 'MB' ? prev : curr;\n }\n return +(prevSize[1]) < +(currSize[1]) ? curr : prev;\n })\n;\n\n\nlet vid_id = new url.URL(msg.yt_url).searchParams.get('v');\nmsg.payload = `vid=${encodeURIComponent(vid_id)}&k=${encodeURIComponent(msg.format.k)}`\nmsg.headers = {\n \"Content-Type\": \"application/x-www-form-urlencoded; charset=UTF-8\"\n};\nnode.status({ fill: \"green\", shape: \"dot\", text: `${vid_id} (${msg.format.size}) at ${TIMESTAMP}`});\n\nreturn [msg, null];",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "url",
"module": "url"
}
],
"x": 690,
"y": 180,
"wires": [
[
"689946c0a9da09d8"
],
[
"1b25200a7673df10"
]
],
"outputLabels": [
"Success",
"Error"
]
},
{
"id": "689946c0a9da09d8",
"type": "http request",
"z": "8e4aba9fe8f50732",
"name": "",
"method": "POST",
"ret": "obj",
"paytoqs": "ignore",
"url": "https://yt1s.com/api/ajaxConvert/convert",
"tls": "3844a92b.fde956",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 870,
"y": 180,
"wires": [
[
"96edcc5059e01b1c"
]
]
},
{
"id": "96edcc5059e01b1c",
"type": "switch",
"z": "8e4aba9fe8f50732",
"name": "Error?",
"property": "statusCode",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "400",
"vt": "num"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 1030,
"y": 180,
"wires": [
[
"d9b7032e97666040",
"53786536ac3cfa22"
],
[
"1b25200a7673df10"
]
],
"outputLabels": [
"Success",
"Error"
]
},
{
"id": "1b25200a7673df10",
"type": "debug",
"z": "8e4aba9fe8f50732",
"name": "Error debug",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1210,
"y": 280,
"wires": []
},
{
"id": "53ed95e65bfed14e",
"type": "http request",
"z": "8e4aba9fe8f50732",
"name": "DL video",
"method": "GET",
"ret": "bin",
"paytoqs": "ignore",
"url": "",
"tls": "3844a92b.fde956",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 1360,
"y": 160,
"wires": [
[
"e74c2a16bd863a13"
]
]
},
{
"id": "d9b7032e97666040",
"type": "change",
"z": "8e4aba9fe8f50732",
"name": "Set request",
"rules": [
{
"t": "set",
"p": "url",
"pt": "msg",
"to": "payload.dlink",
"tot": "msg"
},
{
"t": "set",
"p": "filename",
"pt": "msg",
"to": "'/share/' & $replace(payload.title, /\\W+/, '_') & '.' & payload.ftype\t",
"tot": "jsonata"
},
{
"t": "delete",
"p": "payload",
"pt": "msg"
},
{
"t": "delete",
"p": "headers",
"pt": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1210,
"y": 160,
"wires": [
[
"53ed95e65bfed14e"
]
]
},
{
"id": "53786536ac3cfa22",
"type": "debug",
"z": "8e4aba9fe8f50732",
"name": "Success (with DL link)",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1240,
"y": 120,
"wires": []
},
{
"id": "e74c2a16bd863a13",
"type": "function",
"z": "8e4aba9fe8f50732",
"name": "Write out file",
"func": "const TIMESTAMP = (new Date()).toLocaleString(\"en-US\",\n { month: \"short\", day: \"numeric\", hour12: false, hour: \"numeric\", minute: \"numeric\" }\n);\n\nif(msg.statusCode < 400){\n node.status({fill:\"green\",shape:\"dot\",text:`Success at ${TIMESTAMP}`});\n fs.writeFile(msg.filename, msg.payload, 'binary', function (err) {\n node.status({fill:\"red\",shape:\"ring\",text:`File write error at ${TIMESTAMP}`});\n node.send(msg);\n });\n}\nelse{\n node.status({fill:\"red\",shape:\"ring\",text:`API Error ${msg.statusCode} at ${TIMESTAMP}`});\n return msg;\n}\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "fs",
"module": "fs"
}
],
"x": 1510,
"y": 160,
"wires": [
[]
],
"outputLabels": [
"Error"
]
},
{
"id": "3844a92b.fde956",
"type": "tls-config",
"name": "General SSL",
"cert": "",
"key": "",
"ca": "",
"certname": "",
"keyname": "",
"caname": "",
"servername": "",
"verifyservercert": true
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment