Last active
May 22, 2019 07:15
-
-
Save nyanshiba/cd3f1ecf4c55718a4c92140d657efb42 to your computer and use it in GitHub Desktop.
optifine.netの更新をDiscordにWebhookで通知するPowershellスクリプト
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
| #190424 | |
| #for pwsh linux | |
| #requires -version 6.1 | |
| #$PSName = "$(Split-Path $PSCommandPath -Parent)/$(Split-Path $PSCommandPath -Leafbase)" | |
| $PSName = $PSCommandPath.Replace('.ps1','') | |
| #投稿関数 | |
| function Post-Discord($i) | |
| { | |
| $Content = | |
| @" | |
| **$($i.File) Released.** | |
| Download Link: | |
| <$($i.Mirror)> | |
| ChangeLog: | |
| <$(($i.Changelog).Replace('about:','http://optifine.net/'))> | |
| "@ | |
| $Content | |
| Invoke-RestMethod -Uri 'https://discordapp.com/api/webhooks/XXXXXXXXXX' -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($([PSCustomObject]@{content = $Content} | ConvertTo-Json))) | |
| } | |
| #メインルーチン | |
| #optifine.netから取得 | |
| $Response = Invoke-WebRequest -Uri 'https://optifine.net/downloads' | |
| #HTMLのStringをLFで分割してArrayとして変数に格納 | |
| $Response = $Response.Content -split '\n' | |
| #Preview、Releaseそれぞれの最新バージョンの行を取得 | |
| [PSCustomObject]$Latest = | |
| @{ | |
| Preview = | |
| @{ | |
| File = ($Response | Where-Object {$_ -match "td class=`'downloadLineFile`'"})[0] | |
| } | |
| Release = | |
| @{ | |
| File = ($Response | Where-Object {$_ -match "td class=`'downloadLineFileFirst`'"})[0] | |
| } | |
| } | |
| #Fileは整形、Dateはそれに該当する行を取得・整形 | |
| [PSCustomObject]$Latest = | |
| @{ | |
| Preview = | |
| @{ | |
| File = $Latest.Preview.File -split "<.*?>" | Where-Object {$_ -ne ""} | |
| Date = $Response[[Array]::IndexOf($Response,$Latest.Preview.File) + 4] -split "<.*?>" | Where-Object {$_ -ne ""} | |
| } | |
| Release = | |
| @{ | |
| File = $Latest.Release.File -split "<.*?>" | Where-Object {$_ -ne ""} | |
| Date = $Response[[Array]::IndexOf($Response,$Latest.Release.File) + 4] -split "<.*?>" | Where-Object {$_ -ne ""} | |
| } | |
| } | |
| #Fileはそのまま、MirrorとChangelogはFileを元に生成、DateはStringからDate型に変換 | |
| [PSCustomObject]$Latest = | |
| @{ | |
| Preview = | |
| @{ | |
| File = $Latest.Preview.File | |
| Mirror = "https://optifine.net/adloadx?f=preview_$($Latest.Preview.File -replace ' ','_').jar" #adf.lyでないMirrorのURLを選択 | |
| Changelog = "https://optifine.net/changelog?f=preview_$($Latest.Preview.File -replace ' ','_').jar" | |
| Date = [System.DateTimeOffset]::ParseExact("$($Latest.Preview.Date)","dd.MM.yyyy",$Null) #preview版は正式版がリリースされると消され、古いバージョンが先頭に来てしまうため、日付の大小による判別が適切 | |
| } | |
| Release = | |
| @{ | |
| File = $Latest.Release.File | |
| Mirror = "https://optifine.net/adloadx?f=$($Latest.Release.File -replace ' ','_').jar" | |
| Changelog = "https://optifine.net/changelog?f=$($Latest.Release.File -replace ' ','_').jar" | |
| Date = [System.DateTimeOffset]::ParseExact("$($Latest.Release.Date)","dd.MM.yyyy",$Null) | |
| } | |
| } | |
| #前回の実行結果を取得 | |
| [PSCustomObject]$BeforeLatest = Get-Content -Encoding UTF8 "$PSName.json" -ErrorAction SilentlyContinue | ConvertFrom-Json | |
| #初回($Null)又はPreview版の日付が大きくなった場合Post | |
| #pwshではOut-FileしたJsonに含まれるDateTimeがズレる不具合が無いようなので、$Latest.Preview.Date.UtcDateTimeとしなくて良い[要出典] | |
| if ($Latest.Preview.Date -gt $BeforeLatest.Preview.Date) | |
| { | |
| Post-Discord $Latest.Preview | |
| } | |
| #初回($Null)又はRelease版の日付が大きくなった場合Post | |
| if ($Latest.Release.Date -gt $BeforeLatest.Release.Date) | |
| { | |
| Post-Discord $Latest.Release | |
| } | |
| #比較用jsonファイルを更新 | |
| $Latest | ConvertTo-Json | Out-File "$PSName.json" -Encoding UTF8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment