Skip to content

Instantly share code, notes, and snippets.

@jogerj
Forked from MadeBaruna/importer.ps1
Last active July 31, 2024 08:23
Show Gist options
  • Save jogerj/84e386b27246c87cdcca6ffb982f9958 to your computer and use it in GitHub Desktop.
Save jogerj/84e386b27246c87cdcca6ffb982f9958 to your computer and use it in GitHub Desktop.
Paimon.moe Local Importer (Windows)
Add-Type -AssemblyName System.Web
Write-Host "Paimon.moe Wish Importer" -ForegroundColor Cyan
Write-Host "1. Open Genshin Impact in this PC"
Write-Host "2. Then open the wish history and wait it to load"
Write-Host "3. When you are ready press [ENTER] to continue! (or any key to cancel)"
Write-Host "Waiting..."
$keyInput = [Console]::ReadKey($true).Key
if ($keyInput -ne "13") {
Write-Host "Bye~"
exit
}
$logLocationGlobal = "%userprofile%\AppData\LocalLow\miHoYo\Genshin Impact\output_log.txt";
$logLocationChina = "%userprofile%\AppData\LocalLow\miHoYo\$([char]0x539f)$([char]0x795e)\output_log.txt";
$pathGlobal = [System.Environment]::ExpandEnvironmentVariables($logLocationGlobal);
$pathChina = [System.Environment]::ExpandEnvironmentVariables($logLocationChina);
$tmps = $env:TEMP + '\pm.ps1';
if ([System.IO.File]::Exists($tmps)) {
ri $tmps
}
if ([System.IO.File]::Exists($pathGlobal)) {
if ([System.IO.File]::Exists($pathChina)) {
# both exists, pick the newest one
if ((Get-Item $pathGlobal).LastWriteTime -ge (Get-Item $pathChina).LastWriteTime) {
$path = $pathGlobal;
$apiHost = "public-operation-hk4e-sg.hoyoverse.com";
} else {
$path = $pathChina;
$apiHost = "public-operation-hk4e.mihoyo.com";
}
} else {
$path = $pathGlobal;
$apiHost = "public-operation-hk4e-sg.hoyoverse.com";
}
} else {
if ([System.IO.File]::Exists($pathChina)) {
$path = $pathChina;
$apiHost = "public-operation-hk4e.mihoyo.com";
} else {
Write-Host "Cannot find the log file! Make sure to open the wish history first!" -ForegroundColor Red
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "Do you want to try to run the script as Administrator? Press [ENTER] to continue, or any key to cancel."
$keyInput = [Console]::ReadKey($true).Key
if ($keyInput -ne "13") {
return
}
$myinvocation.mycommand.definition > $tmps
Start-Process powershell -Verb runAs -ArgumentList "-noexit", $tmps, $reg
break
}
return
}
}
$logs = Get-Content -Path $path
$m = $logs -match "(?m).:/.+(GenshinImpact_Data|YuanShen_Data)"
$m[0] -match "(.:/.+(GenshinImpact_Data|YuanShen_Data))" >$null
if ($matches.Length -eq 0) {
Write-Host "Cannot find the wish history url! Make sure to open the wish history first!" -ForegroundColor Red
return
}
$gamedir = $matches[1]
$cachefile = "$gamedir/webCaches/Cache/Cache_Data/data_2"
$tmpfile = "$env:TEMP/ch_data_2"
Copy-Item $cachefile -Destination $tmpfile
function testUrl($url) {
$ProgressPreference = 'SilentlyContinue'
$uri = [System.UriBuilder]::New($url)
$uri.Path = "event/gacha_info/api/getGachaLog"
$uri.Host = $apiHost
$uri.Fragment = ""
$params = [System.Web.HttpUtility]::ParseQueryString($uri.Query)
$params.Set("lang", "en");
$params.Set("gacha_type", 301);
$params.Set("size", "5");
$params.Add("lang", "en-us");
$uri.Query = $params.ToString()
$apiUrl = $uri.Uri.AbsoluteUri
$response = Invoke-WebRequest -Uri $apiUrl -ContentType "application/json" -UseBasicParsing -TimeoutSec 10 | ConvertFrom-Json
$testResult = $response.retcode -eq 0
return $testResult
}
$content = Get-Content -Encoding UTF8 -Raw $tmpfile
$splitted = $content -split "1/0/"
$found = $splitted -match "e20190909gacha-v2"
$link = $false
$linkFound = $false
for ($i = $found.Length - 1; $i -ge 0; $i -= 1) {
$t = $found[$i] -match "(https.+?game_biz=)"
$link = $matches[0]
Write-Host "`rChecking Link $i" -NoNewline
$testResult = testUrl $link
if ($testResult -eq $true) {
$linkFound = $true
break
}
Sleep 1
}
Remove-Item $tmpfile
Write-Host ""
if (-Not $linkFound) {
Write-Host "Cannot find the wish history url! Make sure to open the wish history first!" -ForegroundColor Red
return
}
$uri = [System.UriBuilder]::New($link)
$uri.Path = "gacha_info/api/getGachaLog"
$uri.Host = "$apiHost"
$uri.Fragment = ""
$banners = [ordered]@{
100 = "Beginners' Wish";
200 = "Standard";
301 = "Character Event";
302 = "Weapon Event";
}
$wishes = [System.Collections.ArrayList]@()
function fetch($url) {
$ProgressPreference = 'SilentlyContinue'
$retrycount = 0
$completed = $false
$response = $null
while (-Not $completed) {
try {
$response = Invoke-WebRequest -Uri $url -ContentType "application/json" -UseBasicParsing -TimeoutSec 30
$completed = $true
}
catch {
if ($retrycount -ge 3) {
throw
}
else {
$retrycount++
}
}
}
return $response
}
function GetBannerLog($code, $type) {
$total = 0
$params = [System.Web.HttpUtility]::ParseQueryString($uri.Query)
$params.Set("lang", "en");
$params.Set("gacha_type", $code);
$params.Set("size", "20");
$params.Add("lang", "en-us");
$page = 1
$lastList = [System.Collections.ArrayList]@()
$lastId = 0
do {
$params.Set("page", $page)
$params.Set("end_id", $lastId);
$uri.Query = $params.ToString()
$apiUrl = $uri.Uri.AbsoluteUri
Write-Host "`rProcessing $type Banner - Page $page - x$total" -NoNewline
try {
$response = fetch $apiUrl
$result = $response | ConvertFrom-Json
if ($result.retcode -ne 0) {
if ($result.message -eq "authkey timeout") {
Write-Host "Authkey expired, please re-open the Wish History page" -ForegroundColor Red
exit
}
Write-Host "Error code returned from MiHoYo API! Try again later" -ForegroundColor Red
exit
}
$lastList = $result.data.list
foreach ($wish in $result.data.list) {
[void]$wishes.Add(@($wish.gacha_type, $wish.time, $wish.name, $wish.item_type, $wish.rank_type))
}
$total = $total + $result.data.list.Count;
$page++;
if ($result.data.list.Count -gt 0) {
$lastId = $result.data.list[$result.data.list.Count - 1].id
}
else {
$lastId = 0
}
}
catch {
Write-Host "Error when connecting to MiHoYo API! (Check your internet connection)" -ForegroundColor Red
exit
}
Write-Host "`rProcessing $type Banner - Page $page - x$total" -NoNewline
Start-Sleep -Seconds 1
} while ($lastList.Count -ne 0)
}
foreach ($banner in $banners.GetEnumerator()) {
Write-Host ""
GetBannerLog $banner.Name $banner.Value
}
$generated = "paimonmoe,importer,version,1,0`n"
foreach ($w in $wishes) {
$str = $w -join ","
$generated += "$str`n"
}
Write-Host ""
Write-Host "Press any key to copy the result, then paste it back to paimon.moe" -ForegroundColor Green
[void][Console]::ReadKey($true)
Set-Clipboard -Value $generated
$documentpath = [Environment]::GetFolderPath("MyDocuments")
$time = Get-Date -Format "yyyyMMddHHmm"
$generated | Out-File -FilePath "$documentpath\paimon-moe-import-$time.csv"
Write-Host "The file also saved on $documentpath\paimon-moe-import-$time.csv" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment