diff --git a/backend/youtube/downloader.go b/backend/downloader/downloader.go similarity index 96% rename from backend/youtube/downloader.go rename to backend/downloader/downloader.go index 1e32e33..791e2d1 100644 --- a/backend/youtube/downloader.go +++ b/backend/downloader/downloader.go @@ -1,4 +1,4 @@ -package youtube +package downloader import ( "time" 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 79% rename from backend/youtube/youtube-dl/youtube-dl.go rename to backend/downloader/youtube-dl/youtube-dl.go index ef093bc..633d638 100644 --- a/backend/youtube/youtube-dl/youtube-dl.go +++ b/backend/downloader/youtube-dl/youtube-dl.go @@ -7,9 +7,9 @@ import ( "os/exec" "path/filepath" + "git.andreafazzi.eu/andrea/youtube-dl-service/downloader" "git.andreafazzi.eu/andrea/youtube-dl-service/logger" "git.andreafazzi.eu/andrea/youtube-dl-service/task" - "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" lib "github.com/kkdai/youtube/v2" ) @@ -22,17 +22,17 @@ 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, } - logger.Info("Download of video ID %s STARTED.", video.ID) + logger.Info(fmt.Sprintf("Download of video ID %s STARTED.", video.ID)) cmd := exec.CommandContext(context.Background(), d.CommandName, "-o", filepath.Join("data/", video.ID+filepath.Ext(video.Filename)), "--newline", d.Url) stdout, err := cmd.StdoutPipe() if err != nil { @@ -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/logger/logger.go b/backend/logger/logger.go index f41c8ef..e3860ca 100644 --- a/backend/logger/logger.go +++ b/backend/logger/logger.go @@ -3,19 +3,22 @@ package logger import "log" const ( - Disabled = 0 + Disabled = iota InfoLevel WarningLevel DebugLevel ) -var loggerLevel int +var loggerLevel int = InfoLevel func SetLevel(level int) { loggerLevel = level } -func Info(format string, v ...interface{}) { +func Info(v ...interface{}) { + if loggerLevel == Disabled { + return + } if loggerLevel == InfoLevel { log.Println(v...) } diff --git a/backend/main.go b/backend/main.go index 1f07889..9b30d84 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 4639b7c..596da61 100644 --- a/backend/main_test.go +++ b/backend/main_test.go @@ -8,9 +8,9 @@ import ( "strings" "testing" + "git.andreafazzi.eu/andrea/youtube-dl-service/downloader" "git.andreafazzi.eu/andrea/youtube-dl-service/logger" "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" ) @@ -21,16 +21,17 @@ type testSuite struct { } func TestRunner(t *testing.T) { - - gin.SetMode(gin.ReleaseMode) - logger.SetLevel(logger.Disabled) - prettytest.Run( t, new(testSuite), ) } +func (t *testSuite) BeforeAll() { + gin.SetMode(gin.ReleaseMode) + logger.SetLevel(logger.Disabled) +} + // Test the creation of a new task. A new task is created with a POST // request to the endpoint. func (t *testSuite) TestCreateTask() { @@ -73,7 +74,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) @@ -96,7 +97,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