-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.go
More file actions
198 lines (157 loc) · 4.66 KB
/
playlist.go
File metadata and controls
198 lines (157 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package service
import (
"context"
"encoding/json"
"fmt"
"musicboxapi/database"
"musicboxapi/logging"
"musicboxapi/models"
"os"
"strconv"
"strings"
"time"
)
func downloadPlaylist(
parentTask models.ParentTaskLog,
storage string,
archiveFileName string,
idsFileName string,
namesFileName string,
durationFileName string,
playlistTitleFileName string,
playlistIdFileName string,
imagesFolder string,
fileExtension string,
logfilesOutputPath string,
logfilesOutputPathError string,
storageFolderName string,
) {
/*
Ignore
[Deleted video]
[Deleted video]
[Private video]
*/
ids, _ := readLines(idsFileName)
downloadIds := ids
names, _ := readLines(namesFileName)
durations, _ := readLines(durationFileName)
downloadCount := len(downloadIds)
playlistNames, _ := readLines(playlistTitleFileName)
tasklogTable := database.NewTasklogTableInstance()
playlistTable := database.NewPlaylistTableInstance()
playlistsongTable := database.NewPlaylistsongTableInstance()
songTable := database.NewSongTableInstance()
// Check if exists, if not then create
existingPlaylists, _ := playlistTable.FetchPlaylists(context.Background(), 0)
playlistExists := false
playlistId := -1
for _, playlist := range existingPlaylists {
if playlist.Name == playlistNames[0] {
playlistExists = true
playlistId = playlist.Id
break
}
}
_playlistId, _ := readLines(playlistIdFileName)
if !playlistExists {
_newPlaylistId, err := playlistTable.InsertPlaylist(models.Playlist{
Name: playlistNames[0],
Description: "Custom playlist",
ThumbnailPath: fmt.Sprintf("%s.jpg", _playlistId[0]),
CreationDate: time.Now(),
IsPublic: true,
UpdatedAt: time.Now(),
})
if err != nil {
errorChildLog, err := tasklogTable.CreateChildTaskLog(parentTask)
json, err := json.Marshal(err)
errorChildLog.OutputLog = json
tasklogTable.ChildTaskLogError(errorChildLog)
logging.Error(fmt.Sprintf("[Creating custom playlist error]: %s", err.Error()))
return
}
playlistId = _newPlaylistId
}
// Special case, thumbnail is written to root directory
if len(playlistNames) > 0 && len(_playlistId) > 0 {
oldpath := fmt.Sprintf("%s [%s].jpg", playlistNames[0], _playlistId[0])
newpath := fmt.Sprintf("%s/%s.jpg", imagesFolder, _playlistId[0])
_ = os.Rename(oldpath, newpath)
}
// defaultSettings := ytdlp.New().
// ExtractAudio().
// AudioQuality("0").
// AudioFormat(fileExtension).
// DownloadArchive(archiveFileName).
// WriteThumbnail().
// ConcurrentFragments(10).
// ConvertThumbnails("jpg").
// ForceIPv4().
// //sudo apt install aria2
// Downloader("aria2c").
// DownloaderArgs("aria2c:-x 16 -s 16 -j 16").
// NoKeepVideo().
// Output(storage + "/%(id)s.%(ext)s").
// Cookies("selenium/cookies_netscape")
for id := range downloadCount {
name := names[id]
if canDownload(name) {
childTask, _ := tasklogTable.CreateChildTaskLog(parentTask)
childTask.Status = int(models.Downloading)
tasklogTable.UpdateChildTaskLogStatus(childTask)
downloaded := FlatSingleDownload(
archiveFileName,
idsFileName,
namesFileName,
durationFileName,
playlistTitleFileName,
playlistIdFileName,
fmt.Sprintf("https://www.youtube.com/watch?v=%s", ids[id]),
logfilesOutputPath,
logfilesOutputPathError,
storageFolderName,
fileExtension)
if !downloaded {
file, err := os.ReadFile(logfilesOutputPathError)
json, _ := json.Marshal(file)
childTask.OutputLog = json
tasklogTable.ChildTaskLogError(childTask)
logging.Error(fmt.Sprintf("Failed to download %s, error:%s", ids[id], err.Error()))
continue
}
childTask.Status = int(models.Updating)
tasklogTable.UpdateChildTaskLogStatus(childTask)
var song models.Song
song.Name = names[id]
song.SourceId = ids[id]
song.Duration, _ = strconv.Atoi(durations[id])
song.Path = fmt.Sprintf("%s/%s.%s", storage, ids[id], fileExtension)
song.ThumbnailPath = fmt.Sprintf("%s.jpg", ids[id])
songTable.InsertSong(&song)
playlistsongTable.InsertPlaylistSong(playlistId, song.Id)
oldpath := fmt.Sprintf("%s/%s", storage, song.ThumbnailPath)
newpath := fmt.Sprintf("%s/%s", imagesFolder, song.ThumbnailPath)
_ = os.Rename(oldpath, newpath)
file, err := os.ReadFile(logfilesOutputPath)
if err != nil {
panic(-1564654654)
}
json, _ := json.Marshal(file)
childTask.OutputLog = json
tasklogTable.ChildTaskLogDone(childTask)
}
}
}
func canDownload(name string) bool {
if strings.HasPrefix(name, "[Deleted video]") {
return false
}
if strings.HasPrefix(name, "[Private video]") {
return false
}
if strings.HasPrefix(name, " ") {
return false
}
return true
}