Last active
October 30, 2022 09:00
-
-
Save rekyuu/92b2ef997225dfed57c16dbdcb5709a3 to your computer and use it in GitHub Desktop.
Genshin Impact - Retrieve Gacha API URL
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
// See https://aka.ms/new-console-template for more information | |
using System.Collections.Specialized; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
string homeFolder = "/home/rekyuu"; | |
string genshinPrefix = $"{homeFolder}/wineprefixes/genshin-impact"; | |
string sourceDataPath = | |
$"{genshinPrefix}/drive_c/Program Files/Genshin Impact/Genshin Impact game/GenshinImpact_Data/webCaches/Cache/Cache_Data/data_2"; | |
string tmpDataPath = $"{Path.GetTempPath()}/genshin_data"; | |
File.Copy(sourceDataPath, tmpDataPath, true); | |
string data = File.ReadAllText(tmpDataPath, Encoding.UTF8); | |
string[] dataSplit = data.Split("1/0/"); | |
string[] results = dataSplit.Where(x => x.Contains("e20190909gacha-v2")).Reverse().ToArray(); | |
HttpClient client = new(); | |
client.DefaultRequestHeaders | |
.Accept | |
.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
string successfulApiUrl = ""; | |
foreach (string result in results) | |
{ | |
Regex rx = new Regex("(https.+?game_biz=hk4e_global)"); | |
var match = rx.Match(result); | |
if (!match.Success) continue; | |
UriBuilder uri = new(match.Value); | |
NameValueCollection uriParams = HttpUtility.ParseQueryString(uri.Query); | |
uriParams.Set("lang", "en"); | |
uriParams.Set("gacha_type", "301"); | |
uriParams.Set("size", "5"); | |
uriParams.Add("lang", "en-us"); | |
uri.Path = "event/gacha_info/api/getGachaLog"; | |
uri.Host = "hk4e-api-os.hoyoverse.com"; | |
uri.Fragment = ""; | |
uri.Query = uriParams.ToString(); | |
string apiUrl = uri.Uri.AbsoluteUri; | |
HttpResponseMessage response = await client.GetAsync(apiUrl); | |
if (response.IsSuccessStatusCode) | |
{ | |
successfulApiUrl = apiUrl; | |
Console.WriteLine(successfulApiUrl); | |
break; | |
} | |
} | |
if (string.IsNullOrEmpty(successfulApiUrl)) | |
{ | |
Console.WriteLine("Could not find a valid URL. Try checking your wish history."); | |
Environment.Exit(1); | |
} | |
File.Delete(tmpDataPath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment