-
Notifications
You must be signed in to change notification settings - Fork 49
/
readallcomics.go
121 lines (91 loc) · 2.78 KB
/
readallcomics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package sites
import (
"fmt"
"strings"
"github.com/Girbons/comics-downloader/pkg/config"
"github.com/Girbons/comics-downloader/pkg/core"
"github.com/Girbons/comics-downloader/pkg/util"
"github.com/anaskhan96/soup"
)
// Readallcomics represents a Readallcomics instance.
type Readallcomics struct {
options *config.Options
}
// NewReadallcomics returns a new Readallcomics instance.
func NewReadallcomics(options *config.Options) *Readallcomics {
return &Readallcomics{
options: options,
}
}
func (r *Readallcomics) retrieveImageLinks(comic *core.Comic) ([]string, error) {
var links []string
response, err := soup.Get(comic.URLSource)
if err != nil {
return links, err
}
document := soup.HTMLParse(response)
images := document.FindAll("img")
for _, img := range images {
url := img.Attrs()["src"]
if util.IsURLValid(url) {
links = append(links, url)
}
}
if r.options.Debug {
r.options.Logger.Debug(fmt.Sprintf("Image links found: %s", strings.Join(links, " ")))
}
return links, err
}
func (r *Readallcomics) isSingleIssue(url string) bool {
response, _ := soup.Get(url)
doc := soup.HTMLParse(response)
chapters := doc.Find("select", "id", "selectbox").FindAll("option")
return len(chapters) == 1
}
func (r *Readallcomics) retrieveLastIssue(url string) (string, error) {
response, err := soup.Get(url)
doc := soup.HTMLParse(response)
chapters := doc.Find("select", "id", "selectbox").FindAll("option")
return chapters[0].Attrs()["value"], err
}
// RetrieveIssueLinks retrieves the links to all the issue.
func (r *Readallcomics) RetrieveIssueLinks() ([]string, error) {
url := r.options.URL
if r.options.Last {
lastIssue, err := r.retrieveLastIssue(url)
return []string{lastIssue}, err
}
if r.options.All && r.isSingleIssue(url) {
return []string{url}, nil
}
response, err := soup.Get(url)
if err != nil {
return nil, err
}
doc := soup.HTMLParse(response)
chapters := doc.Find("select", "id", "selectbox").FindAll("option")
var links []string
for _, chapter := range chapters {
url := chapter.Attrs()["value"]
if util.IsURLValid(url) {
links = append(links, url)
}
}
return links, err
}
// GetInfo extracts the comic info from the given URL.
func (r *Readallcomics) GetInfo(url string) (string, string) {
parts := util.TrimAndSplitURL(url)
lastPart := parts[len(parts)-1]
title := strings.Replace(lastPart, "-", " ", -1)
splittedTitle := strings.Split(title, " ")
name := strings.Join(splittedTitle[:len(splittedTitle)-2], " ")
issueNumber := splittedTitle[len(splittedTitle)-2] // get the issue number
return name, issueNumber
}
// Initialize prepare the comic instance with links and images.
func (r *Readallcomics) Initialize(comic *core.Comic) error {
links, err := r.retrieveImageLinks(comic)
comic.Links = links
return err
}