generated from alecodes/base-template
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
synchronizator "git.alecodes.page/alecodes/synchronizator/pkg"
|
|
)
|
|
|
|
type PokeApiResponse[T any] struct {
|
|
Count int `json:"count"`
|
|
Next string `json:"next"`
|
|
Previous string `json:"previous"`
|
|
Results []T `json:"results"`
|
|
}
|
|
|
|
type Pokedex struct {
|
|
Name string `json:"name"`
|
|
Url string `json:"url"`
|
|
}
|
|
|
|
type Pokemon struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func getPokemons(
|
|
pagination synchronizator.Pagination,
|
|
) ([]*synchronizator.Collection, synchronizator.Pagination, error) {
|
|
var collections []*synchronizator.Collection
|
|
|
|
params := url.Values{}
|
|
params.Add("offset", strconv.Itoa(pagination.Offset))
|
|
params.Add("limit", strconv.Itoa(pagination.Limit))
|
|
|
|
resp, err := http.Get("https://pokeapi.co/api/v2/pokedex?" + params.Encode())
|
|
if err != nil {
|
|
return nil, pagination, err
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
|
|
var data PokeApiResponse[Pokedex]
|
|
err = json.Unmarshal(body, &data)
|
|
if err != nil {
|
|
return nil, pagination, err
|
|
}
|
|
|
|
collections = make([]*synchronizator.Collection, 0, len(data.Results))
|
|
|
|
for _, pokedex := range data.Results {
|
|
collection_name := "Pokedex_" + pokedex.Name
|
|
collection := synchronizator.NewCollection(collection_name, nil)
|
|
if err != nil {
|
|
return nil, pagination, err
|
|
}
|
|
collections = append(collections, collection)
|
|
}
|
|
|
|
// fmt.Println(data)
|
|
|
|
pagination.Offset += pagination.Limit
|
|
pagination.HasMore = data.Next != ""
|
|
pagination.Total = data.Count
|
|
|
|
return collections, pagination, nil
|
|
}
|
|
|
|
func main() {
|
|
connection, err := sql.Open("sqlite", "db.sql")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
|
|
return
|
|
}
|
|
|
|
defer connection.Close()
|
|
|
|
opts := synchronizator.DefaultOptions
|
|
opts.Log_level = synchronizator.DEBUG
|
|
opts.DANGEROUSLY_DROP_TABLES = true
|
|
|
|
sync, err := synchronizator.New(connection, opts)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
|
|
return
|
|
}
|
|
|
|
pokeApi, err := sync.NewPlatform("pokeapi", nil)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
err = pokeApi.FetchCollections(getPokemons, synchronizator.StartPagination)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(pokeApi)
|
|
}
|