diff options
Diffstat (limited to 'youtube.go')
| -rw-r--r-- | youtube.go | 44 |
1 files changed, 31 insertions, 13 deletions
@@ -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))) |
