-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (75 loc) · 1.58 KB
/
main.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"net/http"
"os"
"github.com/mattn/go-sixel"
)
type CardImage struct {
ID int `json:"id"`
URL string `json:"image_url"`
URLSmall string `json:"image_url_small"`
URLCropped string `json:"image_url_cropped"`
}
type Card struct {
ID int `json:"id"`
Name string `json:"name"`
Images []CardImage `json:"card_images"`
}
type CardResponse struct {
Data []Card `json:"data"`
}
func getYuGiOhCardByRandom() (Card, error) {
resp, err := http.Get("https://db.ygoprodeck.com/api/v7/cardinfo.php?num=1&offset=0&sort=random&cachebust")
if err != nil {
return Card{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Card{}, err
}
var cardResponse CardResponse
if err := json.Unmarshal(body, &cardResponse); err != nil {
return Card{}, err
}
if len(cardResponse.Data) == 0 {
return Card{}, fmt.Errorf("card not found")
}
return cardResponse.Data[0], nil
}
func main() {
card, err := getYuGiOhCardByRandom()
if err != nil {
log.Fatal(err)
}
if len(card.Images) == 0 {
log.Fatal("card has no images")
}
resp, err := http.Get(card.Images[0].URL)
if err != nil {
log.Fatal(err)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
img, _, err := image.Decode(bytes.NewReader(b))
if err != nil {
log.Fatal(err)
}
err = sixel.NewEncoder(os.Stdout).Encode(img)
if err != nil {
log.Fatal(err)
}
fmt.Println()
fmt.Println(card.Images[0].URL)
}