From bb77a5a5fef4cabef55ec2e7a3e5269a7395ba86 Mon Sep 17 00:00:00 2001 From: Leo Robinovitch Date: Thu, 21 Dec 2023 09:20:50 -0800 Subject: [PATCH] finish --- .github/workflows/build.yml | 29 +++++++++++++++ .github/workflows/release.yml | 35 ++++++++++++++++++ .gitignore | 7 ++-- .goreleaser.yaml | 65 +++++++++++++++++++++++++++++++++ main.go | 68 ++++++++++++++++++++++++----------- 5 files changed, 181 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/release.yml create mode 100644 .goreleaser.yaml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..8d5a343 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,29 @@ +name: build + +on: + pull_request: + push: + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Install Nix + uses: cachix/install-nix-action@v23 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: build --snapshot --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..042b977 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,35 @@ +name: release + +on: + create: + tags: + - v* + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Install Nix + uses: cachix/install-nix-action@v23 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }} + AUR_KEY: ${{ secrets.AUR_KEY }} + NUR_PACKAGES_GITHUB_TOKEN: ${{ secrets.NUR_PACKAGES_GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 4150d84..e05051a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea -image* -img* -out* +*.png +*.jpg +*.jpeg +*.pdf diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..9047de8 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,65 @@ +project_name: webtoon-dl + +before: + hooks: + - go mod tidy + +builds: + - binary: webtoon-dl + env: + - CGO_ENABLED=0 + goos: + - darwin + - freebsd + - linux + - windows + +archives: + - name_template: >- + {{ .ProjectName }}_{{ .Version }}_ + {{- if eq .Os "darwin" }}Darwin + {{- else if eq .Os "linux" }}Linux + {{- else if eq .Os "windows" }}Windows + {{- else }}{{ .Os }}{{ end }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + +checksum: + name_template: 'checksums.txt' + +snapshot: + name_template: "{{ incpatch .Version }}-next" + +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + +universal_binaries: + - replace: true + +release: + github: + owner: robinovitch61 + name: webtoon-dl + +brews: + - name: webtoon-dl + homepage: https://github.com/robinovitch61/webtoon-dl + description: "Download webtoon comics as pdfs." + folder: Formula + commit_author: + name: "Leo Robinovitch" + email: "leorobinovitch@gmail.com" + commit_msg_template: "Brew formula update for {{ .ProjectName }} version {{ .Tag }}" + repository: + owner: robinovitch61 + name: homebrew-tap + branch: main + token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}" + +gomod: + proxy: true \ No newline at end of file diff --git a/main.go b/main.go index 05d407a..ff698a9 100644 --- a/main.go +++ b/main.go @@ -9,8 +9,25 @@ import ( "io" "net/http" "os" + "strings" ) +func getImgLinks(url string) []string { + resp, err := soup.Get(url) + if err != nil { + fmt.Println(fmt.Sprintf("Error fetching page: %v", err)) + os.Exit(1) + } + doc := soup.HTMLParse(resp) + imgs := doc.Find("div", "class", "viewer_lst").FindAll("img") + + var imgLinks []string + for _, img := range imgs { + imgLinks = append(imgLinks, img.Attrs()["data-url"]) + } + return imgLinks +} + func fetchImage(imgLink string) []byte { req, e := http.NewRequest("GET", imgLink, nil) if e != nil { @@ -21,36 +38,34 @@ func fetchImage(imgLink string) []byte { response, err := http.DefaultClient.Do(req) if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } }(response.Body) buff := new(bytes.Buffer) _, err = buff.ReadFrom(response.Body) if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } return buff.Bytes() } func main() { - resp, err := soup.Get("https://www.webtoons.com/en/romance/down-to-earth/s2-episode-169/viewer?title_no=1817&episode_no=169") - if err != nil { - fmt.Println(fmt.Sprintf("Error fetching page: %v", err)) + if len(os.Args) < 2 { + fmt.Println("Usage: webtoon-dl ") os.Exit(1) } - doc := soup.HTMLParse(resp) - imgs := doc.Find("div", "class", "viewer_lst").FindAll("img") - var imgLinks []string - for _, img := range imgs { - imgLinks = append(imgLinks, img.Attrs()["data-url"]) - } - println(fmt.Sprintf("Found %d images", len(imgLinks))) + url := os.Args[1] + imgLinks := getImgLinks(url) + fmt.Println(fmt.Sprintf("found %d pages", len(imgLinks))) pdf := gopdf.GoPdf{} pdf.Start(gopdf.Config{Unit: gopdf.UnitPT, PageSize: *gopdf.PageSizeA4}) @@ -59,30 +74,43 @@ func main() { img := fetchImage(imgLink) holder, err := gopdf.ImageHolderByBytes(img) if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } d, _, err := image.DecodeConfig(bytes.NewReader(img)) if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } // 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 ifrom nches, then to points - // subtract 1 to account for small margins + // 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, }}) err = pdf.ImageByHolder(holder, 0, 0, nil) if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } } - err = pdf.WritePdf("out.pdf") + outURL := strings.ReplaceAll(url, "http://", "") + outURL = strings.ReplaceAll(outURL, "https://", "") + outURL = strings.ReplaceAll(outURL, "www.", "") + outURL = strings.ReplaceAll(outURL, "webtoons.com/", "") + outURL = strings.Split(outURL, "?")[0] + outURL = strings.ReplaceAll(outURL, "/viewer", "") + outURL = strings.ReplaceAll(outURL, string(os.PathSeparator), "-") + outPath := "./" + outURL + ".pdf" + err := pdf.WritePdf(outPath) if err != nil { - panic(err) + fmt.Println(err.Error()) + os.Exit(1) } + fmt.Println(fmt.Sprintf("saved to %s", outPath)) }