10000 Use static file for inspirobot · victrme/i18n-quotes@e9778b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e9778b6

Browse files
committed
Use static file for inspirobot
1 parent e619c7e commit e9778b6

File tree

3 files changed

+43
-85
lines changed

3 files changed

+43
-85
lines changed

cloudflare/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
}
2626

2727
case 'inspirobot': {
28-
return new Response(JSON.stringify(await inspirobot()), { headers })
28+
return new Response(JSON.stringify(await inspirobot(20)), { headers })
2929
}
3030

3131
default:

src/index.test.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest'
2-
import { Quotes, classic, kaamelott, inspirobot } from '.'
2+
import { Langs, Quote, classic, kaamelott, inspirobot } from '.'
33

44
describe('Classic', () => {
55
it('has valid type', async () => {
@@ -25,7 +25,7 @@ describe('Classic', () => {
2525
})
2626

2727
it('all langs are working', async () => {
28-
const langs: Quotes.Langs[] = ['en', 'fr', 'de', 'it', 'nl', 'pl', 'ru', 'sv']
28+
const langs: Langs[] = ['en', 'fr', 'de', 'it', 'nl', 'pl', 'ru', 'sv']
2929
let strings: string[] = []
3030
let string = ''
3131

@@ -62,12 +62,7 @@ describe('Kaamelott', function () {
6262
})
6363

6464
describe('Inspirobot', function () {
65-
let list: Quotes.List = []
66-
67-
it('connects to inspirobot servers', async function () {
68-
const response = await fetch('https://inspirobot.me/api?getSessionID=1')
69-
expect(response.status).toBe(200)
70-
})
65+
let list: Quote[] = []
7166

7267
it('has valid type', async function () {
7368
list = await inspirobot()
@@ -92,7 +87,7 @@ function isOfTypeQuotesList(list: any): boolean {
9287
)
9388
}
9489

95-
function quotesToString(list: Quotes.List) {
90+
function quotesToString(list: Quote[]) {
9691
return list
9792
.map((a) => a.content + a.author)
9893
.flat()

src/index.ts

+38-75
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,62 @@
1-
export declare namespace Quotes {
2-
interface Item {
3-
author: string
4-
content: string
5-
}
6-
7-
type List = Item[]
8-
9-
type Langs = 'en' | 'fr' | 'de' | 'it' | 'nl' | 'pl' | 'ru' | 'sv'
10-
11-
type Classic = { type: 'classic'; lang: Langs }
12-
type Kaamelott = { type: 'kaamelott' }
13-
type Inspirobot = { type: 'inspirobot' }
14-
15-
type Type = Classic | Kaamelott | Inspirobot
16-
}
1+
export type Langs = 'en' | 'fr' | 'de' | 'it' | 'nl' | 'pl' | 'ru' | 'sv'
2+
export type Quote = { author: string; content: string }
173

18-
interface Inspirobot {
19-
data: {
20-
text?: string
21-
type?: string
22-
}[]
23-
}
24-
25-
const QUOTES_VERSION: string = '13122023'
26-
const CDN_BASE_URL: string = 'https://cdn.jsdelivr.net/gh/victrme/i18n-quotes@main/quotes/'
27-
28-
//
29-
//
30-
//
31-
32-
export async function classic(lang: string, amount?: number): Promise<Quotes.List> {
33-
return await jsonFileQuotes(validLang(lang), amount)
34-
}
4+
const QUOTES_VERSION = '15102024'
5+
const LANGS: Langs[] = ['en', 'fr', 'de', 'it', 'nl', 'pl', 'ru', 'sv']
356

36-
export async function kaamelott(amount?: number): Promise<Quotes.List> {
37-
return await jsonFileQuotes('kaamelott', amount)
38-
}
39-
40-
export async function inspirobot(): Promise<Quotes.List> {
41-
const promises = []
42-
let list: Quotes.List = []
43-
44-
for (let i = 0; i < 6; i++) {
45-
promises.push(fetch('https://inspirobot.me/api?generateFlow=1'))
46-
}
7+
export async function classic(lang: string, amount?: number): Promise<Quote[]> {
8+
const base = 'https://cdn.jsdelivr.net/gh/victrme/i18n-quotes@main/quotes/'
9+
const filename = validLang(lang) + '.json?v='
10+
const filepath = base + filename + QUOTES_VERSION
11+
const resp = await fetch(filepath)
12+
const json = await resp.json()
4713

48-
for (const response of await Promise.all(promises)) {
49-
if (response.status === 200) {
50-
const json = await response.json()
51-
list = list.concat(sanitizeInspirobotData(json.data))
52-
}
53-
}
14+
if (amount && amount > 0) return getRandomSample(json, amount)
15+
if (amount === 0) return []
5416

55-
return list
17+
return json
5618
}
5719

58-
//
59-
// Helpers
60-
//
61-
62-
async function jsonFileQuotes(filename: Quotes.Langs | 'kaamelott', amount?: number): Promise<Quotes.List> {
63-
const path = `${CDN_BASE_URL}${filename}.json?v=${QUOTES_VERSION}`
64-
const resp = await fetch(path)
20+
export async function kaamelott(amount?: number): Promise<Quote[]> {
21+
const base = 'https://cdn.jsdelivr.net/gh/victrme/i18n-quotes@main/quotes/'
22+
const filepath = base + 'kaamelott.json?v=' + QUOTES_VERSION
23+
const resp = await fetch(filepath)
6524
const json = await resp.json()
6625

67-
return amount === undefined ? json : getRandomSample(json, amount)
26+
if (amount && amount > 0) return getRandomSample(json, amount)
27+
if (amount === 0) return []
28+
29+
return json
6830
}
6931

70-
function sanitizeInspirobotData(data: Inspirobot['data']): Quotes.List {
71-
const result = []
32+
export async function inspirobot(amount?: number): Promise<Quote[]> {
33+
const base = 'https://raw.githubusercontent.com/victrme/i18n-quotes/refs/heads/main/quotes/'
34+
const filepath = base + 'inspirobot.txt?v=' + QUOTES_VERSION
35+
const resp = await fetch(filepath)
36+
const text = await resp.text()
7237

73-
for (const { type, text } of data) {
74-
const isQuote = type === 'quote'
75-
const isValid = text && !text.includes('[') && text.length < 100
38+
const arr = text.split('\n')
39+
const json: Quote[] = arr.map((line) => ({ author: 'Inspirobot', content: line }))
7640

77-
if (isQuote && isValid) {
78-
result.push({ author: 'Inspirobot', content: text })
79-
}
80-
}
41+
if (amount && amount > 0) return getRandomSample(json, amount)
42+
if (amount === 0) return []
8143

82-
return result
44+
return json
8345
}
8446

85-
function getRandomSample(list: Quotes.List, amount: number): Quotes.List {
86-
let result: typeof list = []
47+
function getRandomSample(list: Quote[], amount: number): Quote[] {
48+
let result: Quote[] = []
49+
let random = 0
8750

8851
for (let i = 0; i < amount; i++) {
89-
result.push(list[Math.floor(Math.random() * list.length)])
52+
random = Math.floor(Math.random() * list.length)
53+
result.push(list[random])
9054
}
9155

9256
return result
9357
}
9458

95-
function validLang(lang: string): Quotes.Langs {
96-
const langs: Quotes.Langs[] = ['en', 'fr', 'de', 'it', 'nl', 'pl', 'ru', 'sv']
97-
const isLang = (l: string): l is Quotes.Langs => langs.includes(l as any)
59+
function validLang(lang: string): Langs {
60+
const isLang = (l: string): l is Langs => LANGS.includes(l as any)
9861
return isLang(lang) ? lang : 'en'
9962
}

0 commit comments

Comments
 (0)
0