Last active
August 7, 2022 21:25
-
-
Save GhostTypes/880e0f94c6693acaec84f66a464d6109 to your computer and use it in GitHub Desktop.
Batch download mc mods from Github (powershell 1-click)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $gh_token = "" | |
| $gh_header = @{'authorization' = {$gh_token}} | |
| $global:progressPreference = 'silentlyContinue' | |
| $has_modf = $true | |
| if ($gh_token) { | |
| } else { | |
| Write-Host -ForegroundColor Red "Github token missing!" | |
| Write-Host -ForegroundColor Yellow "Create a key at https://github.com/settings/tokens" | |
| Write-Host -ForegroundColor Yellow "then paste the key into the first line of this script (gh_token)" | |
| return | |
| } | |
| function GithubDownload { | |
| param ( [Parameter()][string]$author_name, [Parameter()][string]$repo_name) | |
| Write-Host "Trying to download" $repo_name | |
| $download_url = "https://api.github.com/repos/$author_name/$repo_name/releases/latest" #will not always work because of custom tags | |
| $json = $null | |
| try { | |
| $json = Invoke-RestMethod -Uri $download_url | |
| } | |
| catch { #have to cope and find the right tag | |
| Write-Host -ForegroundColor Yellow "default 'latest' tag not found, trying to auto-detect" | |
| try { | |
| Write-Host "Checking release tags..." | |
| $tag_data = Invoke-RestMethod -Uri "https://api.github.com/repos/$author_name/$repo_name/tags" #get all release tags | |
| if ($tag_data.name.length -gt 1) { | |
| Write-Host -ForegroundColor Yellow "Found" $tag_data.name.length "release tags, you may want to touble check" | |
| } | |
| $tag = $tag_data.name[0] #check the first one | |
| if ($tag) { | |
| Write-Host -ForegroundColor Yellow "Using" $tag "tag" | |
| $download_url = "https://api.github.com/repos/$author_name/$repo_name/releases/tags/$tag" | |
| } | |
| } | |
| catch { | |
| Write-Host -ForegroundColor Red "Failed to auto-detect proper release tag." | |
| return | |
| } | |
| try { #re-request release data from the custom tag | |
| $json = Invoke-RestMethod -Uri $download_url -Headers $gh_header | |
| } catch { | |
| Write-Host -ForegroundColor Red "Downloading from" $tag "tag failed, skipping." | |
| return | |
| } | |
| } | |
| $assets = $json.assets | |
| $asset = $json.assets[0] | |
| $dl_url = $asset.browser_download_url | |
| $dl_name = $asset.name | |
| if ($assets.length -gt 1) { | |
| Write-Host -ForegroundColor Yellow $repo_name "has multiple jars in the release, trying to auto-detect" | |
| foreach ($asset in $assets) { | |
| if ($asset.name -like "*sources.jar" -or $asset.name -like "*all.jar") { | |
| continue | |
| } else { | |
| $dl_url = $asset.browser_download_url | |
| $dl_name = $asset.name | |
| if ($dl_url -and $dl_name) { | |
| Write-Host -ForegroundColor Green "Auto-detected correct jar at" $dl_url | |
| break | |
| } | |
| } | |
| } | |
| } | |
| Write-Host "Downloading" $dl_name | |
| try { | |
| if ($has_modf -eq $true) { | |
| Invoke-WebRequest -Uri $dl_url -OutFile $mods_path\$dl_name | |
| #Write-Host -ForegroundColor Green "Saved to" $mods_path\$dl_name | |
| } else { | |
| Invoke-WebRequest -Uri $dl_url -OutFile $PWD\$dl_name | |
| #Write-Host -ForegroundColor Green "Saved to current folder." | |
| } | |
| } | |
| catch { | |
| Write-Host -ForegroundColor Red "Download error." | |
| } | |
| } | |
| Clear-Host | |
| if (Test-Path $PWD\mods.txt) { | |
| } else { | |
| Write-Host -ForegroundColor Red "mods.txt not in folder!" | |
| Write-Host -ForegroundColor Red "You need to make a list of mods to download first" | |
| return | |
| } | |
| $mc_path = $env:APPDATA + "\.minecraft" | |
| $mods_path = $mc_path + "\mods" | |
| if (Test-Path $mc_path) { | |
| if (Test-Path $mods_path) { | |
| } else { | |
| Write-Host -ForegroundColor Yellow ".mods folder not found, creating now." | |
| mkdir -Path $env:APPDATA\.mc\mods | |
| } | |
| } else { | |
| Write-Host -ForegroundColor Yellow ".minecraft folder not found, mods will download to the current folder." | |
| $has_modf = $false | |
| } | |
| foreach ($line in Get-Content .\mods.txt) { | |
| $md = $line.Split("/") | |
| GithubDownload -repo_name $md[1] -author_name $md[0] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| astei/lazydfu | |
| CaffeineMC/sodium-fabric | |
| UltimateBoomer/Resolution-Control |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment