nadimkobeissi / mkbsd

Download all the wallpapers in MKBHD's "Panels" app
Do What The F*ck You Want To Public License
2.43k stars 180 forks source link

powershell #33

Open nascentt opened 1 day ago

nascentt commented 1 day ago

I dont have python or node.js installed so i ported the code to powershell, which comes with windows.

function Main {
    $url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'

    try {
        # Fetching JSON data
        $response = Invoke-RestMethod -Uri $url
        if ($null -eq $response) {
            throw "Failed to fetch JSON file"
        }
        $data = $response.data
        if ($null -eq $data) {
            throw "JSON does not have a 'data' property at its root."
        }
        # Create download directory
        $downloadDir = Join-Path -Path $PSScriptRoot -ChildPath 'downloads'
        if (-not (Test-Path $downloadDir)) {
            New-Item -ItemType Directory -Path $downloadDir | Out-Null
            Write-Host "Created directory: $downloadDir"
        }
        $fileIndex = 1
        # Iterate through each property in the $data object
        $data | Get-Member -MemberType NoteProperty | ForEach-Object {
            $key = $_.Name
            $subproperty = $data.$key

            # Check if the 'dhd' property exists in the subproperty
            if ($null -ne $subproperty.dhd) {
                $imageUrl = $subproperty.dhd
                Write-Host "Found image URL for key $key!"
                # Delay between requests
                Start-Sleep -Milliseconds 100
                # Get file extension and construct file path
                $ext = [System.IO.Path]::GetExtension((New-Object Uri($imageUrl)).AbsolutePath)
                if ([string]::IsNullOrEmpty($ext)) { 
                    $ext = '.jpg' 
                }
                $filename = "$fileIndex$ext"
                $filePath = Join-Path -Path $downloadDir -ChildPath $filename

                # Download and save the image
                DownloadImage -url $imageUrl -filePath $filePath
                Write-Host "Saved image to $filePath"
                # Increment file index and delay
                $fileIndex++
                Start-Sleep -Milliseconds 250
            }
        }
    } catch {
        Write-Error "Error: $_"
    }
}

function DownloadImage {
    param ($url, $filePath)

    try {
        # Downloading image
        $imageData = Invoke-RestMethod -Uri $url -Method Get -OutFile $filePath
    }
    catch {
        throw "Failed to download image: $_"
    }
}

function AsciiArt {
    Write-Host @'
 /$$      /$$ /$$   /$$ /$$$$$$$   /$$$$$$  /$$$$$$$
| $$$    /$$$| $$  /$$/| $$__  $$ /$$__  $$| $$__  $$
| $$$$  /$$$$| $$ /$$/ | $$  \ $$| $$  \__/| $$  \ $$
| $$ $$/$$ $$| $$$$$/  | $$$$$$$ |  $$$$$$ | $$  | $$
| $$  $$$| $$| $$  $$  | $$__  $$ \____  $$| $$  | $$
| $$\  $ | $$| $$\  $$ | $$  \ $$ /$$  \ $$| $$  | $$
| $$ \/  | $$| $$ \  $$| $$$$$$$/|  $$$$$$/| $$$$$$$/
|__/     |__/|__/  \__/|_______/  \______/ |_______/
'@
    Write-Host ""
    Write-Host "Starting downloads from your favorite sellout grifter's wallpaper app..."
}

AsciiArt
Start-Sleep -Seconds 5
Main
0xvisualiris commented 1 day ago

Haha, did the same lol