This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
people_character.go
69 lines (60 loc) · 1.93 KB
/
people_character.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
package people
import (
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/rl404/go-malscraper/model"
"github.com/rl404/go-malscraper/pkg/utils"
)
type character struct {
area *goquery.Selection
cleanImg bool
data []model.PeopleCharacter
}
// GetCharacters to get people anime character list.
func (p *parser) GetCharacters(a *goquery.Selection) []model.PeopleCharacter {
v := character{area: a, cleanImg: p.cleanImg}
v.setDetail()
return v.data
}
func (c *character) setDetail() {
actors := []model.PeopleCharacter{}
area := c.area.Find("#content table tr td").Next()
area = area.Find(".normal_header").First().Next()
if goquery.NodeName(area) == "table" {
area.Find("tr").Each(func(i int, va *goquery.Selection) {
animeImageArea := va.Find("td:nth-of-type(1)")
animeArea := va.Find("td:nth-of-type(2)")
charImageArea := va.Find("td:nth-of-type(4)")
charArea := va.Find("td:nth-of-type(3)")
actors = append(actors, model.PeopleCharacter{
Anime: model.Role{
ID: c.getID(animeArea),
Name: c.getTitle(animeArea),
Image: c.getImage(animeImageArea),
Role: c.getRole(charArea),
},
Character: model.Role{
ID: c.getID(charArea),
Name: c.getTitle(charArea),
Role: c.getRole(charArea),
Image: c.getImage(charImageArea),
},
})
})
}
c.data = actors
}
func (c *character) getID(animeArea *goquery.Selection) int {
animeID, _ := animeArea.Find("a").Attr("href")
return utils.StrToNum(utils.GetValueFromSplit(animeID, "/", 4))
}
func (c *character) getImage(animeImageArea *goquery.Selection) string {
animeImage, _ := animeImageArea.Find("img").Attr("data-src")
return utils.URLCleaner(animeImage, "image", c.cleanImg)
}
func (c *character) getTitle(animeArea *goquery.Selection) string {
return animeArea.Find("a").First().Text()
}
func (c *character) getRole(animeArea *goquery.Selection) string {
return strings.TrimSpace(animeArea.Find("div").Text())
}