Add .file class + Generator user agent

This commit is contained in:
venashial
2022-07-02 22:14:20 -07:00
parent 64ed5fca3b
commit f62723c274
19 changed files with 198 additions and 106 deletions

View File

@@ -0,0 +1,21 @@
import { fetch as baseFetch } from 'undici'
import { promises as fs } from 'fs'
const API_URL =
process.env.VITE_API_URL && process.env.VITE_API_URL === 'https://staging-api.modrinth.com/v2/'
? 'https://staging-api.modrinth.com/v2/'
: 'https://api.modrinth.com/v2/'
let version = ''
export async function fetch(route, options = {}) {
if (!version) {
version = JSON.parse(await fs.readFile('./package.json', 'utf8')).version
}
return baseFetch(API_URL + route, {
...options,
headers: {
'user-agent': `Omorphia / ${version} (venashial@modrinth.com)`,
},
})
}

View File

@@ -5,11 +5,6 @@ import { gameVersions } from './outputs/gameVersions.js'
import { tags } from './outputs/tags.js'
import { openapi } from './outputs/openapi.js'
const API_URL =
process.env.VITE_API_URL && process.env.VITE_API_URL === 'https://staging-api.modrinth.com/v2/'
? 'https://staging-api.modrinth.com/v2/'
: 'https://api.modrinth.com/v2/'
// Time to live: 7 days
const TTL = 7 * 24 * 60 * 60 * 1000
@@ -56,11 +51,11 @@ export default function Generator(options) {
await fs.writeFile('./generated/state.json', JSON.stringify(state, null, 2))
if (options.tags) await tags(API_URL)
if (options.landingPage) await landingPage(API_URL)
if (options.gameVersions) await gameVersions(API_URL)
if (options.openapi) await openapi(API_URL)
if (options.projectColors) await projectColors(API_URL)
if (options.tags) await tags()
if (options.landingPage) await landingPage()
if (options.gameVersions) await gameVersions()
if (options.openapi) await openapi()
if (options.projectColors) await projectColors()
},
}
}

View File

@@ -1,8 +1,8 @@
import { fetch } from 'undici'
import { fetch } from '../fetch.js'
import { promises as fs } from 'fs'
import cliProgress from 'cli-progress'
export async function gameVersions(API_URL) {
export async function gameVersions() {
const progressBar = new cliProgress.SingleBar({
format: 'Generating game versions | {bar} | {percentage}%',
barCompleteChar: '\u2588',
@@ -11,7 +11,7 @@ export async function gameVersions(API_URL) {
})
progressBar.start(2, 0)
const gameVersions = await (await fetch(API_URL + 'tag/game_version')).json()
const gameVersions = await (await fetch('tag/game_version')).json()
progressBar.increment()
// Write JSON file

View File

@@ -1,8 +1,8 @@
import { fetch } from 'undici'
import { fetch } from '../fetch.js'
import { promises as fs } from 'fs'
import cliProgress from 'cli-progress'
export async function landingPage(API_URL) {
export async function landingPage() {
const progressBar = new cliProgress.SingleBar({
format: 'Generating landing page | {bar} | {percentage}% || {value}/{total} mods',
barCompleteChar: '\u2588',
@@ -12,9 +12,7 @@ export async function landingPage(API_URL) {
progressBar.start(100, 0)
// Fetch top 100 mods
const response = await (
await fetch(API_URL + 'search?limit=100&facets=[["project_type:mod"]]')
).json()
const response = await (await fetch('search?limit=100&facets=[["project_type:mod"]]')).json()
// Simplified array with the format: ['id', 'slug', 'icon_extension']
const compressed = response.hits

View File

@@ -1,11 +1,11 @@
import { fetch } from 'undici'
import { fetch } from '../fetch.js'
import { createWriteStream } from 'fs'
import cliProgress from 'cli-progress'
import Jimp from 'jimp'
import { getAverageColor } from 'fast-average-color-node'
// Note: This function has issues and will occasionally fail with some project icons. It averages at a 99.4% success rate. Most issues are from ECONNRESET errors & Jimp not being able to handle webp & svg images.
export async function projectColors(API_URL) {
export async function projectColors() {
const progressBar = new cliProgress.SingleBar({
format: 'Generating project colors | {bar} | {percentage}% || {value}/{total} projects',
barCompleteChar: '\u2588',
@@ -13,7 +13,7 @@ export async function projectColors(API_URL) {
hideCursor: true,
})
// Get total number of projects
const projectCount = (await (await fetch(API_URL + 'search?limit=0')).json()).total_hits
const projectCount = (await (await fetch('search?limit=0')).json()).total_hits
progressBar.start(projectCount, 0)
const writeStream = createWriteStream('./generated/projects.json')
writeStream.write('{')
@@ -24,7 +24,7 @@ export async function projectColors(API_URL) {
const requestCount = Math.ceil(projectCount / 100)
await Promise.allSettled(
Array.from({ length: requestCount }, async (_, index) => {
const response = await fetch(API_URL + `search?limit=100&offset=${index * 100}`)
const response = await fetch(`search?limit=100&offset=${index * 100}`)
if (!response.ok) {
throw new Error(`Failed to fetch projects: ${response.statusText}`)
}

View File

@@ -1,7 +1,8 @@
import { fetch } from 'undici'
import { fetch } from '../fetch.js'
import { promises as fs } from 'fs'
import cliProgress from 'cli-progress'
export async function tags(API_URL) {
export async function tags() {
const progressBar = new cliProgress.SingleBar({
format: 'Generating tags | {bar} | {percentage}%',
barCompleteChar: '\u2588',
@@ -12,11 +13,11 @@ export async function tags(API_URL) {
// eslint-disable-next-line prefer-const
let [categories, loaders, licenses, donationPlatforms, reportTypes] = await Promise.all([
await (await fetch(API_URL + 'tag/category')).json(),
await (await fetch(API_URL + 'tag/loader')).json(),
await (await fetch(API_URL + 'tag/license')).json(),
await (await fetch(API_URL + 'tag/donation_platform')).json(),
await (await fetch(API_URL + 'tag/report_type')).json(),
await (await fetch('tag/category')).json(),
await (await fetch('tag/loader')).json(),
await (await fetch('tag/license')).json(),
await (await fetch('tag/donation_platform')).json(),
await (await fetch('tag/report_type')).json(),
])
progressBar.update(5)