Files
drop/server/internal/metadata/giantbomb.ts
Husky 1ae051f066 Update Prisma to 6.11 (#133)
* chore: update prisma to 6.11

more prisma future proofing due to experimental features

* chore: update dependencies

twemoji - new unicode update
argon2 - bux fixes
vue3-carousel - improve mobile experiance
vue-tsc - more stable

* fix: incorrect prisma version in docker

Also remove default value for BUILD_DROP_VERSION, that is now handled in nuxt config

* fix: no logging in prod

* chore: optimize docker builds even more

* fix: revert adoption of prisma driverAdapters

see: https://github.com/prisma/prisma/issues/27486

* chore: optimize dockerignore some more

* Fix `pino-pretty` not being included in build (#135)

* Remove `pino` from frontend

* Fix for downloads and removing of library source (#136)

* fix: downloads and removing library source

* fix: linting

* Fix max file size of 4GB (update droplet) (#137)

* Fix manual metadata import (#138)

* chore(deps): bump vue-i18n from 10.0.7 to 10.0.8 (#140)

Bumps [vue-i18n](https://github.com/intlify/vue-i18n/tree/HEAD/packages/vue-i18n) from 10.0.7 to 10.0.8.
- [Release notes](https://github.com/intlify/vue-i18n/releases)
- [Changelog](https://github.com/intlify/vue-i18n/blob/master/CHANGELOG.md)
- [Commits](https://github.com/intlify/vue-i18n/commits/v10.0.8/packages/vue-i18n)

---
updated-dependencies:
- dependency-name: vue-i18n
  dependency-version: 10.0.8
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps): bump @intlify/core from 10.0.7 to 10.0.8 (#139)

---
updated-dependencies:
- dependency-name: "@intlify/core"
  dependency-version: 10.0.8
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Small fixes (#141)

* fix: save task as Json rather than string

* fix: pull objects before creating game in database

* fix: strips relative dirs from version information

* fix: #132

* fix: lint

* fix: news object ids and small tweaks

* fix: notification styling errors

* fix: lint

* fix: build issues by regenerating lockfile

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: DecDuck <declanahofmeyr@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-25 21:28:00 +10:00

311 lines
8.0 KiB
TypeScript

import type { CompanyModel } from "~/prisma/client/models";
import { MetadataSource } from "~/prisma/client/enums";
import type { MetadataProvider } from ".";
import { MissingMetadataProviderConfig } from ".";
import type {
GameMetadataSearchResult,
_FetchGameMetadataParams,
GameMetadata,
_FetchCompanyMetadataParams,
CompanyMetadata,
GameMetadataRating,
} from "./types";
import axios, { type AxiosRequestConfig } from "axios";
import TurndownService from "turndown";
import { DateTime } from "luxon";
import type { TaskRunContext } from "../tasks";
interface GiantBombResponseType<T> {
error: "OK" | string;
limit: number;
offset: number;
number_of_page_results: number;
number_of_total_results: number;
status_code: number;
results: T;
version: string;
}
interface GameSearchResult {
guid: string;
name: string;
deck: string;
original_release_date?: string;
expected_release_year?: number;
image?: {
icon_url: string;
};
}
interface GameResult {
guid: string;
name: string;
deck: string;
description?: string;
developers?: Array<{ id: number; name: string }>;
publishers?: Array<{ id: number; name: string }>;
number_of_user_reviews: number; // Doesn't provide an actual rating, so kinda useless
original_release_date?: string;
expected_release_day?: number;
expected_release_month?: number;
expected_release_year?: number;
image: {
icon_url: string;
screen_large_url: string;
};
images: Array<{
tags: string; // If it's "All Images", art, otherwise screenshot
original: string;
}>;
reviews?: Array<{
api_detail_url: string;
}>;
}
interface ReviewResult {
deck: string;
score: number; // Out of 5
reviewer: string;
site_detail_url: string;
}
interface CompanySearchResult {
guid: string;
deck: string | null;
description: string | null;
name: string;
website: string | null;
image: {
icon_url: string;
screen_large_url: string;
};
}
// Api Docs: https://www.giantbomb.com/api/
export class GiantBombProvider implements MetadataProvider {
private apikey: string;
private turndown: TurndownService;
constructor() {
const apikey = process.env.GIANT_BOMB_API_KEY;
if (!apikey)
throw new MissingMetadataProviderConfig(
"GIANT_BOMB_API_KEY",
this.name(),
);
this.apikey = apikey;
this.turndown = new TurndownService();
this.turndown.addRule("remove-links", {
filter: ["a"],
replacement: function (content) {
return content;
},
});
}
private async request<T>(
resource: string,
url: string,
query: { [key: string]: string },
options?: AxiosRequestConfig,
) {
const queryString = new URLSearchParams({
...query,
api_key: this.apikey,
format: "json",
}).toString();
const finalURL = `https://www.giantbomb.com/api/${resource}/${url}?${queryString}`;
const overlay: AxiosRequestConfig = {
url: finalURL,
baseURL: "",
};
const response = await axios.request<GiantBombResponseType<T>>(
Object.assign({}, options, overlay),
);
return response;
}
name() {
return "GiantBomb";
}
source() {
return MetadataSource.GiantBomb;
}
async search(query: string): Promise<GameMetadataSearchResult[]> {
const results = await this.request<Array<GameSearchResult>>("search", "", {
query: query,
resources: ["game"].join(","),
});
const mapped = results.data.results.map((result) => {
const date =
(result.original_release_date
? DateTime.fromISO(result.original_release_date).year
: result.expected_release_year) ?? 0;
const metadata: GameMetadataSearchResult = {
id: result.guid,
name: result.name,
icon: result.image?.icon_url ?? "",
description: result.deck,
year: date,
};
return metadata;
});
return mapped;
}
async fetchGame(
{ id, publisher, developer, createObject }: _FetchGameMetadataParams,
context?: TaskRunContext,
): Promise<GameMetadata> {
context?.logger.info("Using GiantBomb provider");
const result = await this.request<GameResult>("game", id, {});
const gameData = result.data.results;
const longDescription = gameData.description
? this.turndown.turndown(gameData.description)
: gameData.deck;
const publishers: CompanyModel[] = [];
if (gameData.publishers) {
for (const pub of gameData.publishers) {
context?.logger.info(`Importing publisher "${pub.name}"`);
const res = await publisher(pub.name);
if (res === undefined) {
context?.logger.warn(`Failed to import publisher "${pub}"`);
continue;
}
context?.logger.info(`Imported publisher "${pub}"`);
publishers.push(res);
}
}
context?.progress(35);
const developers: CompanyModel[] = [];
if (gameData.developers) {
for (const dev of gameData.developers) {
context?.logger.info(`Importing developer "${dev.name}"`);
const res = await developer(dev.name);
if (res === undefined) {
context?.logger.warn(`Failed to import developer "${dev}"`);
continue;
}
context?.logger.info(`Imported developer "${dev}"`);
developers.push(res);
}
}
context?.progress(70);
const icon = createObject(gameData.image.icon_url);
const banner = createObject(gameData.image.screen_large_url);
const imageURLs: string[] = gameData.images.map((e) => e.original);
const images = [banner, ...imageURLs.map(createObject)];
context?.logger.info(`Found all images. Total of ${images.length + 1}.`);
const releaseDate = gameData.original_release_date
? DateTime.fromISO(gameData.original_release_date).toJSDate()
: DateTime.fromISO(
`${gameData.expected_release_year ?? new Date().getFullYear()}-${
gameData.expected_release_month ?? 1
}-${gameData.expected_release_day ?? 1}`,
).toJSDate();
context?.progress(85);
const reviews: GameMetadataRating[] = [];
if (gameData.reviews) {
context?.logger.info("Found reviews, importing...");
for (const { api_detail_url } of gameData.reviews) {
const reviewId = api_detail_url.split("/").at(-2);
if (!reviewId) continue;
const review = await this.request<ReviewResult>("review", reviewId, {});
reviews.push({
metadataSource: MetadataSource.GiantBomb,
metadataId: reviewId,
mReviewCount: 1,
mReviewRating: review.data.results.score / 5,
mReviewHref: review.data.results.site_detail_url,
});
}
}
const metadata: GameMetadata = {
id: gameData.guid,
name: gameData.name,
shortDescription: gameData.deck,
description: longDescription,
released: releaseDate,
tags: [],
reviews,
publishers,
developers,
icon,
bannerId: banner,
coverId: images[1] ?? banner,
images,
};
context?.logger.info("GiantBomb provider finished.");
context?.progress(100);
return metadata;
}
async fetchCompany({
query,
createObject,
}: _FetchCompanyMetadataParams): Promise<CompanyMetadata | undefined> {
const results = await this.request<Array<CompanySearchResult>>(
"search",
"",
{ query, resources: "company" },
);
// Find the right entry
const company =
results.data.results.find((e) => e.name == query) ??
results.data.results.at(0);
if (!company) return undefined;
const longDescription = company.description
? this.turndown.turndown(company.description)
: company.deck;
const metadata: CompanyMetadata = {
id: company.guid,
name: company.name,
shortDescription: company.deck ?? "",
description: longDescription ?? "",
website: company.website ?? "",
logo: createObject(company.image.icon_url),
banner: createObject(company.image.screen_large_url),
};
return metadata;
}
}