|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "os"
|
6 | 5 | "strconv"
|
7 | 6 | "strings"
|
8 | 7 |
|
9 | 8 | "github.com/arimatakao/mdx/filekit"
|
10 |
| - "github.com/arimatakao/mdx/filekit/metadata" |
| 9 | + "github.com/arimatakao/mdx/internal/mdx" |
11 | 10 | "github.com/arimatakao/mdx/mangadexapi"
|
12 |
| - "github.com/pterm/pterm" |
13 | 11 | "github.com/spf13/cobra"
|
14 | 12 | )
|
15 | 13 |
|
16 | 14 | var (
|
17 | 15 | downloadCmd = &cobra.Command{
|
18 | 16 | Use: "download",
|
19 |
| - Aliases: []string{"dl", "save"}, |
| 17 | + Aliases: []string{"dl", "save", "sv"}, |
20 | 18 | Short: "Download manga by URL",
|
21 | 19 | PreRun: checkDownloadArgs,
|
22 | 20 | Run: downloadManga,
|
23 | 21 | }
|
24 |
| - imgExt string = "png" |
25 | 22 | isJpgFileFormat bool
|
26 | 23 | outputDir string
|
27 | 24 | language string
|
@@ -57,10 +54,6 @@ func init() {
|
57 | 54 | "merge", "m", false, "merge downloaded chapters into one file")
|
58 | 55 | downloadCmd.Flags().BoolVarP(&isLastChapter,
|
59 | 56 | "last", "", false, "download last chapter")
|
60 |
| - |
61 |
| - e = pterm.Error |
62 |
| - dp = pterm.NewStyle(pterm.FgDefault, pterm.BgDefault) |
63 |
| - field = pterm.NewStyle(pterm.FgGreen, pterm.BgDefault, pterm.Bold) |
64 | 57 | }
|
65 | 58 |
|
66 | 59 | func checkDownloadArgs(cmd *cobra.Command, args []string) {
|
@@ -91,10 +84,6 @@ func checkDownloadArgs(cmd *cobra.Command, args []string) {
|
91 | 84 | os.Exit(0)
|
92 | 85 | }
|
93 | 86 |
|
94 |
| - if isJpgFileFormat { |
95 |
| - imgExt = "jpg" |
96 |
| - } |
97 |
| - |
98 | 87 | if outputExt != filekit.CBZ_EXT &&
|
99 | 88 | outputExt != filekit.PDF_EXT &&
|
100 | 89 | outputExt != filekit.EPUB_EXT {
|
@@ -143,221 +132,14 @@ func checkDownloadArgs(cmd *cobra.Command, args []string) {
|
143 | 132 |
|
144 | 133 | }
|
145 | 134 |
|
146 |
| -func printShortMangaInfo(i mangadexapi.MangaInfo) { |
147 |
| - dp.Println(field.Sprint("Manga title: "), i.Title("en")) |
148 |
| - dp.Println(field.Sprint("Alt titles: "), i.AltTitles()) |
149 |
| - field.Println("Read or Buy here:") |
150 |
| - dp.Println(i.Links()) |
151 |
| - dp.Println("==============\n") |
152 |
| -} |
153 |
| - |
154 |
| -func printChapterInfo(c mangadexapi.ChapterFullInfo) { |
155 |
| - tableData := pterm.TableData{ |
156 |
| - {field.Sprint("Chapter"), dp.Sprint(c.Number())}, |
157 |
| - {field.Sprint("Chapter title"), dp.Sprint(c.Title())}, |
158 |
| - {field.Sprint("Volume"), dp.Sprint(c.Volume())}, |
159 |
| - {field.Sprint("Language"), dp.Sprint(c.Language())}, |
160 |
| - {field.Sprint("Translated by"), dp.Sprint(c.Translator())}, |
161 |
| - {field.Sprint("Uploaded by"), dp.Sprint(c.UploadedBy())}, |
162 |
| - } |
163 |
| - pterm.DefaultTable.WithData(tableData).Render() |
164 |
| -} |
165 |
| - |
166 | 135 | func downloadManga(cmd *cobra.Command, args []string) {
|
| 136 | + params := mdx.NewDownloadParam(chaptersRange, lowestChapter, highestChapter, language, |
| 137 | + translateGroup, outputDir, outputExt, isJpgFileFormat, isMergeChapters) |
167 | 138 | if mangaChapterId != "" {
|
168 |
| - downloadSingleChapter() |
169 |
| - return |
170 |
| - } |
171 |
| - |
172 |
| - c := mangadexapi.NewClient(MDX_USER_AGENT) |
173 |
| - |
174 |
| - spinnerMangaInfo, _ := pterm.DefaultSpinner.Start("Fetching manga info...") |
175 |
| - resp, err := c.GetMangaInfo(mangaId) |
176 |
| - if err != nil { |
177 |
| - spinnerMangaInfo.Fail("Failed to get manga info") |
178 |
| - e.Println("While getting manga info, maybe you set malformated link") |
179 |
| - os.Exit(1) |
180 |
| - } |
181 |
| - mangaInfo := resp.MangaInfo() |
182 |
| - spinnerMangaInfo.Success("Fetched manga info") |
183 |
| - |
184 |
| - if isLastChapter { |
185 |
| - downloadLastChapter(c, mangaInfo, |
186 |
| - language, translateGroup, outputExt, MDX_USER_AGENT, isJpgFileFormat) |
187 |
| - return |
188 |
| - } |
189 |
| - |
190 |
| - spinnerChapInfo, _ := pterm.DefaultSpinner.Start("Fetching chapters info...") |
191 |
| - chapters, err := c.GetFullChaptersInfo(mangaId, language, translateGroup, |
192 |
| - lowestChapter, highestChapter) |
193 |
| - if err != nil { |
194 |
| - spinnerChapInfo.Fail("Failed to get chapters info") |
195 |
| - e.Printf("While getting manga chapters: %v\n", err) |
196 |
| - os.Exit(1) |
197 |
| - } |
198 |
| - spinnerChapInfo.Success("Fetched chapters info") |
199 |
| - |
200 |
| - if len(chapters) == 0 { |
201 |
| - e.Printf("Chapters %s not found, try another "+ |
202 |
| - "range, language, translation group etc.\n", chaptersRange) |
203 |
| - os.Exit(0) |
204 |
| - } |
205 |
| - |
206 |
| - printShortMangaInfo(mangaInfo) |
207 |
| - |
208 |
| - if isMergeChapters { |
209 |
| - downloadMergeChapters(c, |
210 |
| - mangaInfo, chapters, outputExt, MDX_USER_AGENT, isJpgFileFormat) |
| 139 | + params.DownloadSpecificChapter(mangaChapterId) |
| 140 | + } else if isLastChapter { |
| 141 | + params.DownloadLastChapter(mangaId) |
211 | 142 | } else {
|
212 |
| - downloadChapters(c, |
213 |
| - mangaInfo, chapters, outputExt, MDX_USER_AGENT, isJpgFileFormat) |
214 |
| - } |
215 |
| -} |
216 |
| - |
217 |
| -func downloadSingleChapter() { |
218 |
| - c := mangadexapi.NewClient(MDX_USER_AGENT) |
219 |
| - spinnerChapInfo, _ := pterm.DefaultSpinner.Start("Fetching chapter info...") |
220 |
| - resp, err := c.GetChapterInfo(mangaChapterId) |
221 |
| - if err != nil { |
222 |
| - spinnerChapInfo.Fail("Failed to get chapter info") |
223 |
| - os.Exit(1) |
224 |
| - } |
225 |
| - chapterInfo := resp.GetChapterInfo() |
226 |
| - chapterFullInfo, err := c.GetChapterImagesInFullInfo(chapterInfo) |
227 |
| - if err != nil { |
228 |
| - spinnerChapInfo.Fail("Failed to get chapter info") |
229 |
| - os.Exit(1) |
230 |
| - } |
231 |
| - spinnerChapInfo.Success("Fetched chapter info") |
232 |
| - |
233 |
| - mangaId := chapterInfo.GetMangaId() |
234 |
| - spinnerMangaInfo, _ := pterm.DefaultSpinner.Start("Fetching manga info...") |
235 |
| - respManga, err := c.GetMangaInfo(mangaId) |
236 |
| - if err != nil { |
237 |
| - spinnerMangaInfo.Fail("Failed to get manga info") |
238 |
| - os.Exit(1) |
239 |
| - } |
240 |
| - mangaInfo := respManga.MangaInfo() |
241 |
| - spinnerMangaInfo.Success("Fetched manga info") |
242 |
| - chapArr := []mangadexapi.ChapterFullInfo{chapterFullInfo} |
243 |
| - printShortMangaInfo(mangaInfo) |
244 |
| - downloadChapters(c, mangaInfo, chapArr, outputExt, MDX_USER_AGENT, isJpgFileFormat) |
245 |
| -} |
246 |
| - |
247 |
| -func downloadLastChapter(c mangadexapi.Clientapi, |
248 |
| - i mangadexapi.MangaInfo, language, translationGroup, outputExt, userAgent string, |
249 |
| - isJpg bool) { |
250 |
| - |
251 |
| - spinnerChapInfo, _ := pterm.DefaultSpinner.Start("Fetching chapter info...") |
252 |
| - chapterFullInfo, err := c.GetLastChapterFullInfo(i.ID, language, translationGroup) |
253 |
| - if err != nil { |
254 |
| - spinnerChapInfo.Fail("Failed to get chapter info") |
255 |
| - e.Printf("While getting manga chapters: %v\n", err) |
256 |
| - os.Exit(1) |
257 |
| - } |
258 |
| - spinnerChapInfo.Success("Fetched chapter info") |
259 |
| - |
260 |
| - chapArr := []mangadexapi.ChapterFullInfo{chapterFullInfo} |
261 |
| - |
262 |
| - printShortMangaInfo(i) |
263 |
| - downloadChapters(c, i, chapArr, outputExt, userAgent, isJpg) |
264 |
| -} |
265 |
| - |
266 |
| -func downloadMergeChapters(client mangadexapi.Clientapi, |
267 |
| - mangaInfo mangadexapi.MangaInfo, |
268 |
| - chapters []mangadexapi.ChapterFullInfo, |
269 |
| - outputExtension, userAgent string, |
270 |
| - isJpg bool) { |
271 |
| - |
272 |
| - containerFile, err := filekit.NewContainer(outputExtension) |
273 |
| - if err != nil { |
274 |
| - e.Printf("While creating output file: %v\n", err) |
275 |
| - os.Exit(1) |
276 |
| - } |
277 |
| - |
278 |
| - for _, chapter := range chapters { |
279 |
| - printChapterInfo(chapter) |
280 |
| - |
281 |
| - err = downloadProcess(client, chapter, containerFile, isJpg) |
282 |
| - if err != nil { |
283 |
| - e.Printf("While downloading chapter: %v\n", err) |
284 |
| - os.Exit(1) |
285 |
| - } |
286 |
| - } |
287 |
| - |
288 |
| - filename := fmt.Sprintf("[%s] %s ch%s", |
289 |
| - language, mangaInfo.Title("en"), chaptersRange) |
290 |
| - metaInfo := metadata.NewMetadata(userAgent, mangaInfo, chapters[0]) |
291 |
| - err = containerFile.WriteOnDiskAndClose(outputDir, filename, metaInfo, chaptersRange) |
292 |
| - if err != nil { |
293 |
| - e.Printf("While saving %s on disk: %v\n", filename, err) |
294 |
| - os.Exit(1) |
295 |
| - } |
296 |
| -} |
297 |
| - |
298 |
| -func downloadChapters(client mangadexapi.Clientapi, |
299 |
| - mangaInfo mangadexapi.MangaInfo, |
300 |
| - chapters []mangadexapi.ChapterFullInfo, |
301 |
| - outputExtension, userAgent string, |
302 |
| - isJpg bool) { |
303 |
| - |
304 |
| - for _, chapter := range chapters { |
305 |
| - printChapterInfo(chapter) |
306 |
| - |
307 |
| - containerFile, err := filekit.NewContainer(outputExtension) |
308 |
| - if err != nil { |
309 |
| - e.Printf("While creating output file: %v\n", err) |
310 |
| - os.Exit(1) |
311 |
| - } |
312 |
| - |
313 |
| - err = downloadProcess(client, chapter, containerFile, isJpg) |
314 |
| - if err != nil { |
315 |
| - e.Printf("While downloading chapter: %v\n", err) |
316 |
| - os.Exit(1) |
317 |
| - } |
318 |
| - |
319 |
| - filename := fmt.Sprintf("[%s] %s vol%s ch%s", |
320 |
| - language, mangaInfo.Title("en"), chapter.Volume(), chapter.Number()) |
321 |
| - metaInfo := metadata.NewMetadata(userAgent, mangaInfo, chapter) |
322 |
| - err = containerFile.WriteOnDiskAndClose(outputDir, filename, metaInfo, "") |
323 |
| - if err != nil { |
324 |
| - e.Printf("While saving %s on disk: %v\n", filename, err) |
325 |
| - os.Exit(1) |
326 |
| - } |
327 |
| - } |
328 |
| -} |
329 |
| - |
330 |
| -func downloadProcess( |
331 |
| - client mangadexapi.Clientapi, |
332 |
| - chapter mangadexapi.ChapterFullInfo, |
333 |
| - outputFile filekit.Container, isJpg bool) error { |
334 |
| - |
335 |
| - files := chapter.PngFiles |
336 |
| - if isJpg { |
337 |
| - files = chapter.JpgFiles |
338 |
| - } |
339 |
| - |
340 |
| - dlbar, _ := pterm.DefaultProgressbar.WithTotal(len(files)). |
341 |
| - WithTitle("Downloading pages..."). |
342 |
| - WithBarStyle(pterm.NewStyle(pterm.FgGreen)).Start() |
343 |
| - defer dlbar.Stop() |
344 |
| - |
345 |
| - for _, imageFile := range files { |
346 |
| - outputImage, err := client.DownloadImage(chapter.DownloadBaseURL, |
347 |
| - chapter.HashId, imageFile, isJpg) |
348 |
| - if err != nil { |
349 |
| - dlbar.WithBarStyle(pterm.NewStyle(pterm.FgRed)). |
350 |
| - UpdateTitle("Failed downloading").Stop() |
351 |
| - return err |
352 |
| - } |
353 |
| - |
354 |
| - if err := outputFile.AddFile(imgExt, outputImage); err != nil { |
355 |
| - dlbar.WithBarStyle(pterm.NewStyle(pterm.FgRed)). |
356 |
| - UpdateTitle("Failed downloading").Stop() |
357 |
| - return err |
358 |
| - } |
359 |
| - dlbar.Increment() |
| 143 | + params.DownloadChapters(mangaId) |
360 | 144 | }
|
361 |
| - dp.Println("") |
362 |
| - return nil |
363 | 145 | }
|
0 commit comments