Created
July 5, 2023 21:12
-
-
Save codemasher/e33a3aacebfa6d2315558acb4f218a06 to your computer and use it in GitHub Desktop.
get genshin gacha log url from cache
This file contains 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
<?php | |
/** | |
* genshin-wish-history.php | |
* | |
* @see https://gist.github.com/MadeBaruna/1d75c1d37d19eca71591ec8a31178235 | |
* @see https://github.com/chillerlan/php-httpinterface | |
* | |
* @created 05.07.2023 | |
* @author smiley <[email protected]> | |
* @copyright 2023 smiley | |
* @license MIT | |
*/ | |
use chillerlan\HTTP\Psr18\CurlClient; | |
use chillerlan\HTTP\Psr7\{Request, Uri}; | |
use chillerlan\HTTP\Utils\QueryUtil; | |
require_once __DIR__.'/../vendor/autoload.php'; | |
$args = getopt('', ['china::']); | |
$dirName = 'Genshin Impact'; | |
$apiHost = 'hk4e-api-os.hoyoverse.com'; | |
if(isset($args['china'])){ | |
$dirName = '原神'; | |
$apiHost = 'hk4e-api.mihoyo.com'; | |
} | |
// attempt to find log file | |
$logLocation = realpath(sprintf('%s/AppData/LocalLow/miHoYo/%s/output_log.txt', getenv('userprofile'), $dirName)); | |
if(!$logLocation){ | |
throw new RuntimeException('cannot find log file'); | |
} | |
// get game dir from log | |
$logData = file_get_contents($logLocation); | |
preg_match('#.:/.+/GenshinImpact_Data|YuanShen_Data#', $logData, $matches); | |
if(empty($matches)){ | |
throw new RuntimeException('cannot find cache directory'); | |
} | |
// get cache file | |
#$cacheFile = sprintf('%s/webCaches/Cache/Cache_Data/data_2', $matches[0]); // <= Genshin 3.7 | |
$cacheFile = sprintf('%s/webCaches/2.13.0.1/Cache/Cache_Data/data_2', $matches[0]); // Genshin 3.8 | |
$cacheFile = realpath($cacheFile); | |
if(!$cacheFile){ | |
throw new RuntimeException('cannot find cache data file'); | |
} | |
// get URLs from cache | |
$cacheData = file_get_contents($cacheFile); | |
preg_match_all('/https[^\x00\x01\s]+/', $cacheData, $matches); | |
if(empty($matches) || empty($matches[0])){ | |
throw new RuntimeException('cannot find URLs in cache'); | |
} | |
$http = new CurlClient; | |
// check the gacha info URLs | |
foreach($matches[0] as $url){ | |
if(!str_contains($url, 'e20190909gacha-v2')){ | |
continue; | |
} | |
// test if the token is valid | |
$test = (new Uri(QueryUtil::merge($url, ['lang' => 'en-us', 'gacha_type' => '301', 'size' => '5']))) | |
->withHost($apiHost) | |
->withPath('event/gacha_info/api/getGachaLog') | |
; | |
$request = (new Request('GET', $test)) | |
->withHeader('Accept', 'application/json') | |
; | |
$response = $http->sendRequest($request); | |
if($response->getStatusCode() !== 200){ | |
sleep(1); | |
continue; | |
} | |
echo $url; | |
exit; | |
} | |
// search for gacha log URL | |
foreach($matches[0] as $url){ | |
if(!str_contains($url, 'event/gacha_info/api/getGachaLog')){ | |
continue; | |
} | |
$request = (new Request('GET', new Uri($url))) | |
->withHeader('Accept', 'application/json') | |
; | |
$response = $http->sendRequest($request); | |
if($response->getStatusCode() !== 200){ | |
sleep(1); | |
continue; | |
} | |
echo $url; | |
exit; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment