Added fetching scripts
This commit is contained in:
18
scripts/utils/promiseAllStepN.ts
Normal file
18
scripts/utils/promiseAllStepN.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export const promiseAllStepN = async <T>(n: number, list: (() => Promise<T>)[]) => {
|
||||
const head = list.slice(0, n)
|
||||
const tail = list.slice(n)
|
||||
const result: T[] = []
|
||||
const execute = async (promise: () => Promise<T>, i: number, runNext: () => Promise<void>) => {
|
||||
result[i] = await promise()
|
||||
await runNext()
|
||||
}
|
||||
const runNext = async () => {
|
||||
const i = list.length - tail.length
|
||||
const promise = tail.shift()
|
||||
if (promise !== undefined) {
|
||||
await execute(promise, i, runNext)
|
||||
}
|
||||
}
|
||||
await Promise.all(head.map((promise, i) => execute(promise, i, runNext)))
|
||||
return result
|
||||
}
|
||||
19
scripts/utils/retrieveRecipes.ts
Normal file
19
scripts/utils/retrieveRecipes.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import fetch from 'node-fetch'
|
||||
import { parse } from 'node-html-parser'
|
||||
import { UnfetchedEntity } from './types'
|
||||
|
||||
export const retrieveRecipes = async (lang?: string) => {
|
||||
const res = await fetch(
|
||||
`https://wiki.factorio.com/Materials_and_recipes${lang ? `/${lang}` : ''}`
|
||||
)
|
||||
const html = await res.text()
|
||||
const root = parse(html)
|
||||
const icons = root.querySelectorAll('.tab > div > div.factorio-icon > a')
|
||||
return icons
|
||||
.map(icon => ({
|
||||
name: icon.attrs.title,
|
||||
href: icon.attrs.href,
|
||||
image: icon.querySelector('img')?.attrs.src
|
||||
}))
|
||||
.filter((entity): entity is UnfetchedEntity => !!(entity.href && entity.name && entity.image))
|
||||
}
|
||||
15
scripts/utils/types.ts
Normal file
15
scripts/utils/types.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface Recipe {
|
||||
prerequisites: Record<string, number>
|
||||
time: number
|
||||
output: Record<string, number>
|
||||
}
|
||||
|
||||
export interface UnfetchedEntity {
|
||||
name: string
|
||||
image: string
|
||||
href: string
|
||||
}
|
||||
|
||||
export interface Entity extends UnfetchedEntity {
|
||||
recipe?: Recipe
|
||||
}
|
||||
Reference in New Issue
Block a user