-
Notifications
You must be signed in to change notification settings - Fork 4
/
achievement.go
80 lines (66 loc) · 2.22 KB
/
achievement.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
package godestone
import (
"strconv"
"time"
"github.com/gocolly/colly/v2"
"github.com/xivapi/godestone/v2/provider/models"
)
func (s *Scraper) buildAchievementCollector(aai *AllAchievementInfo, output chan *AchievementInfo, errors chan error) *colly.Collector {
c := colly.NewCollector(
colly.UserAgent(s.meta.UserAgentDesktop),
colly.IgnoreRobotsTxt(),
colly.MaxDepth(100), // Should be set to ceil(nAchievements / 50) + 1
colly.Async(),
)
achievementSelectors := s.getProfileSelectors().Achievements
c.OnHTML(achievementSelectors.TotalAchievements.Selector, func(e *colly.HTMLElement) {
taStr := achievementSelectors.TotalAchievements.Parse(e)[0]
ta, err := strconv.ParseUint(taStr, 10, 32)
if err == nil {
aai.TotalAchievements = uint32(ta)
}
})
c.OnHTML(achievementSelectors.AchievementPoints.Selector, func(e *colly.HTMLElement) {
apStr := achievementSelectors.AchievementPoints.Parse(e)[0]
ap, err := strconv.ParseUint(apStr, 10, 32)
if err == nil {
aai.TotalAchievementPoints = uint32(ap)
}
})
c.OnHTML(achievementSelectors.Root.Selector, func(e1 *colly.HTMLElement) {
nextURI := achievementSelectors.ListNextButton.ParseThroughChildren(e1)[0]
entrySelectors := achievementSelectors.Entry
e1.ForEach(entrySelectors.Root.Selector, func(i int, e2 *colly.HTMLElement) {
nameOptions := entrySelectors.Name.ParseThroughChildren(e2)
name := nameOptions[0]
if name == "" {
name = nameOptions[1]
}
nextAchievement := &AchievementInfo{
NamedEntity: &models.NamedEntity{},
}
achievement, err := s.dataProvider.Achievement(name)
if err == nil {
nextAchievement.NamedEntity = achievement
}
idStr := entrySelectors.ID.ParseThroughChildren(e2)[0]
id, err := strconv.ParseUint(idStr, 10, 32)
if err == nil {
nextAchievement.ID = uint32(id)
}
datetimeSecondsStr := entrySelectors.Time.ParseThroughChildren(e2)[0]
datetimeSeconds, err := strconv.ParseInt(datetimeSecondsStr, 10, 32)
if err == nil {
nextAchievement.Date = time.Unix(0, datetimeSeconds*1000*int64(time.Millisecond))
}
output <- nextAchievement
})
if nextURI != "javascript:void(0);" {
err := e1.Request.Visit(nextURI)
if err != nil {
errors <- err
}
}
})
return c
}