Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |