Initial commit
This commit is contained in:
109
main.go
Normal file
109
main.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", getRoot)
|
||||
http.HandleFunc("/favicon.ico", getRoot)
|
||||
http.HandleFunc("/{videoId}", getYouTubeThumbnail)
|
||||
|
||||
_ = http.ListenAndServe(":3333", nil)
|
||||
}
|
||||
|
||||
func getYouTubeThumbnail(writer http.ResponseWriter, request *http.Request) {
|
||||
videoId := request.PathValue("videoId")
|
||||
|
||||
resp, err := http.Get("https://img.youtube.com/vi/" + videoId + "/maxresdefault.jpg")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if resp.StatusCode == 200 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
_, _ = io.WriteString(writer, string(body))
|
||||
} else if resp.StatusCode == 404 {
|
||||
mqResp, err := http.Get("https://img.youtube.com/vi/" + videoId + "/mqdefault.jpg")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
imageMq, _, err := image.Decode(mqResp.Body)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(imageMq.Bounds().Max.X, imageMq.Bounds().Max.Y)
|
||||
|
||||
hqResp, err := http.Get("https://img.youtube.com/vi/" + videoId + "/hqdefault.jpg")
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
imageHq, _, err := image.Decode(hqResp.Body)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
imageMqBounds := imageMq.Bounds()
|
||||
imageHqBounds := imageHq.Bounds()
|
||||
|
||||
if imageMqBounds.Max.X > imageMqBounds.Max.Y {
|
||||
fmt.Println("Youtube thumbnail horizontal")
|
||||
fmt.Println("Ideal size")
|
||||
fmt.Print(imageHq.Bounds().Max.X, ", ")
|
||||
idealHeight := imageMqBounds.Max.Y * 100 / (imageMqBounds.Max.X * 100 / imageHqBounds.Max.X)
|
||||
fmt.Println(idealHeight)
|
||||
|
||||
newImage := image.NewRGBA64(image.Rectangle{
|
||||
Min: image.Point{},
|
||||
Max: image.Point{
|
||||
X: imageHqBounds.Max.X, Y: idealHeight,
|
||||
},
|
||||
})
|
||||
|
||||
for i := 0; i < idealHeight; i++ {
|
||||
for j := 0; j < imageHqBounds.Max.X; j++ {
|
||||
iHq := (imageHqBounds.Max.Y / 2) - (idealHeight / 2) + i
|
||||
r, g, b, a := imageHq.At(j, iHq).RGBA()
|
||||
newImage.SetRGBA64(j, i, color.RGBA64{
|
||||
R: uint16(r),
|
||||
G: uint16(g),
|
||||
B: uint16(b),
|
||||
A: uint16(a),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
err := jpeg.Encode(writer, newImage, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
} else {
|
||||
fmt.Println("Youtube thumbnail vertical")
|
||||
}
|
||||
} else {
|
||||
_, _ = io.WriteString(writer, "Something went wrong")
|
||||
}
|
||||
}
|
||||
|
||||
func getRoot(writer http.ResponseWriter, request *http.Request) {
|
||||
_, _ = io.WriteString(writer, "youtubethumbnails is a tool that allows you to get YouTube thumbnails. You can fetch thumbnails with the /{videoId} endpoint.")
|
||||
}
|
Reference in New Issue
Block a user