-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (153 loc) · 3.75 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/jpeg"
_ "image/png"
"io"
"log"
"math/rand"
"net/http"
"os"
"text/template"
"time"
"github.com/joho/godotenv"
"github.com/shkh/lastfm-go/lastfm"
)
var (
trackName string
artistName string
imageData string
userUrl string
imageUrl string
theme string
bgUrl string
ContentTypePNG = "image/png"
ContentTypeJPEG = "image/jpeg"
ContentTypeGIF = "image/gif"
)
type TrackData struct {
TrackName string
ArtistName string
Image string
UserUrl string
ThemeName string
ThemeCss string
ImageBackground string
}
func init() {
if err := godotenv.Load(); err != nil {
log.Fatalf("No .env file found")
}
if os.Getenv("THEME") == "" {
theme = "default"
} else {
theme = os.Getenv("THEME")
}
}
func main() {
key := os.Getenv("KEY")
secret := os.Getenv("SECRET")
user := os.Getenv("USERNAME")
limit := os.Getenv("LIMIT")
userUrl = os.Getenv("YANDEX_URL")
bgUrl = os.Getenv("BG")
backgroundData, _ := imageToBackground(bgUrl)
http.HandleFunc("/yandex", func(w http.ResponseWriter, r *http.Request) {
api := lastfm.New(key, secret)
result, err := api.User.GetRecentTracks(lastfm.P{"limit": limit,
"user": user})
if err != nil {
return
}
for _, track := range result.Tracks {
if track.NowPlaying == "true" {
trackName = track.Name
artistName = track.Artist.Name
imageUrl = track.Images[3].Url
break
} else {
rand.Seed(time.Now().UnixNano())
track = result.Tracks[rand.Intn(len(result.Tracks))]
trackName = track.Name
artistName = track.Artist.Name
imageUrl = track.Images[3].Url
break
}
}
imageData, err = trackImageToBase64(imageUrl)
if err != nil {
log.Printf("image64 error: %v", err)
}
filepath := fmt.Sprintf("./themes/%s.css", theme)
themeCss, err := os.ReadFile(filepath)
if err != nil {
fmt.Printf("read file error: %v", err)
}
data := TrackData{
TrackName: trackName,
ArtistName: artistName,
Image: imageData,
UserUrl: userUrl,
ThemeName: theme,
ThemeCss: string(themeCss),
ImageBackground: backgroundData,
}
tmpl, err := template.ParseFiles("templates/index.html")
if err != nil {
fmt.Printf("template execution: %s", err)
}
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "max-age=0")
if err := tmpl.Execute(w, data); err != nil {
log.Printf("Error executing template: %v", err)
}
})
err := http.ListenAndServe(":1984", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func trackImageToBase64(url string) (string, error) {
responseImage, err := http.Get(url)
if err != nil || responseImage.StatusCode != 200 {
defaultImage, _ := os.ReadFile("themes/noImage")
return string(defaultImage), nil
}
defer responseImage.Body.Close()
imageBody, _, err := image.Decode(responseImage.Body)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, imageBody, nil)
if err != nil {
return "", err
}
imageByte := buf.Bytes()
imageData = base64.StdEncoding.EncodeToString(imageByte)
return imageData, nil
}
func imageToBackground(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
imageType := http.DetectContentType(data)
var buf bytes.Buffer
switch imageType {
case ContentTypePNG, ContentTypeJPEG, ContentTypeGIF:
buf.Write(data)
default:
return "", fmt.Errorf("unsupported image type: %s", imageType)
}
base64Str := base64.StdEncoding.EncodeToString(buf.Bytes())
return base64Str, nil
}