Torph
Torph

Reputation: 33

Any way to download latest GitHub release w/ a batch script?

So, I'm trying to download the latest release from GitHub using a Windows batch script. I can get a long list of URLs by running curl -s https://api.github.com/repos/ActualMandM/cemu_graphic_packs/releases/latest, but I can't figure out how to pass the "browser_download_url": "https://github.com/ActualMandM/cemu_graphic_packs/releases/download/Github828/graphicPacks828.zip" it outputs to curl. I've looked online, but everything I found was for PowerShell and most of them used wget.

Upvotes: 3

Views: 2585

Answers (3)

rzickler
rzickler

Reputation: 747

I extended the answer from @SomethingDark adding the option to specify a specific asset for the download. This is useful if you don't need all available assets. So you could define SET target_name_match=.zip to only download assets that have ".zip" in their filename.

I also fixed the non existing repository and replaced find with findstr. The latter was needed to fix a bug saying find: browser_download_url: No such file or directory prevents a mixup between Windows- and GNU-find.exe (e.g. due to MSYS2).

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM Specify the target repository
SET github_user=crate-ci
SET github_repo=typos

REM If you just want a specific version: specify a unique part of the file name
REM If you want to download all available assets: Delete after `=`
SET target_name_match=pc-windows-msvc.zip

FOR /f "tokens=1,* delims=:" %%A IN ('curl -ks https://api.github.com/repos/%github_user%/%github_repo%/releases/latest ^| findstr "browser_download_url"') DO (
    SET url=%%B
    IF NOT "!url:%target_name_match%=!"=="!url!" (
        ECHO Downloading !url!
        curl -kOL !url!
    )
)

Further extension: Archive extraction

If you also want to extract the just downloaded asset, extend the for loop as show below.
I find it quite handy to quickly update some tools in one go.

Note 1: This has just been tested with ZIP files
Note 2: This does probably just make sense if you download a single asset
Note 3: Make sure to have 7-Zip in the PATH environment variable or specify the absolute path to the 7z.exe. (Or use a different tool for extraction)
Note 4: The extraction will override all existing files of the archive without a prompt (-aoa)!

FOR /f "tokens=1,* delims=:" %%A IN ('curl -ks https://api.github.com/repos/%github_user%/%github_repo%/releases/latest ^| findstr "browser_download_url"') DO (
SET url=%%B
IF NOT "!url:%target_name_match%=!"=="!url!" (
    ECHO Downloading !url!
    curl -kOL !url!

    REM Get the filename of the just downloaded asset
    FOR %%F IN (!url!) DO (
        SET download_filename=%%~nxF
    )
    REM It is required to have 7zip in the PATH or you need to
    REM specify the full path to the 7z.exe
    7z x -aoa !download_filename!
    )
)

Upvotes: 1

SomethingDark
SomethingDark

Reputation: 14325

If you really want to use batch for this, you'll have to search the output JSON for the value you're looking for and then process that string. If the JSON had appeared all on one line, you'd need to take a different approach, but you got lucky.

for /f "tokens=1,* delims=:" %%A in ('curl -kLs https://api.github.com/repos/ActualMandM/cemu_graphic_packs/releases/latest ^| find "browser_download_url"') do (
    curl -kOL %%B
)

I've added the -k flag because my computer requires it for some reason (so other peoples' might as well).

-O will set the name of the output file to the remote output file name

-L follows a redirect, which is required for downloading from Github.

Upvotes: 3

Reino
Reino

Reputation: 3443

The Github API url you're accessing returns JSON, so you're going to need a JSON parser.
I can highly recommend . xidel can open and download urls, so you won't need curl or a batch-script.

To query the "browser_download_url"-attribute:

xidel.exe -s "https://api.github.com/repos/ActualMandM/cemu_graphic_packs/releases/latest" -e "$json//browser_download_url"
https://github.com/ActualMandM/cemu_graphic_packs/releases/download/Github874/graphicPacks874.zip

(or -e "$json/(assets)()/browser_download_url" in full)

To download 'graphicPacks874.zip' in the current dir:

xidel.exe ^
-s "https://api.github.com/repos/ActualMandM/cemu_graphic_packs/releases/latest" ^
-f "$json//browser_download_url" ^
--download "{substring-after($headers[starts-with(.,'Content-Disposition')],'filename=')}"

With r8389 or newer (because of this commit) you can just use --download ..

Upvotes: 1

Related Questions