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
/
manga_stats.go
109 lines (96 loc) · 3.34 KB
/
manga_stats.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
package manga
import (
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/rl404/go-malscraper/model"
"github.com/rl404/go-malscraper/pkg/utils"
)
type stats struct {
area *goquery.Selection
data model.Stats
}
// GetStats to get manga stats.
func (p *parser) GetStats(a *goquery.Selection) *model.Stats {
v := stats{area: a}
v.setSummary()
v.setScore()
return &v.data
}
func (s *stats) setSummary() {
area := s.area.Find("h2").First().Next()
for goquery.NodeName(area) == "div" {
summaryType := area.Find("span").Text()
summaryType = strings.Replace(summaryType, ":", "", -1)
summaryType = strings.ToLower(summaryType)
area.Find("span").Remove()
summaryValue := area.Text()
switch strings.TrimSpace(summaryType) {
case "reading":
s.data.Summary.Current = utils.StrToNum(summaryValue)
case "completed":
s.data.Summary.Completed = utils.StrToNum(summaryValue)
case "on-hold":
s.data.Summary.OnHold = utils.StrToNum(summaryValue)
case "dropped":
s.data.Summary.Dropped = utils.StrToNum(summaryValue)
case "plan to read":
s.data.Summary.Planned = utils.StrToNum(summaryValue)
case "total":
s.data.Summary.Total = utils.StrToNum(summaryValue)
}
area = area.Next()
}
}
func (s *stats) setScore() {
s.area.Find("h2").First().Remove()
area := s.area.Find("h2").First().Next()
if goquery.NodeName(area) == "table" {
area.Find("tr").Each(func(i int, eachScore *goquery.Selection) {
switch s.getScoreType(eachScore) {
case 1:
s.data.Score.Score1.Vote = s.getScoreVote(eachScore)
s.data.Score.Score1.Percent = s.getScorePercent(eachScore)
case 2:
s.data.Score.Score2.Vote = s.getScoreVote(eachScore)
s.data.Score.Score2.Percent = s.getScorePercent(eachScore)
case 3:
s.data.Score.Score3.Vote = s.getScoreVote(eachScore)
s.data.Score.Score3.Percent = s.getScorePercent(eachScore)
case 4:
s.data.Score.Score4.Vote = s.getScoreVote(eachScore)
s.data.Score.Score4.Percent = s.getScorePercent(eachScore)
case 5:
s.data.Score.Score5.Vote = s.getScoreVote(eachScore)
s.data.Score.Score5.Percent = s.getScorePercent(eachScore)
case 6:
s.data.Score.Score6.Vote = s.getScoreVote(eachScore)
s.data.Score.Score6.Percent = s.getScorePercent(eachScore)
case 7:
s.data.Score.Score7.Vote = s.getScoreVote(eachScore)
s.data.Score.Score7.Percent = s.getScorePercent(eachScore)
case 8:
s.data.Score.Score8.Vote = s.getScoreVote(eachScore)
s.data.Score.Score8.Percent = s.getScorePercent(eachScore)
case 9:
s.data.Score.Score9.Vote = s.getScoreVote(eachScore)
s.data.Score.Score9.Percent = s.getScorePercent(eachScore)
case 10:
s.data.Score.Score10.Vote = s.getScoreVote(eachScore)
s.data.Score.Score10.Percent = s.getScorePercent(eachScore)
}
})
}
}
func (s *stats) getScoreType(eachScore *goquery.Selection) int {
return utils.StrToNum(eachScore.Find("td").First().Text())
}
func (s *stats) getScoreVote(eachScore *goquery.Selection) int {
vote := eachScore.Find("td:nth-of-type(2) span small").Text()
vote = strings.Replace(vote, " votes", "", -1)
return utils.StrToNum(vote[1 : len(vote)-1])
}
func (s *stats) getScorePercent(eachScore *goquery.Selection) float64 {
eachScore.Find("td:nth-of-type(2) span small").Remove()
percent := eachScore.Find("td:nth-of-type(2) span").Text()
return utils.StrToFloat(strings.Replace(percent, "%", "", -1))
}