-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import env from "@/globals/env" | ||
import database from "@/globals/database" | ||
import cache from "@/globals/cache" | ||
import getVersion from "@/index" | ||
import logger from "@/globals/logger" | ||
import * as sourcexchange from "@/globals/sourcexchange" | ||
import * as builtbybit from "@/globals/builtbybit" | ||
|
||
export const runContext = { | ||
/** | ||
* The Environment Variables of the Server | ||
* @since 1.0.0 | ||
*/ env, | ||
/** | ||
* The SQLite Database Connection | ||
* @since 1.0.0 | ||
*/ database, | ||
/** | ||
* The Redis Cache Client | ||
* @since 1.0.0 | ||
*/ cache, | ||
/** | ||
* The Logger | ||
* @since 1.0.0 | ||
*/ logger, | ||
/** | ||
* The SourceXchange Client | ||
* @since 1.0.0 | ||
*/ sourcexchange, | ||
/** | ||
* The BuiltByBit Client | ||
* @since 1.0.0 | ||
*/ builtbybit, | ||
/** | ||
* The App Version | ||
* @since 1.0.0 | ||
*/ appVersion: getVersion(), | ||
|
||
/** | ||
* Create Multi Line Strings | ||
* @since 1.0.0 | ||
*/ join(...strings: string[]): string { | ||
return strings.join('\n') | ||
} | ||
} as const | ||
|
||
export type CrontabContext = typeof runContext | ||
|
||
export default class Builder<Excluded extends (keyof Builder)[] = []> { | ||
protected interval: string = '* * * * *' | ||
protected listener: (ctx: typeof runContext) => any | Promise<any> = () => undefined | ||
|
||
/** | ||
* Set the Interval | ||
* @since 1.0.0 | ||
*/ public cron(interval: string): Omit<Builder<[...Excluded, 'cron']>, 'cron' | Excluded[number]> { | ||
this.interval = interval | ||
|
||
return this as any | ||
} | ||
|
||
/** | ||
* Listen for The Crontab | ||
* @since 1.0.0 | ||
*/ public listen(callback: (ctx: typeof runContext) => any | Promise<any>): Omit<Builder<[...Excluded, 'listen']>, 'listen' | Excluded[number]> { | ||
this.listener = callback as any | ||
|
||
return this as any | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import Crontab from "@/crontab" | ||
import logger from "@/globals/logger" | ||
import env from "@/globals/env" | ||
import { eq } from "drizzle-orm" | ||
|
||
let isRunning = false, blockRunning = false | ||
export default new Crontab() | ||
.cron(env.LOG_LEVEL === 'debug' ? '*/3 * * * * *' : '0 */6 * * *') | ||
.listen(async(ctx) => { | ||
if (isRunning || blockRunning) return | ||
isRunning = true | ||
|
||
if (env.LOG_LEVEL === 'debug') blockRunning = true | ||
|
||
logger() | ||
.text('Running Update Prices Schedule') | ||
.info() | ||
|
||
const [ extensions, sxcProducts ] = await Promise.all([ | ||
ctx.database.select({ | ||
id: ctx.database.schema.extensions.id, | ||
name: ctx.database.schema.extensions.name, | ||
platforms: ctx.database.schema.extensions.platforms, | ||
}) | ||
.from(ctx.database.schema.extensions) | ||
.where(eq(ctx.database.schema.extensions.pending, false)), | ||
ctx.sourcexchange.products() | ||
]) | ||
|
||
for (const extension of extensions) { | ||
if (!Object.keys(extension.platforms).filter((platform) => platform !== 'GITHUB').length) continue | ||
|
||
logger() | ||
.text('Updating Extension Prices of') | ||
.text(extension.name, (c) => c.cyan) | ||
.info() | ||
|
||
const platforms = Object.assign({}, extension.platforms) | ||
|
||
if (extension.platforms.SOURCEXCHANGE) { | ||
const product = sxcProducts.find((product) => product.url === extension.platforms.SOURCEXCHANGE.url) | ||
|
||
if (product) { | ||
platforms.SOURCEXCHANGE = { | ||
url: extension.platforms.SOURCEXCHANGE.url, | ||
price: product.price, | ||
currency: product.currency | ||
} | ||
} | ||
} | ||
|
||
if (extension.platforms.BUILTBYBIT) { | ||
const product = await ctx.builtbybit.product(parseInt(extension.platforms.BUILTBYBIT.url.split('.').pop() as string)) | ||
|
||
if (product) { | ||
platforms.BUILTBYBIT = { | ||
url: extension.platforms.BUILTBYBIT.url, | ||
price: product.price, | ||
currency: product.currency | ||
} | ||
} | ||
} | ||
|
||
if (JSON.stringify(platforms) !== JSON.stringify(extension.platforms)) { | ||
await ctx.database.update(ctx.database.schema.extensions) | ||
.set({ | ||
platforms | ||
}) | ||
.where(eq(ctx.database.schema.extensions.id, extension.id)) | ||
} | ||
|
||
logger() | ||
.text('Updated Extension Prices of') | ||
.text(extension.name, (c) => c.cyan) | ||
.info() | ||
} | ||
|
||
logger() | ||
.text('Finished Update Prices Schedule') | ||
.info() | ||
|
||
isRunning = false | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import env from "@/globals/env" | ||
import { Currency } from "@/schema" | ||
|
||
export type BBBProduct = { | ||
price: number | ||
currency: Currency | ||
} | ||
|
||
/** | ||
* Get Information for a Blueprint Product on BuiltByBit | ||
* @since 1.0.0 | ||
*/ export async function product(product: number) { | ||
if (!env.BBB_TOKEN) return null | ||
|
||
const data = await fetch(`https://api.builtbybit.com/v1/resources/${product}`, { | ||
headers: { | ||
Authorization: `Private ${env.BBB_TOKEN}`, | ||
Accept: 'application/json' | ||
} | ||
}).then((res) => res.json()).catch(() => ({ data: null })) as { data: BBBProduct } | ||
|
||
return data.data | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import env from "@/globals/env" | ||
import { Currency } from "@/schema" | ||
|
||
export type SXCProduct = { | ||
price: number | ||
currency: Currency | ||
url: string | ||
} | ||
|
||
/** | ||
* Get all Blueprint products on Sourcexchange | ||
* @since 1.0.0 | ||
*/ export async function products() { | ||
if (!env.SXC_TOKEN) return [] | ||
|
||
const data = await fetch('https://www.sourcexchange.net/api/products/blueprint', { | ||
headers: { | ||
Authorization: `Bearer ${env.SXC_TOKEN}`, | ||
Accept: 'application/json' | ||
} | ||
}).then((res) => res.json()).catch(() => ({ data: [] })) as { data: SXCProduct[] } | ||
|
||
return data.data | ||
} |
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
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