diff options
-rw-r--r-- | src/cleaners/skyblock/endedAuctions.ts | 6 | ||||
-rw-r--r-- | src/database.ts | 4 | ||||
-rw-r--r-- | src/hypixel.ts | 11 | ||||
-rw-r--r-- | src/hypixelApi.ts | 5 | ||||
-rw-r--r-- | src/index.ts | 34 |
5 files changed, 48 insertions, 12 deletions
diff --git a/src/cleaners/skyblock/endedAuctions.ts b/src/cleaners/skyblock/endedAuctions.ts index 0dfa0ab..0c61c6c 100644 --- a/src/cleaners/skyblock/endedAuctions.ts +++ b/src/cleaners/skyblock/endedAuctions.ts @@ -3,7 +3,7 @@ import { cleanInventory, headIdFromBase64, Item } from './inventory.js' import { cleanItemId } from './itemId.js' -interface Auction { +interface EndedAuction { id: string sellerUuid: string sellerProfileUuid: string @@ -16,11 +16,11 @@ interface Auction { export interface EndedAuctions { lastUpdated: number - auctions: Auction[] + auctions: EndedAuction[] } export async function cleanEndedAuctions(data: typedHypixelApi.SkyBlockRecentlyEndedAuctionsResponse): Promise<EndedAuctions> { - const auctions: Auction[] = [] + const auctions: EndedAuction[] = [] for (const auction of data.auctions) { auctions.push({ id: auction.auction_id, diff --git a/src/database.ts b/src/database.ts index 8f63d97..3aafd05 100644 --- a/src/database.ts +++ b/src/database.ts @@ -126,8 +126,8 @@ export interface SimpleAuctionSchema { * since we don't need to be super exact and so it's shorter. */ ts: number - /** Whether the auction was bought or simply expired. */ - success: boolean + /** Whether the auction was successfully bought or simply expired. */ + s: boolean bin: boolean } export interface ItemAuctionsSchema { diff --git a/src/hypixel.ts b/src/hypixel.ts index d4e1a49..88f3768 100644 --- a/src/hypixel.ts +++ b/src/hypixel.ts @@ -32,6 +32,7 @@ import * as cached from './hypixelCached.js' import { debug } from './index.js' import { WithId } from 'mongodb' import { cleanEndedAuctions } from './cleaners/skyblock/endedAuctions.js' +import { cleanAuctions } from './cleaners/skyblock/auctions.js' export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefined @@ -63,6 +64,7 @@ const cleanResponseFunctions = { 'skyblock/profile': (data: typedHypixelApi.SkyBlockProfileResponse, options) => cleanSkyblockProfileResponse(data.profile, options), 'skyblock/profiles': (data, options) => cleanSkyblockProfilesResponse(data.profiles), 'skyblock/auctions_ended': (data, options) => cleanEndedAuctions(data), + 'skyblock/auction': (data, options) => cleanAuctions(data), 'resources/skyblock/election': (data, options) => cleanElectionResponse(data), 'resources/skyblock/items': (data, options) => cleanItemListResponse(data), } as const @@ -360,6 +362,13 @@ export async function fetchItemList() { return itemList } +export async function fetchAuctionUncached(uuid: string) { + return await sendCleanApiRequest( + 'skyblock/auction', + { uuid } + ) +} + // this function is called from database.ts so it starts when we connect to the database // it should only ever be called once! export async function periodicallyFetchRecentlyEndedAuctions() { @@ -398,7 +407,7 @@ export async function periodicallyFetchRecentlyEndedAuctions() { } const simpleAuction: SimpleAuctionSchema = { - success: true, + s: true, coins: auction.coins, id: auction.id, ts: Math.floor(auction.timestamp / 1000), diff --git a/src/hypixelApi.ts b/src/hypixelApi.ts index 42d4f74..40a972a 100644 --- a/src/hypixelApi.ts +++ b/src/hypixelApi.ts @@ -13,6 +13,10 @@ if (!process.env.hypixel_keys) /** This array should only ever contain one item because using multiple hypixel api keys isn't allowed :) */ const apiKeys = process.env?.hypixel_keys?.split(' ') ?? [] +if (apiKeys.length === 0) { + console.warn('Warning: hypixel_keys was not found in .env. This will prevent the program from using the Hypixel API.') +} + interface KeyUsage { remaining: number limit: number @@ -100,6 +104,7 @@ export interface HypixelPlayerSocialMedia { /** Send an HTTP request to the Hypixel API */ export let sendApiRequest = async<P extends keyof typedHypixelApi.Requests>(path: P, options: typedHypixelApi.Requests[P]['options']): Promise<typedHypixelApi.Requests[P]['response']['data']> => { // Send a raw http request to api.hypixel.net, and return the parsed json + console.log('requesting', path, options) let response: typedHypixelApi.Requests[P]['response'] try { response = await typedHypixelApi.request( diff --git a/src/index.ts b/src/index.ts index 59e7761..1dcb523 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount, deleteSession, fetchPaginatedItemsAuctions } from './database.js' -import { fetchElection, fetchItemList, fetchMemberProfile, fetchUser } from './hypixel.js' +import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount, deleteSession, fetchPaginatedItemsAuctions, fetchItemsAuctions } from './database.js' +import { fetchAuctionUncached, fetchElection, fetchItemList, fetchMemberProfile, fetchUser } from './hypixel.js' import rateLimit from 'express-rate-limit' import * as constants from './constants.js' import * as discord from './discord.js' @@ -155,9 +155,11 @@ app.get('/election', async (req, res) => { app.get('/items', async (req, res) => { try { - res.json( - await fetchItemList() - ) + res + .setHeader('Cache-Control', 'public, max-age=600') + .json( + await fetchItemList() + ) } catch (err) { console.error(err) res.json({ ok: false }) @@ -166,11 +168,17 @@ app.get('/items', async (req, res) => { app.get('/auctionprices', async (req, res) => { + const itemIds = typeof req.query.items === 'string' ? req.query.items.split(',') : null + if (itemIds && itemIds.length > 100) + return res.json({ + ok: false, + error: 'More than 100 items in the items query parameter.' + }) try { res .setHeader('Cache-Control', 'public, max-age=600') .json( - await fetchPaginatedItemsAuctions(0, 100) + itemIds ? await fetchItemsAuctions(itemIds) : await fetchPaginatedItemsAuctions(0, 100) ) } catch (err) { console.error(err) @@ -178,6 +186,20 @@ app.get('/auctionprices', async (req, res) => { } }) +app.get('/auction/:uuid', async (req, res) => { + console.log('fetching auction', req.params.uuid) + const auction = await fetchAuctionUncached(req.params.uuid) + console.log('fetched auction', req.params.uuid) + try { + res + // .setHeader('Cache-Control', 'public, max-age=600') + .json(auction) + } catch (err) { + console.error(err) + res.json({ ok: false }) + } +}) + app.post('/accounts/createsession', async (req, res) => { try { const { code } = req.body |