Initial Commit
This commit is contained in:
commit
376dc2591d
5 changed files with 126 additions and 0 deletions
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module mii-fetch
|
||||
|
||||
go 1.22
|
||||
36
handlers/mii.go
Normal file
36
handlers/mii.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"mii-fetch/services"
|
||||
)
|
||||
|
||||
func MiiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
if len(parts) < 2 {
|
||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
pid := parts[0]
|
||||
filename := parts[1]
|
||||
|
||||
// block favicon cuz fuck u
|
||||
if strings.Contains(pid, "favicon") || strings.Contains(filename, "favicon") {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
imgBytes, err := services.FetchMii(pid)
|
||||
if err != nil {
|
||||
http.Error(w, err.Message, err.Code)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
w.Write(imgBytes)
|
||||
}
|
||||
19
main.go
Normal file
19
main.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"mii-fetch/handlers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", handlers.MiiHandler)
|
||||
|
||||
log.Println("server running on :80")
|
||||
err := http.ListenAndServe(":80", mux)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
67
services/mii_handler.go
Normal file
67
services/mii_handler.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServiceError struct {
|
||||
Message string
|
||||
Code int
|
||||
}
|
||||
|
||||
func FetchMii(pid string) ([]byte, *ServiceError) {
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
spfnURL := fmt.Sprintf("https://account.spfn.net/api/v2/users/%s/mii", pid)
|
||||
|
||||
req, _ := http.NewRequest("GET", spfnURL, nil)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("User-Agent", "what we think about v/s 6.0?")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, &ServiceError{"internal server error", 500}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, &ServiceError{"user not found", 404}
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
var miiData string
|
||||
if err := json.Unmarshal(body, &miiData); err != nil {
|
||||
return nil, &ServiceError{"invalid response", 500}
|
||||
}
|
||||
|
||||
miiData = strings.TrimSpace(miiData)
|
||||
miiData = strings.ReplaceAll(miiData, "\r", "")
|
||||
miiData = strings.ReplaceAll(miiData, "\n", "")
|
||||
|
||||
encoded := url.QueryEscape(miiData)
|
||||
|
||||
renderURL := fmt.Sprintf(
|
||||
"https://mii-unsecure.ariankordi.net/miis/image.png?data=%s&resourceType=very_high&type=face&width=256",
|
||||
encoded,
|
||||
)
|
||||
|
||||
imgResp, err := client.Get(renderURL)
|
||||
if err != nil {
|
||||
return nil, &ServiceError{"renderer failed", 502}
|
||||
}
|
||||
defer imgResp.Body.Close()
|
||||
|
||||
if imgResp.StatusCode != 200 {
|
||||
return nil, &ServiceError{"mii renderer failed", 502}
|
||||
}
|
||||
|
||||
imgBytes, _ := io.ReadAll(imgResp.Body)
|
||||
return imgBytes, nil
|
||||
}
|
||||
1
utils/http.go
Normal file
1
utils/http.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package utils
|
||||
Loading…
Reference in a new issue