From b282732c29f2563afb012f799339f03664a5842c Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Mon, 25 Oct 2021 09:44:12 +0200 Subject: [PATCH] Marshal json response directly in Video instance --- backend/youtube/youtube-dl/#youtube-dl.go# | 144 ------------------ backend/youtube/youtube-dl/.#youtube-dl.go | 1 - backend/youtube/youtube-dl/youtube-dl.go | 75 +-------- backend/youtube/youtube-dl/youtube-dl_test.go | 3 +- 4 files changed, 5 insertions(+), 218 deletions(-) delete mode 100644 backend/youtube/youtube-dl/#youtube-dl.go# delete mode 120000 backend/youtube/youtube-dl/.#youtube-dl.go diff --git a/backend/youtube/youtube-dl/#youtube-dl.go# b/backend/youtube/youtube-dl/#youtube-dl.go# deleted file mode 100644 index 7c1ecab..0000000 --- a/backend/youtube/youtube-dl/#youtube-dl.go# +++ /dev/null @@ -1,144 +0,0 @@ -package youtube_dl - -import ( - "context" - "encoding/json" - "fmt" - "log" - "os/exec" - "path/filepath" - "strconv" - "strings" - "time" - - "git.andreafazzi.eu/andrea/youtube-dl-service/task" - "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" - lib "github.com/kkdai/youtube/v2" -) - -type YoutubeDlDownloader struct { - Url string - CommandName string -} - -func NewYoutubeDlDownloader(url string, commandName string) *YoutubeDlDownloader { - return &YoutubeDlDownloader{url, commandName} -} - -func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) { - videoJson, err := d.getVideoJson(d.Url) - - thumbnails := make([]youtube.Thumbnail, 0) - thumbnailsJson := videoJson["thumbnails"].([]interface{}) - - for _, i := range thumbnailsJson { - t := i.(map[string]interface{}) - var err error - wString, wOk := t["width"].(string) - hString, hOk := t["height"].(string) - w := 0 - h := 0 - if wString != "" && hString != "" { - w, err = strconv.Atoi(wString) - if err != nil { - return nil, err - } - h, err = strconv.Atoi(hString) - if err != nil { - return nil, err - } - } - thumbnails = append(thumbnails, youtube.Thumbnail{ - URL: t["url"].(string), - Width: uint(w), - Height: uint(h), - }) - } - - return &youtube.Video{ - ID: videoJson["id"].(string), - Title: videoJson["title"].(string), - Duration: time.Duration(videoJson["duration"].(int64) * time.Hour.Nanoseconds()), - Thumbnails: thumbnails, - }, err -} - -func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) { - tasks[video.ID] = &task.Task{ - Status: task.StatusDownloading, - } - - log.Printf("Download of video ID %s STARTED.", video.ID) - cmd := exec.CommandContext(context.Background(), d.CommandName, "--newline", d.Url) - stdout, err := cmd.StdoutPipe() - if err != nil { - panic(err) - } - if err = cmd.Start(); err != nil { - panic(err) - } - for { - tmp := make([]byte, 1024) - _, err := stdout.Read(tmp) - fmt.Print(string(tmp)) - if err != nil { - break - } - } - - log.Printf("Download of video ID %s COMPLETED.", video.ID) - tasks[video.ID] = &task.Task{ - Status: task.StatusCompleted, - DownloadPath: filepath.Join("data", video.ID+".mp4"), - } -} - -func (d *YoutubeDlDownloader) ExtractVideoID() (string, error) { - return lib.ExtractVideoID(d.Url) -} - -func (d *YoutubeDlDownloader) getVideoJson(url string) (map[string]interface{}, error) { - cmd := exec.CommandContext(context.Background(), d.CommandName, "-j", url) - output, err := cmd.Output() - if err != nil { - return nil, err - } - result := make(map[string]interface{}, 0) - err = json.Unmarshal(output, &result) - if err != nil { - return nil, err - } - return result, nil -} - -func (d *YoutubeDlDownloader) getThumbnails() ([]youtube.Thumbnail, error) { - thumbnails := make([]youtube.Thumbnail, 0) - cmd := exec.CommandContext(context.Background(), d.CommandName, "--list-thumbnails", d.Url) - output, err := cmd.Output() - if err != nil { - return nil, err - } - lines := strings.Split(string(output), "\n")[3:] - for _, line := range lines { - splits := strings.Split(line, " ") - if len(splits) == 4 { - wString := strings.TrimSpace(splits[1]) - hString := strings.TrimSpace(splits[2]) - w, err := strconv.Atoi(wString) - if err != nil { - return nil, err - } - h, err := strconv.Atoi(hString) - if err != nil { - return nil, err - } - - thumbnails = append(thumbnails, youtube.Thumbnail{ - URL: strings.TrimSpace(splits[3]), - Width: uint(w), - Height: uint(h), - }) - } - } - return thumbnails, nil -} diff --git a/backend/youtube/youtube-dl/.#youtube-dl.go b/backend/youtube/youtube-dl/.#youtube-dl.go deleted file mode 120000 index 9b02a6a..0000000 --- a/backend/youtube/youtube-dl/.#youtube-dl.go +++ /dev/null @@ -1 +0,0 @@ -andrea@lv5.88143:1634702979 \ No newline at end of file diff --git a/backend/youtube/youtube-dl/youtube-dl.go b/backend/youtube/youtube-dl/youtube-dl.go index e7af4aa..5aef90d 100644 --- a/backend/youtube/youtube-dl/youtube-dl.go +++ b/backend/youtube/youtube-dl/youtube-dl.go @@ -7,9 +7,6 @@ import ( "log" "os/exec" "path/filepath" - "strconv" - "strings" - "time" "git.andreafazzi.eu/andrea/youtube-dl-service/task" "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" @@ -26,41 +23,7 @@ func NewYoutubeDlDownloader(url string, commandName string) *YoutubeDlDownloader } func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) { - videoJson, err := d.getVideoJson(d.Url) - - thumbnails := make([]youtube.Thumbnail, 0) - thumbnailsJson := videoJson["thumbnails"].([]interface{}) - - for _, i := range thumbnailsJson { - t := i.(map[string]interface{}) - var err error - wString := t["width"].(string) - hString := t["height"].(string) - w := 0 - h := 0 - if wString != "" && hString != "" { - w, err = strconv.Atoi(wString) - if err != nil { - return nil, err - } - h, err = strconv.Atoi(hString) - if err != nil { - return nil, err - } - } - thumbnails = append(thumbnails, youtube.Thumbnail{ - URL: t["url"].(string), - Width: uint(w), - Height: uint(h), - }) - } - - return &youtube.Video{ - ID: videoJson["id"].(string), - Title: videoJson["title"].(string), - Duration: time.Duration(videoJson["duration"].(int64) * time.Hour.Nanoseconds()), - Thumbnails: thumbnails, - }, err + return d.getVideoJson(d.Url) } func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) { @@ -97,48 +60,16 @@ func (d *YoutubeDlDownloader) ExtractVideoID() (string, error) { return lib.ExtractVideoID(d.Url) } -func (d *YoutubeDlDownloader) getVideoJson(url string) (map[string]interface{}, error) { +func (d *YoutubeDlDownloader) getVideoJson(url string) (*youtube.Video, error) { cmd := exec.CommandContext(context.Background(), d.CommandName, "-j", url) output, err := cmd.Output() if err != nil { return nil, err } - result := make(map[string]interface{}, 0) + result := new(youtube.Video) err = json.Unmarshal(output, &result) if err != nil { return nil, err } return result, nil } - -func (d *YoutubeDlDownloader) getThumbnails() ([]youtube.Thumbnail, error) { - thumbnails := make([]youtube.Thumbnail, 0) - cmd := exec.CommandContext(context.Background(), d.CommandName, "--list-thumbnails", d.Url) - output, err := cmd.Output() - if err != nil { - return nil, err - } - lines := strings.Split(string(output), "\n")[3:] - for _, line := range lines { - splits := strings.Split(line, " ") - if len(splits) == 4 { - wString := strings.TrimSpace(splits[1]) - hString := strings.TrimSpace(splits[2]) - w, err := strconv.Atoi(wString) - if err != nil { - return nil, err - } - h, err := strconv.Atoi(hString) - if err != nil { - return nil, err - } - - thumbnails = append(thumbnails, youtube.Thumbnail{ - URL: strings.TrimSpace(splits[3]), - Width: uint(w), - Height: uint(h), - }) - } - } - return thumbnails, nil -} diff --git a/backend/youtube/youtube-dl/youtube-dl_test.go b/backend/youtube/youtube-dl/youtube-dl_test.go index 090c027..cc75a0c 100644 --- a/backend/youtube/youtube-dl/youtube-dl_test.go +++ b/backend/youtube/youtube-dl/youtube-dl_test.go @@ -21,9 +21,10 @@ func TestRunner(t *testing.T) { // End of setup // Tests start here -func (t *testSuite) TestGetThumbnails() { +func (t *testSuite) TestGetVideoInfo() { d := NewYoutubeDlDownloader("https://www.youtube.com/watch?v=mWZ6b_I-Djg", "yt-dlp") video, err := d.GetVideo() t.Nil(err) + t.True(video.Title != "") t.True(video.Thumbnails[0].URL != "") }