Skip to content

Commit

Permalink
Use Yatta.moe API URLs instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed Sep 18, 2024
1 parent c255b5e commit 5c17de2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ This repository is for generating GachaMetaDB to serve those apps conforming to
- Built executable file path is `.build/release/GachaMetaGenerator`.
- Step 3: Run the compiled executable and pipeline the output contents into a new JSON file.
- You only need one parameter to specify whether it writes for Genshin or HSR.
- GI using Ambr.top API: `./GachaMetaGenerator -GI > ./OUTPUT-GI.json`.
- HSR using Yatta.top API: `./GachaMetaGenerator -HSR > ./OUTPUT-HSR.json`
- GI using Yatta.moe API: `./GachaMetaGenerator -GI > ./OUTPUT-GI.json`.
- HSR using Yatta.moe API: `./GachaMetaGenerator -HSR > ./OUTPUT-HSR.json`
- GI using Dimbreath's Repo: `./GachaMetaGenerator -GID > ./OUTPUT-GI.json`.
- HSRD using Dimbreath's Repo: `./GachaMetaGenerator -HSRD > ./OUTPUT-HSR.json`
- The above pipeline commands are proved effective in Bash and ZSH. Be careful that some other shells like `nu` may have different pipeline commands.
Expand Down
2 changes: 1 addition & 1 deletion Sources/GachaMetaDB/GachaMeta.MetaDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension GachaMeta.MetaDB {

public static func fetchAndCompileLatestDB(for game: SupportedGame) async throws
-> GachaMeta.MetaDB {
try await GachaMetaGenerator.fetchAndCompileFromAmbrYatta(for: game)
try await GachaMetaGenerator.fetchAndCompileFromYatta(for: game)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/GachaMetaGenerator/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ case 1, 2:
let useDimBreath: Bool = firstArgument.suffix(1).lowercased() == "d"
let dict = useDimBreath
? try await GachaMetaGenerator.fetchAndCompileFromDimbreath(for: game)
: try await GachaMetaGenerator.fetchAndCompileFromAmbrYatta(for: game)
: try await GachaMetaGenerator.fetchAndCompileFromYatta(for: game)
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
if let encoded = String(data: try encoder.encode(dict), encoding: .utf8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import Foundation

extension GachaMetaGenerator {
typealias AmbrYattaFetchedItem = AmbrYattaResponse.FetchedModel.FetchedItem
typealias YattaFetchedItem = YattaResponse.FetchedModel.FetchedItem

struct AmbrYattaResponse: Codable {
struct YattaResponse: Codable {
struct FetchedModel: Codable {
/// We use this shared struct for both sides
/// since only these 3 fields are useful in this project.
Expand Down Expand Up @@ -61,21 +61,21 @@ extension GachaMetaGenerator {
}
}

extension GachaMetaGenerator.AmbrYattaResponse {
var items: [GachaMetaGenerator.AmbrYattaFetchedItem] {
extension GachaMetaGenerator.YattaResponse {
var items: [GachaMetaGenerator.YattaFetchedItem] {
Array((data?.items ?? [:]).values).sorted {
$0.id < $1.id
}
}
}

extension GachaMetaGenerator.AmbrYattaFetchedItem {
extension GachaMetaGenerator.YattaFetchedItem {
func toGachaItemMeta() -> GachaMetaGenerator.GachaItemMeta {
.init(id: id, rank: rank, nameTextMapHash: nameTextMapHash ?? -114514)
}
}

extension [GachaMetaGenerator.GachaDictLang?: [GachaMetaGenerator.AmbrYattaFetchedItem]] {
extension [GachaMetaGenerator.GachaDictLang?: [GachaMetaGenerator.YattaFetchedItem]] {
/// 这里假设所有语言下的 FetchedItem 都是雷同的,且必须有 static 的查询结果。
func assemble() -> [GachaMetaGenerator.GachaItemMeta]? {
guard let staticStack = self[nil] else { return nil }
Expand Down
26 changes: 13 additions & 13 deletions Sources/GachaMetaGeneratorModule/Components/SupportedGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,42 @@ extension GachaMetaGenerator {
}
}

// MARK: - Dealing with Ambr.top and Yatta.top API Results.
// MARK: - Dealing with Yatta.moe API Results.

extension GachaMetaGenerator.SupportedGame {
/// Only used for dealing with Ambr.top and Yatta.top API Results.
/// Only used for dealing with Yatta.moe API Results.
///
/// If the lang is given null, then the parameter raw value will be `static`.
/// This will let the `name` field become `nameTextMapHash`.
func getAmbrYattaAPIURL(for type: DataURLType, lang: GachaMetaGenerator.GachaDictLang?) -> URL {
func getYattaAPIURL(for type: DataURLType, lang: GachaMetaGenerator.GachaDictLang?) -> URL {
var langTag = lang.ambrTopLangID
if lang == .langCHS, self == .starRail { langTag = "cn" }
var result = ""
switch (self, type) {
case (.genshinImpact, .weaponData): result += "https://gi.yatta.top/api/v2/\(langTag)/weapon"
case (.genshinImpact, .characterData): result += "https://gi.yatta.top/api/v2/\(langTag)/avatar"
case (.starRail, .weaponData): result += "https://api.yatta.top/hsr/v2/\(langTag)/equipment"
case (.starRail, .characterData): result += "https://api.yatta.top/hsr/v2/\(langTag)/avatar"
case (.genshinImpact, .weaponData): result += "https://gi.yatta.moe/api/v2/\(langTag)/weapon"
case (.genshinImpact, .characterData): result += "https://gi.yatta.moe/api/v2/\(langTag)/avatar"
case (.starRail, .weaponData): result += "https://sr.yatta.moe/api/v2/\(langTag)/equipment"
case (.starRail, .characterData): result += "https://sr.yatta.moe/api/v2/\(langTag)/avatar"
}
return URL(string: result)!
}

/// Only used for dealing with Ambr.top and Yatta.top API Results.
/// Only used for dealing with Yatta.moe API Results.
///
/// If the lang is given null, then the parameter raw value will be `static`.
/// This will fetch the `nameTextMapHash`.
func fetchAmbrYattaData(
func fetchYattaData(
lang: [GachaMetaGenerator.GachaDictLang?]? = nil
) async throws
-> [GachaMetaGenerator.GachaItemMeta] {
var buffer = [(items: [GachaMetaGenerator.AmbrYattaFetchedItem], lang: GachaMetaGenerator.GachaDictLang?)]()
var buffer = [(items: [GachaMetaGenerator.YattaFetchedItem], lang: GachaMetaGenerator.GachaDictLang?)]()
for dataType in GachaMetaGenerator.SupportedGame.DataURLType.allCases {
for locale in GachaMetaGenerator.GachaDictLang?.allCases(for: self) {
let url = getAmbrYattaAPIURL(for: dataType, lang: locale)
let url = getYattaAPIURL(for: dataType, lang: locale)
try await Task.sleep(nanoseconds: UInt64(0.4 * Double(1_000_000_000)))
let (data, _) = try await URLSession.shared.asyncData(from: url)
do {
let jsonParsed = try JSONDecoder().decode(GachaMetaGenerator.AmbrYattaResponse.self, from: data)
let jsonParsed = try JSONDecoder().decode(GachaMetaGenerator.YattaResponse.self, from: data)
var rawStack = jsonParsed.items
if locale == .langJP {
rubyTest: for i in 0 ..< rawStack.count {
Expand All @@ -89,7 +89,7 @@ extension GachaMetaGenerator.SupportedGame {
}
}
}
var results = [GachaMetaGenerator.GachaDictLang?: [GachaMetaGenerator.AmbrYattaFetchedItem]]()
var results = [GachaMetaGenerator.GachaDictLang?: [GachaMetaGenerator.YattaFetchedItem]]()
for result in buffer {
results[result.lang, default: []].append(contentsOf: result.items)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/GachaMetaGeneratorModule/GeneratorAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum GachaMetaGenerator {}
extension GachaMetaGenerator {
public typealias CompilationResult = [String: GachaItemMeta]

public static func fetchAndCompileFromAmbrYatta(
public static func fetchAndCompileFromYatta(
for game: SupportedGame, lang: [GachaDictLang?]? = nil
) async throws
-> CompilationResult {
Expand All @@ -24,7 +24,7 @@ extension GachaMetaGenerator {
}

var result = CompilationResult()
try await game.fetchAmbrYattaData(lang: lang).forEach {
try await game.fetchYattaData(lang: lang).forEach {
result[$0.id.description] = $0
}
return result
Expand Down
14 changes: 7 additions & 7 deletions Tests/GachaMetaGeneratorTests/GachaMetaGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ final class GachaMetaGeneratorTests: XCTestCase {
// MARK: - Ambr Yatta API Tests.

extension GachaMetaGeneratorTests {
func testGeneratingHSRFromAmbrYattaAPI() async throws {
let dataHSR = try await GachaMetaGenerator.SupportedGame.starRail.fetchAmbrYattaData()
func testGeneratingHSRFromYattaAPI() async throws {
let dataHSR = try await GachaMetaGenerator.SupportedGame.starRail.fetchYattaData()
print(try dataHSR.encodedJSONString() ?? "FAILED.")
XCTAssertNotNil(!dataHSR.isEmpty)
}

func testGeneratingGIFromAmbrYattaAPI() async throws {
let dataGI = try await GachaMetaGenerator.SupportedGame.genshinImpact.fetchAmbrYattaData()
func testGeneratingGIFromYattaAPI() async throws {
let dataGI = try await GachaMetaGenerator.SupportedGame.genshinImpact.fetchYattaData()
print(try dataGI.encodedJSONString() ?? "FAILED.")
XCTAssertNotNil(!dataGI.isEmpty)
}

func testDecodingAmbrYattaData() async throws {
func testDecodingYattaData() async throws {
for game in GachaMetaGenerator.SupportedGame.allCases {
for dataType in GachaMetaGenerator.SupportedGame.DataURLType.allCases {
for lang in GachaMetaGenerator.GachaDictLang?.allCases(for: game) {
print("------------------------------------")
let url = game.getAmbrYattaAPIURL(for: dataType, lang: lang)
let url = game.getYattaAPIURL(for: dataType, lang: lang)
print(url.absoluteString)
let (data, _) = try await URLSession.shared.asyncData(from: url)
do {
let jsonParsed = try JSONDecoder().decode(GachaMetaGenerator.AmbrYattaResponse.self, from: data)
let jsonParsed = try JSONDecoder().decode(GachaMetaGenerator.YattaResponse.self, from: data)
XCTAssertFalse(jsonParsed.data?.items?.isEmpty ?? true)
} catch {
print(error.localizedDescription)
Expand Down

0 comments on commit 5c17de2

Please sign in to comment.