-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratePokebase.js
136 lines (117 loc) · 3.93 KB
/
generatePokebase.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require('fs')
const path = require('path')
const https = require('https')
main()
async function main () {
let data
if (process.env.ONLINE) {
data = await getData()
} else {
const jsonFile = path.join(__dirname, './source.json')
data = JSON.parse(fs.readFileSync(jsonFile, 'utf8'))
}
processData(data)
}
async function getData () {
return new Promise((resolve, reject) => {
const endpoint = 'https://beta.pokeapi.co/'
const query = `query gottaCatchThemAll {
pokemon: pokemon_v2_pokemon{
id
name
height
weight
types: pokemon_v2_pokemontypes {
type: pokemon_v2_type {
name
id
}
}
images: pokemon_v2_pokemonsprites {
sprites
}
specy:pokemon_v2_pokemonspecy {
generation_id
is_baby
is_legendary
is_mythical
color:pokemon_v2_pokemoncolor {
name
id
}
evolutions: pokemon_v2_evolutionchain {
baby_trigger_item_id
id
chain: pokemon_v2_pokemonspecies {
id
order
}
}
}
}
}`
const options = {
method: 'POST',
path: '/graphql/v1beta',
headers: {
'Content-Type': 'application/json',
'X-Method-Used': 'graphiql'
}
}
const request = https.request(endpoint, options, response => {
let data = ''
response.on('data', chunk => {
data += chunk
})
response.on('end', () => {
resolve(JSON.parse(data))
})
response.on('error', reject)
})
request.write(JSON.stringify({ query }))
request.end()
})
}
function processData (data) {
const sqlColors = new Set()
const sqlTypes = new Set()
const sqlEvolutionSteps = new Set()
const sqlEvolutionChain = new Set()
const sqlPokemon = new Set()
// Generate the INSERT statements
data.data.pokemon.forEach(pokemon => {
const name = pokemon.name
const height = pokemon.height
const weight = pokemon.weight
const isBaby = pokemon.specy.is_baby
const isLegendary = pokemon.specy.is_legendary
const isMythical = pokemon.specy.is_mythical
const colorId = pokemon.specy.color.id
const gen = pokemon.specy.generation_id
const evolutionChainId = pokemon.specy.evolutions?.id || null
// const link = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png`
const link = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${pokemon.id}.png`
sqlColors.add(`INSERT INTO color (id, name) VALUES (${colorId}, '${pokemon.specy.color.name}');`)
if (evolutionChainId) {
sqlEvolutionChain.add(`INSERT INTO evolution_chain (id) VALUES (${evolutionChainId});`)
pokemon.specy.evolutions.chain.forEach(evo => {
sqlEvolutionSteps.add(`INSERT INTO evolution_step ("order", evolution_chain_id, pokemon_id) VALUES (${evo.order}, ${evolutionChainId}, ${evo.id});`)
})
}
sqlPokemon.add(`INSERT INTO pokemon (id, name, height, weight, is_baby, generation, is_legendary, is_mythical, color_id, evolution_chain_id) VALUES (${pokemon.id}, '${name}', ${height}, ${weight}, ${isBaby}, ${gen}, ${isLegendary}, ${isMythical}, ${colorId}, ${evolutionChainId});`)
sqlPokemon.add(`INSERT INTO picture (pokemon_id, url) VALUES (${pokemon.id}, '${link}');`)
pokemon.types.forEach(type => {
const typeId = type.type.id
sqlTypes.add(`INSERT INTO element (id, name) VALUES (${typeId}, '${type.type.name}');`)
sqlPokemon.add(`INSERT INTO pokemon_element (pokemon_id, element_id) VALUES (${pokemon.id}, ${typeId});`)
})
})
// Write the INSERT statements to a file
fs.writeFileSync(path.join(__dirname, '../migrations/002.do.data.sql'), [
...[...sqlColors].sort(),
...[...sqlTypes].sort(),
...[...sqlEvolutionChain].sort(),
...sqlEvolutionSteps,
...sqlPokemon
].join('\n'))
}