Skip to content

Commit

Permalink
Edit close file and add metadata music for status
Browse files Browse the repository at this point in the history
  • Loading branch information
debug-ing committed Jan 11, 2025
1 parent fc2e66e commit 21a29d4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func main() {
r.GET("/radio", client.HandleClientGin)
r.GET("/status", func(c *gin.Context) {
c.JSON(200, gin.H{
"name_music": internal.CurrentMusic,
"music": internal.CurrentMusic,
"info": internal.Info,
})
})
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/debug-ing/radio-music
go 1.22.3

require (
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8
github.com/gin-gonic/gin v1.10.0
github.com/prometheus/client_golang v1.20.5
github.com/spf13/viper v1.19.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8 h1:OtSeLS5y0Uy01jaKK4mA/WVIYtpzVm63vLVAPzJXigg=
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8/go.mod h1:apkPC/CR3s48O2D7Y++n1XWEpgPNNCjXYga3PPbJe2E=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down
38 changes: 32 additions & 6 deletions internal/stream.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package internal

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"

"github.com/dhowden/tag"
)

type InfoMusic struct {
Title string
Artist string
Album string
Year int
Composer string
Genre string
Lyrics string
}

var (
CurrentMusic string
Info *InfoMusic
)

func getPlaylist(folderPath string) ([]string, error) {
Expand Down Expand Up @@ -39,29 +53,41 @@ func StartStream(folderPath string, client *Client) {

for {
for _, filePath := range playlist {
CurrentMusic = strings.Split(filePath, "/")[1]
CurrentMusic = strings.Split(filePath, "/")[len(strings.Split(filePath, "/"))-1]
file, err := os.Open(filePath)
if err != nil {
log.Println("Error opening file:", err)
continue
}

defer file.Close()
metadata, err := tag.ReadFrom(file)
if err != nil {
log.Println("Error reading metadata:", err)
return
}
fmt.Println(metadata.Title())
Info = &InfoMusic{
Title: metadata.Title(),
Artist: metadata.Artist(),
Album: metadata.Album(),
Year: metadata.Year(),
Composer: metadata.Composer(),
Genre: metadata.Genre(),
Lyrics: metadata.Lyrics(),
}
buffer := make([]byte, 2048)
for {
n, err := file.Read(buffer)
if err != nil {
if err.Error() == "EOF" {
break // به فایل بعدی بروید
break
}
log.Println("Error reading file:", err)
break
}
client.Broadcast(buffer[:n])
// broadcast(buffer[:n])
time.Sleep(50 * time.Millisecond)
}

file.Close()
}
}
}

0 comments on commit 21a29d4

Please sign in to comment.