forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-names_anilist.ps1
58 lines (52 loc) · 1.72 KB
/
get-names_anilist.ps1
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
#!/usr/bin/pwsh
<#
.SYNOPSIS
Script to crawl AniList for character names
.PARAMETER output
Output filename.
Default is names_anilist.txt.
#>
param(
[string]$output = 'names_anilist.txt'
)
# disable extra noise for invoke-webrequest
#$ProgressPreference = "SilentlyContinue"
$requestTemplate = '{ "query": "query { Page (page: {0}, perPage: 50) { characters { name { full } } pageInfo { hasNextPage lastPage } } }" }'
$startTime = [DateTime]::UtcNow
$page = 0
$result = @()
$hasNextPage = $false
do
{
try
{
$json = $requestTemplate.Replace('{0}', $page)
$response = Invoke-RestMethod 'https://graphql.anilist.co' -Method Post -Body $json -ContentType "application/json"
$response = $response.data.Page
$hasNextPage = $response.pageInfo.hasNextPage
$chars = $response.characters
foreach ($char in $chars)
{
$result += $char.name.full.Trim()
}
$page++
$total = $response.pageInfo.lastPage
$remainingSeconds = ($total - $page) * 1.7
if ($page -gt 100)
{
$remainingSeconds = ([DateTime]::UtcNow - $startTime).TotalSeconds / $page * ($total - $page)
}
Write-Progress Downloading -CurrentOperation "Page $page out of $total" -PercentComplete ($page * 100 / $total) -SecondsRemaining $remainingSeconds
Start-Sleep -Seconds 1
}
catch
{
Write-Host "Failed to request page $page"
$hasNextPage = $false
}
} while ($hasNextPage)
Write-Progress Downloading -Completed
Write-Host 'Saving the results...'
'# https://anilist.co/search/characters' | Out-File -LiteralPath $output
$result | Sort-Object | Get-Unique | Out-File -LiteralPath $output -Append
Write-Host 'Done.'