Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
Girbons committed Nov 16, 2018
1 parent e520c52 commit 6fe1324
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,6 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# path where comics are downloaded
comics/
26 changes: 26 additions & 0 deletions README.md
@@ -0,0 +1,26 @@
# Comics Downloader (Go version)

## Supported Sites

- http://www.comicextra.com/

## Usage

```bash
./comics-downloader -url=http://source
```

## Built With

- [go](https://github.com/golang/go)
- [gofpdf](https://github.com/jung-kurt/gofpdf)
- [soup](https://github.com/anaskhan96/soup)
- [progressbar](https://github.com/schollz/progressbar)

## Contribuiting

Feel free to submit a pull request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
88 changes: 88 additions & 0 deletions core.go
@@ -0,0 +1,88 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/anaskhan96/soup"
"github.com/jung-kurt/gofpdf"
"github.com/schollz/progressbar"
)

type Comic struct {
Name string
IssueNumber string
ImageRegex string
Source string
}

func isUrlValid(url string) bool {
return !strings.Contains(url, ".gif") && !strings.Contains(url, "logo")
}

func imagesLink(res, regex string) []string {
re := regexp.MustCompile(regex)
match := re.FindAllStringSubmatch(res, -1)

out := make([]string, len(match))

for i := range out {
url := match[i][1]
if isUrlValid(url) {
out[i] = url
}
}
return out
}

func NewComic(name, issueNumber, imageRegex, source string) *Comic {
return &Comic{
Name: name,
IssueNumber: issueNumber,
ImageRegex: imageRegex,
Source: source,
}
}

func (c *Comic) MakeComic(url, source string, splittedUrl []string) {
res, _ := soup.Get(url)

// retrieve links
links := imagesLink(res, c.ImageRegex)
pdf := gofpdf.New("P", "mm", "A4", "")

bar := progressbar.New(len(links))
bar.RenderBlank()

for i, link := range links {
bar.Add(i)
if link != "" {
rsp, err := http.Get(link)

if err == nil {
pdf.AddPage()
tp := pdf.ImageTypeFromMime(rsp.Header["Content-Type"][0])
pdf.RegisterImageReader(link, tp, rsp.Body)
pdf.Image(link, 0, 0, 210, 0, false, "", 0, "")
} else {
pdf.SetError(err)
}
}
}
source, err := UrlSource(url)

if err != nil {
fmt.Println("Can't detect source ")
}

dir, _ := filepath.Abs(fmt.Sprintf("%s/%s/%s/%s/", filepath.Dir(os.Args[0]), "comics", c.Source, c.Name))
os.MkdirAll(dir, os.ModePerm)

pdfErr := pdf.OutputFileAndClose(fmt.Sprintf("%s/%s.pdf", dir, c.IssueNumber))
log.Fatal(pdfErr)
}
23 changes: 23 additions & 0 deletions detector.go
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"os"
)

func DetectComic(url string) {
source, _ := UrlSource(url)
splittedUrl := SplitUrl(url)

switch source {
case "www.comicextra.com":
name := splittedUrl[3]
issueNumber := splittedUrl[4]
imageRegex := `<img[^>]+src="([^">]+)"`
comic := NewComic(name, issueNumber, imageRegex, source)
comic.MakeComic(url, source, splittedUrl)
default:
fmt.Println("This site is not supported :(")
os.Exit(0)
}
}
27 changes: 27 additions & 0 deletions main.go
@@ -0,0 +1,27 @@
package main

import (
"flag"
"fmt"
"log"
"os"
)

func usage() {
fmt.Println("comics-download -u=http://comic-source")
os.Exit(0)
}

func main() {
url := flag.String("url", "", "Comic URL")

flag.Usage = usage
flag.Parse()

if *url == "" {
log.Fatal("url parameter is required")
}

DetectComic(*url)
fmt.Println("Download Completed")
}
20 changes: 20 additions & 0 deletions utils.go
@@ -0,0 +1,20 @@
package main

import (
"net/url"
"strings"
)

func SplitUrl(u string) []string {
return strings.Split(u, "/")
}

func UrlSource(u string) (string, error) {
parsedUrl, err := url.Parse(u)

if err != nil {
return "", err
}

return parsedUrl.Hostname(), nil
}

0 comments on commit 6fe1324

Please sign in to comment.