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
/
user_details.go
93 lines (79 loc) · 2.98 KB
/
user_details.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
package user
import (
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/rl404/go-malscraper/model"
"github.com/rl404/go-malscraper/pkg/utils"
)
type detail struct {
area *goquery.Selection
cleanImg bool
data model.User
}
// GetDetails to get user detail.
func (p *parser) GetDetails(a *goquery.Selection) *model.User {
v := detail{area: a, cleanImg: p.cleanImg}
v.setUsername()
v.setImage()
v.setInfo()
v.setMoreInfo()
v.setSns()
v.setFriend()
v.setAbout()
return &v.data
}
func (d *detail) setUsername() {
user := strings.TrimSpace(d.area.Parent().Find("h1.h1 span").Text())
d.data.Username = user[:len(user)-10]
}
func (d *detail) setImage() {
imageSrc, _ := d.area.Find(".container-left .user-profile .user-image img").Attr("data-src")
d.data.Image = utils.URLCleaner(imageSrc, "image", d.cleanImg)
}
func (d *detail) setInfo() {
infoList := make(map[string]string)
infoArea := d.area.Find(".container-left .user-profile .user-status:nth-of-type(1)")
infoArea.Find("li").Each(func(i int, info *goquery.Selection) {
infoType := info.Find("span:nth-of-type(1)").Text()
infoValue := info.Find("span:nth-of-type(2)").Text()
infoList[infoType] = infoValue
})
d.data.LastOnline, _ = utils.StrToTime(infoList["Last Online"])
d.data.Gender = infoList["Gender"]
y1, m1, d1 := utils.StrToDate(infoList["Birthday"])
d.data.Birthday = model.Date{Year: y1, Month: m1, Day: d1}
d.data.Location = infoList["Location"]
d.data.JoinedDate, _ = utils.StrToTime(infoList["Joined"])
}
func (d *detail) setMoreInfo() {
infoArea := d.area.Find(".container-left .user-profile .user-status:nth-of-type(3)")
d.data.ForumPost = utils.StrToNum(infoArea.Find("li:nth-of-type(1)").Find("span:nth-of-type(2)").Text())
d.data.Review = utils.StrToNum(infoArea.Find("li:nth-of-type(2)").Find("span:nth-of-type(2)").Text())
d.data.Recommendation = utils.StrToNum(infoArea.Find("li:nth-of-type(3)").Find("span:nth-of-type(2)").Text())
d.data.BlogPost = utils.StrToNum(infoArea.Find("li:nth-of-type(4)").Find("span:nth-of-type(2)").Text())
d.data.Club = utils.StrToNum(infoArea.Find("li:nth-of-type(5)").Find("span:nth-of-type(2)").Text())
}
func (d *detail) setSns() {
snsArea := d.area.Find(".container-left .user-profile .user-profile-sns")
snsArea.Find("a").Each(func(i int, sns *goquery.Selection) {
snsClass, _ := sns.Attr("class")
if snsClass != "di-ib mb8" {
snsHref, _ := sns.Attr("href")
d.data.Sns = append(d.data.Sns, snsHref)
}
})
}
func (d *detail) setFriend() {
friendArea := d.area.Find(".container-left .user-profile .user-friends")
friendCount := friendArea.Prev().Find("a").Text()
r := regexp.MustCompile(`\(\d+\)`)
friendCount = r.FindString(friendCount)
replacer := strings.NewReplacer("(", "", ")", "")
d.data.Friend = utils.StrToNum(replacer.Replace(friendCount))
}
func (d *detail) setAbout() {
aboutArea := d.area.Find(".container-right table tr td div[class=word-break]")
aboutContent, _ := aboutArea.Html()
d.data.About = strings.TrimSpace(aboutContent)
}