aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2022-12-27 15:59:15 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2022-12-27 15:59:15 +0100
commit2dd235cc125b9f5c57a22729fdaef3187cdcec06 (patch)
treef7c6b8ef4be50166dfe51c348273aee07823e2df
parent5004fee6ce1eb8fdde6acbe8b5b3d9e61e74859d (diff)
downloadWRMS-2dd235cc125b9f5c57a22729fdaef3187cdcec06.tar.gz
WRMS-2dd235cc125b9f5c57a22729fdaef3187cdcec06.zip
implement youtube search using youtube-dlv0.0.4-alpha
-rw-r--r--TODO2
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--player.go2
-rw-r--r--youtube.go44
5 files changed, 33 insertions, 21 deletions
diff --git a/TODO b/TODO
index 4bd1d60..f9fc0b9 100644
--- a/TODO
+++ b/TODO
@@ -7,7 +7,7 @@
## Backends
- [X] Implement local storage backend
-- [ ] Fix youtube search
+- [X] Fix youtube search
## Configuration
- [ ] Implement command line flags
diff --git a/go.mod b/go.mod
index c2d9525..a4c1cf7 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,6 @@ module muhq.space/go/wrms
go 1.18
require (
- github.com/AnjanaMadu/YTSearch v1.0.0
github.com/dhowden/tag v0.0.0-20220618230019-adf36e896086
github.com/google/uuid v1.3.0
github.com/librespot-org/librespot-golang v0.0.0-20220325184705-31669e5a889f
@@ -12,7 +11,6 @@ require (
require (
github.com/badfortrains/mdns v0.0.0-20160325001438-447166384f51 // indirect
- github.com/buger/jsonparser v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/klauspost/compress v1.15.13 // indirect
github.com/miekg/dns v1.1.50 // indirect
diff --git a/go.sum b/go.sum
index 8a7deb3..2d42bac 100644
--- a/go.sum
+++ b/go.sum
@@ -1,9 +1,5 @@
-github.com/AnjanaMadu/YTSearch v1.0.0 h1:88ovSXJDVSr/bNQY90CYdpAIDSYtlW2Ik6D8VHRdfFo=
-github.com/AnjanaMadu/YTSearch v1.0.0/go.mod h1:ReflwduZK14df7oUOSuNDqLwox2D2heQknhYbEWQlD8=
github.com/badfortrains/mdns v0.0.0-20160325001438-447166384f51 h1:b6+GdpGhuzxPETrVLwY77iq0L/BqLpcPRmaLNW+SoRY=
github.com/badfortrains/mdns v0.0.0-20160325001438-447166384f51/go.mod h1:qHRkxMBkkpAWD2poeBYQt6O5IS4dE6w1Cr4Z8Q3feXI=
-github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
-github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
diff --git a/player.go b/player.go
index 94efa27..35eee9f 100644
--- a/player.go
+++ b/player.go
@@ -25,7 +25,7 @@ func NewPlayer(wrms *Wrms, backends []string) Player {
case "spotify":
b, err = NewSpotify()
case "youtube":
- b = &YoutubeBackend{}
+ b = NewYoutubeBackend()
case "dummy":
b = &DummyBackend{}
case "local":
diff --git a/youtube.go b/youtube.go
index ad715ce..8997bc9 100644
--- a/youtube.go
+++ b/youtube.go
@@ -1,34 +1,52 @@
package main
import (
+ "encoding/json"
"fmt"
- "github.com/AnjanaMadu/YTSearch"
"muhq.space/go/wrms/llog"
+ "os/exec"
+ "strings"
)
-type YoutubeBackend struct{}
+type YoutubeBackend struct {
+ searchResults int
+}
+
+func NewYoutubeBackend() *YoutubeBackend {
+ return &YoutubeBackend{10}
+}
-func (youtube *YoutubeBackend) Play(song *Song, player *Player) {
+func (_ *YoutubeBackend) Play(song *Song, player *Player) {
player.PlayUri("https://youtube.com/watch?v=" + song.Uri)
}
-func (youtube *YoutubeBackend) Search(pattern string) []Song {
- results, err := ytsearch.Search(pattern)
+type YoutubeDlSearchResult struct {
+ Id string
+ Title string
+}
+
+func (b *YoutubeBackend) Search(pattern string) []Song {
+ searchOption := fmt.Sprintf("ytsearch%d:%s", b.searchResults, pattern)
+ llog.Debug(fmt.Sprintf("Search youtube using: youtube-dl -j %s", searchOption))
+ results, err := exec.Command("youtube-dl", "-j", searchOption).Output()
+
if err != nil {
- llog.Error(fmt.Sprintf("Youtube search failed with", err))
+ llog.Error(fmt.Sprintf("youtube-dl failed with: %s", err))
}
songs := []Song{}
- for i, result := range results {
- if i == 10 {
- break
- }
-
- if result.VideoId == "" {
+ for _, l := range strings.Split(string(results), "\n") {
+ if l == "" {
continue
}
- songs = append(songs, NewSong(result.Title, "", "youtube", result.VideoId))
+ result := YoutubeDlSearchResult{}
+ err = json.Unmarshal([]byte(l), &result)
+ if err != nil {
+ llog.Debug(fmt.Sprintf("Parsing youtube-dl results '%s' failed", l))
+ llog.Error(fmt.Sprintf("Parsing youtube-dl results failed with: %s", err))
+ }
+ songs = append(songs, NewSong(result.Title, "", "youtube", result.Id))
}
llog.Debug(fmt.Sprintf("youtube found %d matching videos", len(songs)))