This repository was archived by the owner on Jan 5, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathadmiral_stats_exporter_ps.ps1
More file actions
178 lines (154 loc) · 6.92 KB
/
Copy pathadmiral_stats_exporter_ps.ps1
File metadata and controls
178 lines (154 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Simple admiral stats exporter from kancolle-arcade.net for Windows PowerShell
# プログラム翻案: KOIZUMI Naoki (@sophiarcp)
function putJson2AS ($json_dir, $access_token) {
# Upload exported files to Admiral Stats
# 戻り値:
# 0 : 正常終了
# 1 : インポート対象ファイルタイプ取得エラー(401)
# 2 : インポート対象ファイルタイプ取得エラー(その他)
# 3 : インポート実行時のエラー(400,401)
# 4 : インポート実行時のエラー(その他)
# Admiral Stats Import URL
$AS_IMPORT_URL = 'https://www.admiral-stats.com/api/v1/import'
# User Agent for logging on www.admiral-stats.com
$AS_HTTP_HEADER_UA = 'AdmiralStatsExporter-PS/1.15.0'
# Set Authorization header
$headers = @{ "Authorization" = ("Bearer", $access_token -join " ") }
# Get currently importable file types
try {
$uri = "$AS_IMPORT_URL/file_types"
$importable_file_types = Invoke-RestMethod -uri $uri -Method Get -Headers $headers -UserAgent $AS_HTTP_HEADER_UA -ErrorAction stop
Write-host "Importable file types: $importable_file_types"
} catch {
switch ($error[0].Exception.Response.StatusCode.value__) {
401 {
Write-Host "ERROR: "$Error[0]
return 1
}
default {
Write-Host "ERROR: "$Error[0]
return 2
}
}
}
foreach ($json_file in (Get-ChildItem "$json_dir\*.*" -include *.json)) {
$dummy = $json_file.name -match "(.*)_(\d{8}_\d{6})\.json$"
$file_type = $matches[1]
$timestamp = $matches[2]
if ( -not ($importable_file_types -contains $file_type) ) { continue }
$json = get-content $json_file -Raw -encoding UTF8
# この GetBytes() がないと、UTF-8 を含む Body(例:艦娘一覧の装備スロット)が
# UTF-8 として認識されず、送信時に文字化けする
# 参考:https://www.uramiraikan.net/Works/entry-2798.html
$json = [System.Text.Encoding]::UTF8.GetBytes($json)
$uri = "$AS_IMPORT_URL/$file_type/$timestamp"
$res = ""
try {
$res = Invoke-WebRequest -uri $uri -UseBasicParsing -Method Post -Body $json -ContentType 'application/json' -UserAgent $AS_HTTP_HEADER_UA -Headers $headers
switch -regex ($res.StatusCode) {
"20[01]" {
$resJson = ConvertFrom-Json($res.content)
Write-Host $resjson.data.message"(ファイル名:"$json_file.name")"
}
default {
Write-Host "ERROR: $res.content"
}
}
} catch {
switch -regex ($error[0].Exception.Response.StatusCode.value__) {
"40[01]" {
Write-host "ERROR: "$error[0]
return 3
}
default {
Write-host "ERROR: "$error[0]
return 4
}
}
}
}
return 0
}
$api_base = "https://kancolle-arcade.net/ac/api/"
$ymdhms = get-date -Format "yyyyMMdd_HHmmss"
$outdir = ".\json\" + $ymdhms
$credential_path = ".\cred.xml"
$token_path = ".\token.dat"
$do_upload = $false
if ( -not(Test-Path $credential_path )) {
try {
Get-Credential -Message "初回実行のためプレイヤーズサイトの認証情報を登録してください。" | Export-Clixml $credential_path | Out-Null
} catch {
exit 4
}
echo "$credential_path に保存しました。"
}
$credential = Import-Clixml $credential_path
$username = $credential.UserName
$pass = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($credential.Password))
# Check whether to upload JSON files or not
if ( $args[0] -eq "--upload" ) {
$do_upload = $true
if ( Test-Path $token_path ) {
$access_token = Get-Content $token_path -Encoding ascii
} else {
try {
$access_token = Read-Host "初回実行のためAdmiral StatsのAPIトークンを入力してください。"
if ( $access_token -ne "" ) {
$access_token | out-file $token_path -Encoding ascii #| Out-Null
} else {
Write-Host "トークン情報が入力されていません。処理を中断します。"
exit 4
}
} catch {
Write-Host $error[0]
Write-Host "トークン情報の取得/保存中にエラーが発生しました。処理を中断します。"
exit 5
}
echo "$token_path に保存しました。"
}
}
$err = 0
$headers = @{ "Referer" = "https://kancolle-arcade.net/ac/"; "X-Requested-With" = "XMLHttpRequest" ; "Host" = "kancolle-arcade.net" }
$bodytext = '{"id":"' + $username + '","password":"' + $pass + '"}'
try {
$res = Invoke-RestMethod -uri "https://kancolle-arcade.net/ac/api/Auth/login" -Method Post -Body $bodytext -ContentType 'application/json' -Headers $headers -SessionVariable sv
if (-not($res.login)) {
echo "認証に失敗しました。ユーザー名またはパスワードが間違っています。"
echo "パスワードの登録を間違えた場合は、 $credential_path を削除して再実行してください。"
exit 1
}
} catch {
echo "認証に失敗しました。プレイヤーズサイトが停止しているか、メンテナンス中(2:00~7:00)です。"
exit 2
}
New-Item $outdir -ItemType Directory -Force | Out-Null
try {
$infoarray = @("Personal/basicInfo", 'Area/captureInfo', 'TcBook/info', 'EquipBook/info', 'Campaign/history', 'Campaign/info', 'Campaign/present', 'CharacterList/info', 'EquipList/info', 'Quest/info', 'Event/info', 'RoomItemList/info', 'BlueprintList/info', 'Exercise/info', 'Cop/info')
foreach( $infoaddr in $infoArray ) {
$outfn = $outdir + "\" + $infoaddr.Replace("/", "_") + "_" + $ymdhms + ".json"
$uri = $api_base + $infoaddr
#Invoke-RestMethod -uri $uri -Method Get -Headers $headers -WebSession $sv | ConvertTo-Json -Compress | Out-File $outfn #Invoke-RestMethod
Invoke-WebRequest -Uri $uri -UseBasicParsing -WebSession $sv -Headers $headers -OutFile $outfn
# 204 (No Content) が返された場合はファイルサイズが空になる。その場合はファイルを削除する
# Invoke-WebRequest には、正常終了時のステータスコードを取得する方法がなかったため、ファイルサイズで判断
if ((Get-ChildItem $outfn).Length -eq 0) {
echo "ダウンロードしたファイルが空のため、削除します。対象ファイル: $outfn"
Remove-Item $outfn
}
}
} catch {
echo "データ取得中にエラーが発生しました。対象アドレス: $uri"
exit 3
}
echo "データ取得が完了しました。"
echo "保存先: $outdir"
# Upload exported files to Admiral Stats
if ($do_upload) {
$ret = putJson2AS $outdir $access_token
switch ($ret) {
{1,3 -contains $_ } {
Write-Host "APIトークンが正しくない可能性があります。$token_path を修正するか、削除して再登録してください。"
}
}
}