-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (117 loc) · 2.1 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"github.com/mattn/go-sixel"
"github.com/nfnt/resize"
)
func nyanko() (string, error) {
resp, err := http.Get("https://aws.random.cat/meow")
if err != nil {
return "", err
}
defer resp.Body.Close()
var r struct {
File string `json:"file"`
}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return "", err
}
return r.File, nil
}
func innu() (string, error) {
resp, err := http.Get("https://random.dog/woof.json")
if err != nil {
return "", err
}
defer resp.Body.Close()
var r struct {
URL string `json:"url"`
}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return "", err
}
return r.URL, nil
}
func shiba() (string, error) {
resp, err := http.Get("http://shibe.online/api/shibes")
if err != nil {
return "", err
}
defer resp.Body.Close()
var r []string
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return "", err
}
return r[0], nil
}
func tori() (string, error) {
resp, err := http.Get("https://some-random-api.ml/img/birb")
if err != nil {
return "", err
}
defer resp.Body.Close()
var r struct {
URL string `json:"link"`
}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return "", err
}
return r.URL, nil
}
func main() {
exe, err := os.Executable()
if err != nil {
log.Fatal(err)
}
var uri string
switch filepath.Base(exe) {
default:
fallthrough
case "nyanko":
uri, err = nyanko()
case "innu":
uri, err = innu()
case "shiba":
uri, err = shiba()
case "tori":
uri, err = tori()
}
if err != nil {
log.Fatal(err)
}
resp, err := http.Get(uri)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
img, _, err := image.Decode(bytes.NewReader(b))
if err != nil {
log.Fatal(err)
}
img = resize.Resize(600, 0, img, resize.Lanczos3)
err = sixel.NewEncoder(os.Stdout).Encode(img)
if err != nil {
log.Fatal(err)
}
fmt.Println()
fmt.Println(uri)
}