cleanup
This commit is contained in:
parent
5ecf31612d
commit
ad5dca42cb
2 changed files with 83 additions and 112 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# webtoon-dl
|
# webtoon-dl
|
||||||
|
|
||||||
Download [webtoon](https://www.webtoons.com/en/) comics as PDFs using a terminal/command line.
|
Download [webtoon](https://www.webtoons.com/en/) comics as PDF or CBZ using a terminal/command line.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
@ -11,6 +11,9 @@ webtoon-dl "<your-webtoon-episode-url>"
|
||||||
# download entire series, default 10 episodes per pdf
|
# download entire series, default 10 episodes per pdf
|
||||||
webtoon-dl "<your-webtoon-series-url>"
|
webtoon-dl "<your-webtoon-series-url>"
|
||||||
|
|
||||||
|
# download as cbz (default is pdf)
|
||||||
|
webtoon-dl --format cbz "<your-webtoon-series-url>"
|
||||||
|
|
||||||
# specify a range of episodes (inclusive on both ends)
|
# specify a range of episodes (inclusive on both ends)
|
||||||
webtoon-dl --min-ep=10 --max-ep=20 "<your-webtoon-series-url>"
|
webtoon-dl --min-ep=10 --max-ep=20 "<your-webtoon-series-url>"
|
||||||
|
|
||||||
|
|
|
||||||
182
main.go
182
main.go
|
|
@ -32,25 +32,25 @@ type EpisodeBatch struct {
|
||||||
maxEp int
|
maxEp int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Comic interface {
|
type ComicFile interface {
|
||||||
addImage([]byte) error
|
addImage([]byte) error
|
||||||
save(outputPath string) error
|
save(outputPath string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type PDFComic struct {
|
type PDFComicFile struct {
|
||||||
pdf *gopdf.GoPdf
|
pdf *gopdf.GoPdf
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate PDFComic implements Comic
|
// validate PDFComicFile implements ComicFile
|
||||||
var _ Comic = &PDFComic{}
|
var _ ComicFile = &PDFComicFile{}
|
||||||
|
|
||||||
func newPDFComic() *PDFComic {
|
func newPDFComicFile() *PDFComicFile {
|
||||||
pdf := gopdf.GoPdf{}
|
pdf := gopdf.GoPdf{}
|
||||||
pdf.Start(gopdf.Config{Unit: gopdf.UnitPT, PageSize: *gopdf.PageSizeA4})
|
pdf.Start(gopdf.Config{Unit: gopdf.UnitPT, PageSize: *gopdf.PageSizeA4})
|
||||||
return &PDFComic{pdf: &pdf}
|
return &PDFComicFile{pdf: &pdf}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PDFComic) addImage(img []byte) error {
|
func (c *PDFComicFile) addImage(img []byte) error {
|
||||||
holder, err := gopdf.ImageHolderByBytes(img)
|
holder, err := gopdf.ImageHolderByBytes(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -72,29 +72,29 @@ func (c *PDFComic) addImage(img []byte) error {
|
||||||
return c.pdf.ImageByHolder(holder, 0, 0, nil)
|
return c.pdf.ImageByHolder(holder, 0, 0, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PDFComic) save(outputPath string) error {
|
func (c *PDFComicFile) save(outputPath string) error {
|
||||||
return c.pdf.WritePdf(outputPath)
|
return c.pdf.WritePdf(outputPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CBZComic struct {
|
type CBZComicFile struct {
|
||||||
zipWriter *zip.Writer
|
zipWriter *zip.Writer
|
||||||
outFile *os.File
|
outFile *os.File
|
||||||
numFiles int
|
numFiles int
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate CBZComic implements Comic
|
// validate CBZComicFile implements ComicFile
|
||||||
var _ Comic = &CBZComic{}
|
var _ ComicFile = &CBZComicFile{}
|
||||||
|
|
||||||
func newCBZComic() (*CBZComic, error) {
|
func newCBZComicFile() (*CBZComicFile, error) {
|
||||||
out, err := os.CreateTemp("", "output.tmp.cbz")
|
out, err := os.CreateTemp("", "output.tmp.cbz")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
zipWriter := zip.NewWriter(out)
|
zipWriter := zip.NewWriter(out)
|
||||||
return &CBZComic{zipWriter: zipWriter, outFile: out, numFiles: 0}, nil
|
return &CBZComicFile{zipWriter: zipWriter, outFile: out, numFiles: 0}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CBZComic) addImage(img []byte) error {
|
func (c *CBZComicFile) addImage(img []byte) error {
|
||||||
f, err := c.zipWriter.Create(fmt.Sprintf("%d.jpg", c.numFiles))
|
f, err := c.zipWriter.Create(fmt.Sprintf("%d.jpg", c.numFiles))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -107,7 +107,7 @@ func (c *CBZComic) addImage(img []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CBZComic) save(outputPath string) error {
|
func (c *CBZComicFile) save(outputPath string) error {
|
||||||
err := c.zipWriter.Close()
|
err := c.zipWriter.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -342,37 +342,37 @@ func fetchImage(imgLink string) []byte {
|
||||||
return buff.Bytes()
|
return buff.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func addImgToPdf(pdf *gopdf.GoPdf, imgLink string) error {
|
func getComicFile(format string) ComicFile {
|
||||||
img := fetchImage(imgLink)
|
var comic ComicFile
|
||||||
holder, err := gopdf.ImageHolderByBytes(img)
|
var err error
|
||||||
|
comic = newPDFComicFile()
|
||||||
|
if format == "cbz" {
|
||||||
|
comic, err = newCBZComicFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
d, _, err := image.DecodeConfig(bytes.NewReader(img))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return comic
|
||||||
// gopdf assumes dpi 128 https://github.com/signintech/gopdf/issues/168
|
|
||||||
// W and H are in points, 1 point = 1/72 inch
|
|
||||||
// convert pixels (Width and Height) to points
|
|
||||||
// subtract 1 point to account for margins
|
|
||||||
pdf.AddPageWithOption(gopdf.PageOption{PageSize: &gopdf.Rect{
|
|
||||||
W: float64(d.Width)*72/128 - 1,
|
|
||||||
H: float64(d.Height)*72/128 - 1,
|
|
||||||
}})
|
|
||||||
return pdf.ImageByHolder(holder, 0, 0, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
type Opts struct {
|
||||||
if len(os.Args) < 2 {
|
url string
|
||||||
|
minEp int
|
||||||
|
maxEp int
|
||||||
|
epsPerFile int
|
||||||
|
format string
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOpts(args []string) Opts {
|
||||||
|
if len(args) < 2 {
|
||||||
fmt.Println("Usage: webtoon-dl <url>")
|
fmt.Println("Usage: webtoon-dl <url>")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
minEp := flag.Int("min-ep", 0, "Minimum episode number to download (inclusive)")
|
minEp := flag.Int("min-ep", 0, "Minimum episode number to download (inclusive)")
|
||||||
maxEp := flag.Int("max-ep", math.MaxInt, "Maximum episode number to download (inclusive)")
|
maxEp := flag.Int("max-ep", math.MaxInt, "Maximum episode number to download (inclusive)")
|
||||||
epsPerFile := flag.Int("eps-per-file", 10, "Number of episodes to put in each PDF file")
|
epsPerFile := flag.Int("eps-per-file", 10, "Number of episodes to put in each PDF file")
|
||||||
|
format := flag.String("format", "pdf", "Output format (pdf or cbz)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *minEp > *maxEp {
|
if *minEp > *maxEp {
|
||||||
fmt.Println("min-ep must be less than or equal to max-ep")
|
fmt.Println("min-ep must be less than or equal to max-ep")
|
||||||
|
|
@ -388,19 +388,17 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
url := os.Args[len(os.Args)-1]
|
url := os.Args[len(os.Args)-1]
|
||||||
episodeBatches := getEpisodeBatches(url, *minEp, *maxEp, *epsPerFile)
|
return Opts{
|
||||||
|
url: url,
|
||||||
totalPages := 0
|
minEp: *minEp,
|
||||||
for _, episodeBatch := range episodeBatches {
|
maxEp: *maxEp,
|
||||||
totalPages += len(episodeBatch.imgLinks)
|
epsPerFile: *epsPerFile,
|
||||||
|
format: *format,
|
||||||
}
|
}
|
||||||
totalEpisodes := episodeBatches[len(episodeBatches)-1].maxEp - episodeBatches[0].minEp + 1
|
}
|
||||||
fmt.Println(fmt.Sprintf("found %d total image links across %d episodes", totalPages, totalEpisodes))
|
|
||||||
fmt.Println(fmt.Sprintf("saving into %d files with max of %d episodes per file", len(episodeBatches), *epsPerFile))
|
|
||||||
|
|
||||||
for _, episodeBatch := range episodeBatches {
|
func getOutFile(opts Opts, episodeBatch EpisodeBatch) string {
|
||||||
var err error
|
outURL := strings.ReplaceAll(opts.url, "http://", "")
|
||||||
outURL := strings.ReplaceAll(url, "http://", "")
|
|
||||||
outURL = strings.ReplaceAll(outURL, "https://", "")
|
outURL = strings.ReplaceAll(outURL, "https://", "")
|
||||||
outURL = strings.ReplaceAll(outURL, "www.", "")
|
outURL = strings.ReplaceAll(outURL, "www.", "")
|
||||||
outURL = strings.ReplaceAll(outURL, "webtoons.com/", "")
|
outURL = strings.ReplaceAll(outURL, "webtoons.com/", "")
|
||||||
|
|
@ -408,83 +406,53 @@ func main() {
|
||||||
outURL = strings.ReplaceAll(outURL, "/viewer", "")
|
outURL = strings.ReplaceAll(outURL, "/viewer", "")
|
||||||
outURL = strings.ReplaceAll(outURL, "/", "-")
|
outURL = strings.ReplaceAll(outURL, "/", "-")
|
||||||
if episodeBatch.minEp != episodeBatch.maxEp {
|
if episodeBatch.minEp != episodeBatch.maxEp {
|
||||||
outURL = fmt.Sprintf("%s-epNo%d-epNo%d", outURL, episodeBatch.minEp, episodeBatch.maxEp)
|
outURL = fmt.Sprintf("%s-epNo%d-epNo%d.%s", outURL, episodeBatch.minEp, episodeBatch.maxEp, opts.format)
|
||||||
} else {
|
} else {
|
||||||
outURL = fmt.Sprintf("%s-epNo%d", outURL, episodeBatch.minEp)
|
outURL = fmt.Sprintf("%s-epNo%d.%s", outURL, episodeBatch.minEp, opts.format)
|
||||||
}
|
}
|
||||||
//comic := newPDFComic()
|
return outURL
|
||||||
//outPath := outURL + ".pdf"
|
}
|
||||||
comic, err := newCBZComic()
|
|
||||||
if err != nil {
|
func main() {
|
||||||
fmt.Println(err.Error())
|
opts := parseOpts(os.Args)
|
||||||
os.Exit(1)
|
episodeBatches := getEpisodeBatches(opts.url, opts.minEp, opts.maxEp, opts.epsPerFile)
|
||||||
|
totalPages := 0
|
||||||
|
for _, episodeBatch := range episodeBatches {
|
||||||
|
totalPages += len(episodeBatch.imgLinks)
|
||||||
}
|
}
|
||||||
outPath := outURL + ".cbz"
|
totalEpisodes := episodeBatches[len(episodeBatches)-1].maxEp - episodeBatches[0].minEp + 1
|
||||||
|
fmt.Println(fmt.Sprintf("found %d total image links across %d episodes", totalPages, totalEpisodes))
|
||||||
|
fmt.Println(fmt.Sprintf("saving into %d files with max of %d episodes per file", len(episodeBatches), opts.epsPerFile))
|
||||||
|
|
||||||
|
for _, episodeBatch := range episodeBatches {
|
||||||
|
var err error
|
||||||
|
outFile := getOutFile(opts, episodeBatch)
|
||||||
|
comicFile := getComicFile(opts.format)
|
||||||
for idx, imgLink := range episodeBatch.imgLinks {
|
for idx, imgLink := range episodeBatch.imgLinks {
|
||||||
if strings.Contains(imgLink, ".gif") {
|
if strings.Contains(imgLink, ".gif") {
|
||||||
fmt.Println(fmt.Sprintf("WARNING: skipping gif %s", imgLink))
|
fmt.Println(fmt.Sprintf("WARNING: skipping gif %s", imgLink))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := comic.addImage(fetchImage(imgLink))
|
err := comicFile.addImage(fetchImage(imgLink))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println(fmt.Sprintf("saving episodes %d through %d: added page %d/%d", episodeBatch.minEp, episodeBatch.maxEp, idx+1, len(episodeBatch.imgLinks)))
|
fmt.Println(
|
||||||
|
fmt.Sprintf(
|
||||||
|
"saving episodes %d through %d: added page %d/%d",
|
||||||
|
episodeBatch.minEp,
|
||||||
|
episodeBatch.maxEp,
|
||||||
|
idx+1,
|
||||||
|
len(episodeBatch.imgLinks),
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
err = comicFile.save(outFile)
|
||||||
err = comic.save(outPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println(fmt.Sprintf("saved to %s", outPath))
|
fmt.Println(fmt.Sprintf("saved to %s", outFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
//createCbz := func(episodeBatches []EpisodeBatch, outPath string) error {
|
|
||||||
// out, err := os.Create(outPath)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// defer func(out *os.File) {
|
|
||||||
// err := out.Close()
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Println(err.Error())
|
|
||||||
// os.Exit(1)
|
|
||||||
// }
|
|
||||||
// }(out)
|
|
||||||
//
|
|
||||||
// zipWriter := zip.NewWriter(out)
|
|
||||||
// for _, episodeBatch := range episodeBatches {
|
|
||||||
// for idx, imgLink := range episodeBatch.imgLinks {
|
|
||||||
// if strings.Contains(imgLink, ".gif") {
|
|
||||||
// fmt.Println(fmt.Sprintf("WARNING: skipping gif %s", imgLink))
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// img := fetchImage(imgLink)
|
|
||||||
// f, err := zipWriter.Create(fmt.Sprintf("%d.jpg", idx))
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// _, err = f.Write(img)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// fmt.Println(fmt.Sprintf("saving episodes %d through %d: added page %d/%d", episodeBatch.minEp, episodeBatch.maxEp, idx+1, len(episodeBatch.imgLinks)))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// err = zipWriter.Close()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
//}
|
|
||||||
//// create cbz output from image links
|
|
||||||
//outPath := "output.cbz"
|
|
||||||
//err := createCbz(episodeBatches, outPath)
|
|
||||||
//if err != nil {
|
|
||||||
// fmt.Println(err.Error())
|
|
||||||
// os.Exit(1)
|
|
||||||
//}
|
|
||||||
//fmt.Println(fmt.Sprintf("saved to %s", outPath))
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue