~q3cpma/mangadex-tools

ref: a76b1d2fc2717da81c60497fd31cdd417e11f675 mangadex-tools/mdex_dl.tcl -rwxr-xr-x 2.2 KiB
a76b1d2fq3cpma Centralize source once mechanism 2 months ago
                                                                                
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
#!/usr/bin/env tclsh
# TODO: group filtering ?
set scriptdir [file dirname [file dirname \
								 [file normalize [file join [info script] dummy]]]]
source [file join $scriptdir util.tcl]
source [file join $scriptdir mdex_util.tcl]
util::exec_require curl


# Option and argument handling
set optres [util::autocli \
	{
		proxy  {param {}   PROXY_URL {Set the curl HTTP/HTTPS proxy.}}
		lang   {param {en} LANG_CODE {Only download chapters in this language.}}
	} \
	[file tail [info script]] \
	{download MangaDex chapters} \
	{{MANGA_URL [CHAPTER_NUM...]} {CHAPTER_URL...} {MANGA_URL covers [VOLUME_NUM...]}} \
	{
		{Download each of the specified items (chapters or covers).}
		{If no item number list is specified, download all of them.}
	}]

if {$argc == 0} {
	util::usage stderr 1
}

dict assign $optres
if {$proxy ne ""} {
	set ::env(http_proxy)  $proxy
	set ::env(https_proxy) $proxy
}

if {[regexp "^$URL_BASE_RE/title/($UUID_RE)(?:/\[a-z-\]+)?\$" [lindex $argv 0] -> mid]} {
	if {$argc > 1 && [lindex $argv 1] eq "covers"} {
		dl_covers $mid $lang [lrange $argv 2 end]
		exit
	}
	if {[catch {get_chapter_list $mid $lang} chapters]} {
		util::die "Failed to download chapter list JSON!\n\n$chapters"
	}
	# Only keep specified chapters
	if {$argc > 1} {
		set chapters [util::lfilter ch $chapters {
			[dict get $ch data attributes chapter] in [lrange $argv 1 end]
		}]
	}
} else {
	set cids [lmap url $argv {
		if {![regexp "^$URL_BASE_RE/chapter/($UUID_RE)\$" $url -> cid]} {
			util::die "$url: not a chapter or manga URL"
		}
		set cid
	}]
	set chapters [lmap cid $cids {
		if {[catch {get_chapter $cid} chapter]} {
			puts stderr "Failed to download chapter $cid JSON\n\n$chapter"
			continue
		}
		if {$argc > 5} {
			after 300; # Sleep to avoid hitting the rate limit of 5 req/s
		}
		set chapter
	}]
}

# Iterate over the filtered chapters and download
foreach ch $chapters {
	set outdir [util::path_sanitize [chapter_dirname $ch $lang]]
	if {[file exists $outdir] && ![util::is_dir_empty $outdir]} {
		continue
	}
	file mkdir $outdir
	puts stderr "\[[incr count]/[llength $chapters]\] Downloading $outdir..."
	if {[catch {dl_chapter $ch $outdir} err]} {
		puts stderr "Failed to download $outdir!\n\n$err"
		file delete -force -- $outdir
	}
}