This commit is contained in:
Leo Robinovitch 2023-12-21 09:20:50 -08:00
parent a0c22ded86
commit bb77a5a5fe
5 changed files with 181 additions and 23 deletions

29
.github/workflows/build.yml vendored Normal file
View file

@ -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 }}

35
.github/workflows/release.yml vendored Normal file
View file

@ -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 }}

7
.gitignore vendored
View file

@ -1,4 +1,5 @@
.idea
image*
img*
out*
*.png
*.jpg
*.jpeg
*.pdf

65
.goreleaser.yaml Normal file
View file

@ -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

68
main.go
View file

@ -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 <url>")
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))
}