@@ -2,7 +2,6 @@ package service
22
33import (
44 "bufio"
5- "context"
65 "encoding/json"
76 "fmt"
87 "io/fs"
@@ -13,8 +12,6 @@ import (
1312 "os"
1413 "strconv"
1514 "strings"
16-
17- "github.com/lrstanley/go-ytdlp"
1815)
1916
2017func StartDownloadTask (downloadRequest models.DownloadRequestModel ) {
@@ -34,6 +31,8 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
3431 playlistIdFileName := fmt .Sprintf ("%s/playlist_id.%d" , storageFolderName , parentTask .Id )
3532 imagesFolder := fmt .Sprintf ("%s/images" , storageFolderName )
3633 fileExtension := config .OutputExtension
34+ logfilesOutputPath := fmt .Sprintf ("%s/hotfix_logs/%s" , storageFolderName , fmt .Sprintf ("logrun_%d" , parentTask .Id ))
35+ logfilesOutputPathError := fmt .Sprintf ("%s/hotfix_logs/%s" , storageFolderName , fmt .Sprintf ("logrunError_%d" , parentTask .Id ))
3736
3837 if ! pathExists (storageFolderName ) {
3938 err := os .Mkdir (storageFolderName , fs .ModePerm | fs .ModeDir )
@@ -59,30 +58,31 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
5958 durationFileName ,
6059 playlistTitleFileName ,
6160 playlistIdFileName ,
61+ logfilesOutputPath ,
62+ logfilesOutputPathError ,
6263 }
6364
6465 if isPlaylist {
65- dlp := ytdlp .New ().
66- DownloadArchive (archiveFileName ).
67- ForceIPv4 ().
68- NoKeepVideo ().
69- SkipDownload ().
70- FlatPlaylist ().
71- WriteThumbnail ().
72- PrintToFile ("%(id)s" , idsFileName ).
73- PrintToFile ("%(title)s" , namesFileName ).
74- PrintToFile ("%(duration)s" , durationFileName ).
75- PrintToFile ("%(playlist_title)s" , playlistTitleFileName ).
76- PrintToFile ("%(playlist_id)s" , playlistIdFileName ).
77- Cookies ("selenium/cookies_netscape" ).
78- IgnoreErrors ()
66+ downloaded := FlatPlaylistDownload (
67+ archiveFileName ,
68+ idsFileName ,
69+ namesFileName ,
70+ durationFileName ,
71+ playlistTitleFileName ,
72+ playlistIdFileName ,
73+ downloadRequest .Url ,
74+ logfilesOutputPath ,
75+ logfilesOutputPathError )
7976
8077 // Start download (flat download)
81- result , err := dlp .Run (context .Background (), downloadRequest .Url )
78+ if ! downloaded {
79+
80+ fmt .Printf ("Failed to download for %s check logs at %s" , downloadRequest .Url , logfilesOutputPathError )
81+
82+ file , err := os .ReadFile (logfilesOutputPathError )
8283
83- if err != nil {
8484 // Set Task state -> Error
85- json , err := json .Marshal (result . OutputLogs )
85+ json , err := json .Marshal (string ( file ) )
8686
8787 errChildTask , err := tasklogTable .CreateChildTaskLog (parentTask )
8888
@@ -96,6 +96,19 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
9696
9797 //Delete created files if any
9898 for _ , path := range cleanupFile {
99+
100+ if strings .Contains (path , "logrunError" ) {
101+ lines , err := readLines (path )
102+
103+ if err != nil {
104+ panic (- 654654654 )
105+ }
106+
107+ if len (lines ) > 0 && len (lines [0 ]) > 0 {
108+ // skip deleting log so it can be used for debug
109+ continue
110+ }
111+ }
99112 os .Remove (path )
100113 }
101114 return
@@ -133,10 +146,27 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
133146 playlistTitleFileName ,
134147 playlistIdFileName ,
135148 imagesFolder ,
136- fileExtension )
149+ fileExtension ,
150+ logfilesOutputPath ,
151+ logfilesOutputPathError ,
152+ storageFolderName )
137153
138154 // Delete created files
139155 for _ , path := range cleanupFile {
156+
157+ if strings .Contains (path , "logrunError" ) {
158+ lines , err := readLines (path )
159+
160+ if err != nil {
161+ panic (- 654654654 )
162+ }
163+
164+ if len (lines ) > 0 && len (lines [0 ]) > 0 {
165+ // skip deleting log so it can be used for debug
166+ continue
167+ }
168+ }
169+
140170 os .Remove (path )
141171 }
142172 } else {
@@ -148,26 +178,19 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
148178 return
149179 }
150180
151- dlp := ytdlp .New ().
152- ExtractAudio ().
153- AudioQuality ("0" ).
154- AudioFormat (fileExtension ).
155- DownloadArchive (archiveFileName ).
156- WriteThumbnail ().
157- ConcurrentFragments (10 ).
158- ConvertThumbnails ("jpg" ).
159- ForceIPv4 ().
160- PrintToFile ("%(id)s" , idsFileName ).
161- PrintToFile ("%(title)s" , namesFileName ).
162- PrintToFile ("%(duration)s" , durationFileName ).
163- //sudo apt install aria2
164- Downloader ("aria2c" ).
165- DownloaderArgs ("aria2c:-x 16 -s 16 -j 16" ).
166- NoKeepVideo ().
167- Output (storageFolderName + "/%(id)s.%(ext)s" ).
168- Cookies ("selenium/cookies_netscape" )
181+ downloaded := FlatSingleDownload (
182+ archiveFileName ,
183+ idsFileName ,
184+ namesFileName ,
185+ durationFileName ,
186+ playlistTitleFileName ,
187+ playlistIdFileName ,
188+ downloadRequest .Url ,
189+ logfilesOutputPath ,
190+ logfilesOutputPathError ,
191+ storageFolderName ,
192+ fileExtension )
169193
170- // Update task status
171194 childTask .Status = int (models .Downloading )
172195
173196 err = tasklogTable .UpdateChildTaskLogStatus (childTask )
@@ -176,12 +199,13 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
176199 return
177200 }
178201
179- // Start download
180- result , err := dlp .Run (context .Background (), downloadRequest .Url )
202+ if ! downloaded {
203+ fmt .Printf ("Failed to download for %s check logs at %s" , downloadRequest .Url , logfilesOutputPathError )
204+
205+ file , err := os .ReadFile (logfilesOutputPathError )
181206
182- if err != nil {
183207 // Set Task state -> Error
184- json , err := json .Marshal (result . OutputLogs )
208+ json , err := json .Marshal (string ( file ) )
185209
186210 childTask .OutputLog = json
187211
@@ -251,7 +275,9 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
251275 logging .Error (fmt .Sprintf ("Failed to move song image to / images folder: %s" , err .Error ()))
252276 }
253277
254- json , err := json .Marshal (result .OutputLogs )
278+ file , err := os .ReadFile (logfilesOutputPath )
279+
280+ json , err := json .Marshal (string (file ))
255281
256282 childTask .OutputLog = json
257283 childTask .Status = int (models .Done )
@@ -263,6 +289,19 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
263289
264290 //Delete created files
265291 for _ , path := range cleanupFile {
292+
293+ if strings .Contains (path , "logrunError" ) {
294+ lines , err := readLines (path )
295+
296+ if err != nil {
297+ panic (- 654654654 )
298+ }
299+
300+ if len (lines ) > 0 && len (lines [0 ]) > 0 {
301+ // skip deleting log so it can be used for debug
302+ continue
303+ }
304+ }
266305 os .Remove (path )
267306 }
268307 }
0 commit comments