From d11bf17d4c294525d071e461790e3f2413f4c62f Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 7 Dec 2021 13:49:08 +0100 Subject: [PATCH] Remove unused library and refactoring --- backend/downloader/downloader.go | 30 +++++++++++++++++++ .../kkdai_youtube/kkdai_youtube.go | 0 .../youtube-dl/youtube-dl.go | 10 +++---- .../youtube-dl/youtube-dl_test.go | 0 backend/main.go | 6 ++-- backend/main_test.go | 6 ++-- backend/youtube/downloader.go | 2 +- 7 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 backend/downloader/downloader.go rename backend/{youtube => downloader}/kkdai_youtube/kkdai_youtube.go (100%) rename backend/{youtube => downloader}/youtube-dl/youtube-dl.go (82%) rename backend/{youtube => downloader}/youtube-dl/youtube-dl_test.go (100%) diff --git a/backend/downloader/downloader.go b/backend/downloader/downloader.go new file mode 100644 index 0000000..791e2d1 --- /dev/null +++ b/backend/downloader/downloader.go @@ -0,0 +1,30 @@ +package downloader + +import ( + "time" + + "git.andreafazzi.eu/andrea/youtube-dl-service/task" +) + +type Thumbnails []Thumbnail + +type Thumbnail struct { + URL string + Width uint + Height uint +} + +type Video struct { + ID string + Title string + Duration time.Duration + Thumbnails Thumbnails + Filename string + DownloadPath string +} + +type Downloader interface { + GetVideo() (*Video, error) + StartDownload(video *Video, tasks task.Tasks) + ExtractVideoID() (string, error) +} diff --git a/backend/youtube/kkdai_youtube/kkdai_youtube.go b/backend/downloader/kkdai_youtube/kkdai_youtube.go similarity index 100% rename from backend/youtube/kkdai_youtube/kkdai_youtube.go rename to backend/downloader/kkdai_youtube/kkdai_youtube.go diff --git a/backend/youtube/youtube-dl/youtube-dl.go b/backend/downloader/youtube-dl/youtube-dl.go similarity index 82% rename from backend/youtube/youtube-dl/youtube-dl.go rename to backend/downloader/youtube-dl/youtube-dl.go index c18bb2a..ae67c7e 100644 --- a/backend/youtube/youtube-dl/youtube-dl.go +++ b/backend/downloader/youtube-dl/youtube-dl.go @@ -8,8 +8,8 @@ import ( "os/exec" "path/filepath" + "git.andreafazzi.eu/andrea/youtube-dl-service/downloader" "git.andreafazzi.eu/andrea/youtube-dl-service/task" - "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" lib "github.com/kkdai/youtube/v2" ) @@ -22,11 +22,11 @@ func NewYoutubeDlDownloader(url string, commandName string) *YoutubeDlDownloader return &YoutubeDlDownloader{url, commandName} } -func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) { +func (d *YoutubeDlDownloader) GetVideo() (*downloader.Video, error) { return d.getVideoJson(d.Url) } -func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) { +func (d *YoutubeDlDownloader) StartDownload(video *downloader.Video, tasks task.Tasks) { tasks[video.ID] = &task.Task{ ID: video.ID, Status: task.StatusDownloading, @@ -62,13 +62,13 @@ func (d *YoutubeDlDownloader) ExtractVideoID() (string, error) { return lib.ExtractVideoID(d.Url) } -func (d *YoutubeDlDownloader) getVideoJson(url string) (*youtube.Video, error) { +func (d *YoutubeDlDownloader) getVideoJson(url string) (*downloader.Video, error) { cmd := exec.CommandContext(context.Background(), d.CommandName, "-j", url) output, err := cmd.CombinedOutput() if err != nil { return nil, fmt.Errorf("Command terminated with %w: %s", err, output) } - result := new(youtube.Video) + result := new(downloader.Video) err = json.Unmarshal(output, &result) if err != nil { return nil, err diff --git a/backend/youtube/youtube-dl/youtube-dl_test.go b/backend/downloader/youtube-dl/youtube-dl_test.go similarity index 100% rename from backend/youtube/youtube-dl/youtube-dl_test.go rename to backend/downloader/youtube-dl/youtube-dl_test.go diff --git a/backend/main.go b/backend/main.go index 9477cea..2821035 100644 --- a/backend/main.go +++ b/backend/main.go @@ -7,9 +7,9 @@ import ( "path/filepath" "strconv" + "git.andreafazzi.eu/andrea/youtube-dl-service/downloader" + youtube_dl "git.andreafazzi.eu/andrea/youtube-dl-service/downloader/youtube-dl" "git.andreafazzi.eu/andrea/youtube-dl-service/task" - "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" - youtube_dl "git.andreafazzi.eu/andrea/youtube-dl-service/youtube/youtube-dl" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) @@ -21,7 +21,7 @@ func init() { tasks = make(task.Tasks, 0) } -func download(c *gin.Context, downloader youtube.Downloader) error { +func download(c *gin.Context, downloader downloader.Downloader) error { video, err := downloader.GetVideo() if err != nil { return err diff --git a/backend/main_test.go b/backend/main_test.go index 5b762e0..99a0817 100644 --- a/backend/main_test.go +++ b/backend/main_test.go @@ -8,8 +8,8 @@ import ( "strings" "testing" + "git.andreafazzi.eu/andrea/youtube-dl-service/downloader" "git.andreafazzi.eu/andrea/youtube-dl-service/task" - "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" "github.com/gin-gonic/gin" "github.com/remogatto/prettytest" ) @@ -68,7 +68,7 @@ func (t *testSuite) TestGetTask() { t.Equal("AVIBLFl28vo", task.ID) } -func postTask(ytUrl string) (*youtube.Video, error) { +func postTask(ytUrl string) (*downloader.Video, error) { // Set form values form := url.Values{} form.Add("url", ytUrl) @@ -91,7 +91,7 @@ func postTask(ytUrl string) (*youtube.Video, error) { createTask(c) // Read the response and assert it. - video := new(youtube.Video) + video := new(downloader.Video) if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil { return nil, err diff --git a/backend/youtube/downloader.go b/backend/youtube/downloader.go index 1e32e33..791e2d1 100644 --- a/backend/youtube/downloader.go +++ b/backend/youtube/downloader.go @@ -1,4 +1,4 @@ -package youtube +package downloader import ( "time"