anon
anon

Reputation:

I am making a Discord bot that needs some Powershell code translated into Javascript

I found this code (Powershell) somewhere online that gives you all the video URL's of a Youtube playlist. That is exactly what I was looking for but I need to use it in Javascript. I don't know how to code in Powershell so that is why I am asking it here.

I am going to use this code in my Discord bot to play music. It will take a Youtube playlist and put all the URL's into the queue. I already have the code for playing individual videos.

I have already tried using a plugin for Javascript called 'node-powershell'. This plugin runs Powershell code in Javascript. This method did not work for me.

$Playlist = ((Invoke-WebRequest "https://www.youtube.com/playlist?list=PLvl4I7g6dfPRLP6etCkU8ZsIben8PWBmI").Links | Where {$_.class -match "pl-video-title-link"}).href ForEach ($Video in $Playlist) { $s ="https://www.youtube.com" + $Video $s
=$s.Substring(0, $s.IndexOf('&'))   Write-Output ($s) }

The 'node-powershell' method gave me errors. So I want that Powershell code translated into Javascript.

Upvotes: 0

Views: 678

Answers (1)

Adam
Adam

Reputation: 4168

I threw-up in my mouth a little reading this question. The same part of me that tells kids not to use dirty needles is urging me to tell you to just convert the Powershell to JavaScript. That said, to your question:

const { spawn } = require("child_process");

const cmd = `$Playlist = ((Invoke-WebRequest "https://www.youtube.com/playlist?list=PLvl4I7g6dfPRLP6etCkU8ZsIben8PWBmI").Links | Where {$_.class -match "pl-video-title-link"}).href; ForEach ($Video in $Playlist) { $s ="https://www.youtube.com" + $Video; $s=$s.Substring(0, $s.IndexOf('&')); Write-Output ($s); }`

const cp = spawn('C:\\Program Files\\PowerShell\\6\\pwsh.exe',['-Command', cmd]);
cp.stdout.on("data", (data) => { console.log("Data: " + data); } );
cp.stderr.on("data", (data) => { console.log("Errors: " + data); } );
cp.stdin.end();
cp.on("exit", () => { console.log("Powershell Script finished"); });
cp.stdin.end(); //end input

Upvotes: 1

Related Questions