Skip to content

Instantly share code, notes, and snippets.

@arrow2nd
Last active February 2, 2023 03:31
Show Gist options
  • Save arrow2nd/feebf1df09e6ede4b03eb4bcd240c66c to your computer and use it in GitHub Desktop.
Save arrow2nd/feebf1df09e6ede4b03eb4bcd240c66c to your computer and use it in GitHub Desktop.
シャニの4コマ漫画をランダムで端末に出力するサンプル (sixel)
package main
import (
"encoding/json"
"fmt"
"image"
"image/jpeg"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/mattn/go-sixel"
)
type yonkomaResponse struct {
Category string `json:"category"`
Title string `json:"title"`
Number int `json:"number"`
Idols []string `json:"idols"`
Url string `json:"url"`
PhotoURL string `json:"photoUrl"`
PublishedUTC string `json:"publishedUtc"`
}
func main() {
raw, err := fetchYonkomaData()
if err != nil {
log.Fatalln(err)
}
rand.Seed(time.Now().UnixNano())
url := raw[rand.Intn(len(raw))].PhotoURL
fmt.Println(url)
img, err := fetchImage(url)
if err != nil {
log.Fatalln(err)
}
if err := sixel.NewEncoder(os.Stdout).Encode(img); err != nil {
log.Fatalln(err)
}
}
func fetchYonkomaData() ([]yonkomaResponse, error) {
res, err := http.Get("https://arrow2nd.github.io/shiny-yonkoma/yonkoma.json")
if err != nil {
return nil, err
}
defer res.Body.Close()
decorder := json.NewDecoder(res.Body)
raw := []yonkomaResponse{}
if err := decorder.Decode(&raw); err != nil {
return nil, err
}
return raw, nil
}
func fetchImage(url string) (image.Image, error) {
res, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
return jpeg.Decode(res.Body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment