/
planarExtraction.ts
94 lines (81 loc) · 2.39 KB
/
planarExtraction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/// <reference types="bun-types" />
import { join } from 'node:path';
export { };
interface ScryfallCard {
oracle_id: string;
name: string;
type_line: string;
oracle_text: string;
image_uris: { normal: string };
set: string;
set_name: string;
security_stamp?: string;
}
interface ScryfallResponse {
total_cards: number;
has_more: boolean;
next_page?: string;
data: Array<ScryfallCard>;
}
interface Card {
id: string;
name: string;
typeLine: string;
oracleText: string;
sets: Array<string>;
isUB: boolean;
}
interface Set {
code: string;
name: string;
}
const CARD_DIR = join(import.meta.dir, 'public', 'cards');
const API_SEARCH = 'https://api.scryfall.com/cards/search?q=(t%3Aplane+or+t%3Aphenomenon)+game%3Apaper+-is%3Apromo&unique=prints&order=released';
const CARD_BACK = 'https://backs.scryfall.io/large/7/8/7840c131-f96b-4700-9347-2215c43156e6.jpg';
async function fetchAllPrints(): Promise<Array<ScryfallCard>> {
let nextPage: string | undefined = API_SEARCH;
const cards: Array<ScryfallCard> = [];
while (nextPage) {
const raw: ScryfallResponse = await (await fetch(nextPage)).json();
cards.push(...raw.data);
nextPage = raw.next_page;
}
return cards;
}
async function buildImg(url: string, out: string): Promise<void> {
const proc = Bun.spawn(
[ 'convert', '-', '-resize', '680', '-rotate', '90', out ],
{ stdin: 'pipe' },
);
proc.stdin.write(await (await fetch(url)).arrayBuffer());
proc.stdin.end();
await proc.exited;
}
const cards = new Map<string, Card>();
const sets = new Map<string, Set>();
for (const print of await fetchAllPrints()) {
const card = cards.get(print.oracle_id);
if (!sets.has(print.set)) {
sets.set(print.set, {
code: print.set,
name: print.set_name,
});
}
if (card) {
card.sets.push(print.set);
} else {
cards.set(print.oracle_id, {
id: print.oracle_id,
name: print.name,
typeLine: print.type_line,
oracleText: print.oracle_text,
sets: [print.set],
isUB: print.security_stamp === 'triangle',
});
await buildImg(print.image_uris.normal, join(CARD_DIR, `${print.oracle_id}.jpg`));
console.log(`Built ${print.name}`);
}
}
await buildImg(CARD_BACK, join(CARD_DIR, 'back.jpg'));
await Bun.write('./src/assets/cards.json', JSON.stringify([...cards.values()]));
await Bun.write('./src/assets/sets.json', JSON.stringify([...sets.values()]));