add lastfm last track
parent
6691fea238
commit
dd0f516fb6
|
@ -75,6 +75,25 @@ pre {
|
|||
min-width: calc(100% - 20px);
|
||||
}
|
||||
|
||||
.lastfm-container {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.lastfm-text {
|
||||
font-weight: bold;
|
||||
color: purple;
|
||||
animation: scalePulse 2s infinite ease-in-out;
|
||||
}
|
||||
|
||||
@keyframes scalePulse {
|
||||
0%, 100% {
|
||||
transform: scale(1);
|
||||
}
|
||||
50% {
|
||||
transform: scale(1.05);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 800px) {
|
||||
header, footer, main {
|
||||
flex: 1 100%;
|
||||
|
|
5
main.go
5
main.go
|
@ -59,6 +59,11 @@ func setupRoutes(app *models.App) *http.ServeMux {
|
|||
router.Handle(postLink, m(controllers_pages.PostPageHandler(app)))
|
||||
}
|
||||
|
||||
// Api
|
||||
{
|
||||
router.Handle("/api/lastfm", m(controllers.LastfmHandler(app)))
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/mvc/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Обработчик главной страницы
|
||||
func LastfmHandler(app *models.App) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
|
||||
resp, err := models.GetRecentTracks("s3rr", "bc5ce9ff9289ac925841cf483e211fc1")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
data := fmt.Sprintf("%s - %s", resp.RecentTracks.Track[0].Name, resp.RecentTracks.Track[0].Artist.Name)
|
||||
|
||||
sendLastfm(w, data)
|
||||
})
|
||||
}
|
||||
|
||||
// Отправляет страницу
|
||||
func sendLastfm(w http.ResponseWriter, data string) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, data)
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type LastFMResponse struct {
|
||||
RecentTracks struct {
|
||||
Track []struct {
|
||||
Name string `json:"name"`
|
||||
Artist struct {
|
||||
Name string `json:"#text"`
|
||||
} `json:"artist"`
|
||||
Album struct {
|
||||
Name string `json:"#text"`
|
||||
} `json:"album"`
|
||||
Date struct {
|
||||
Unix string `json:"#text"`
|
||||
} `json:"date"`
|
||||
} `json:"track"`
|
||||
} `json:"recenttracks"`
|
||||
}
|
||||
|
||||
func GetRecentTracks(username, apiKey string) (*LastFMResponse, error) {
|
||||
url := fmt.Sprintf("https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&api_key=%s&format=json", username, apiKey)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lastfm get resp err: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lastfm read resp body err: %v", err)
|
||||
}
|
||||
var lastFMResponse LastFMResponse
|
||||
if err := json.Unmarshal(body, &lastFMResponse); err != nil {
|
||||
return nil, fmt.Errorf("lastfm unmarshal err: %v", err)
|
||||
}
|
||||
|
||||
return &lastFMResponse, nil
|
||||
}
|
|
@ -3,6 +3,13 @@
|
|||
<div>
|
||||
<img src="/assets/pic/miffy.png?v={{ .version }}" width="100%" height="100%">
|
||||
</div>
|
||||
<div class="lastfm-container">
|
||||
<p class="lastfm-text">
|
||||
<code>🎵 last played track: </code>
|
||||
<code id="lastfm">waiting for a response from lastfm...</code>
|
||||
<code> 🎵</code>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<h1>
|
||||
contacts
|
||||
|
@ -22,5 +29,22 @@
|
|||
and also you can subscribe to my <a href="https://t.me/lolistack" target="_blank">telegram channel</a> with pictures!
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function updateLastfm() {
|
||||
fetch('/api/lastfm')
|
||||
.then(response => response.text())
|
||||
.then(text => {
|
||||
document.getElementById('lastfm').textContent = text;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching lastfm:', error);
|
||||
});
|
||||
}
|
||||
|
||||
updateLastfm();
|
||||
|
||||
setInterval(updateLastfm, 10000);
|
||||
</script>
|
||||
</header>
|
||||
{{ end }}
|
Loading…
Reference in New Issue