-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreddit_parser.go
More file actions
59 lines (44 loc) · 1.53 KB
/
reddit_parser.go
File metadata and controls
59 lines (44 loc) · 1.53 KB
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
package sauron
import (
"github.com/PuerkitoBio/goquery"
"net/url"
"strconv"
)
// Reddit is our internal Reddit parser
// This parser will get page information as well as Reddit post information such as dislikes, likes, and overall score
func Reddit(doc *goquery.Document, url *url.URL, fullURL string) (link *Link, parserErr error) {
link, parserErr = Primitive(doc, url, fullURL) // First get our link information from Primitive
link.Extras["IsRedditLink"] = "true" // Indicate it is a Reddit link
dislikes := doc.Find(".unvoted > .dislikes").Text()
likes := doc.Find(".unvoted > .likes").Text()
scoreStr := doc.Find(".unvoted > .unvoted").Text()
link.Extras["Dislikes"] = dislikes
link.Extras["Likes"] = likes
link.Extras["Score"] = scoreStr
// #region Percentage Calculation
if dislikes != "" && likes != "" && scoreStr != "" {
var score int
if score, parserErr = strconv.Atoi(scoreStr); parserErr != nil { // Failed to parse our score
return
}
if score != 0 { // Have a score
var downvotes int
if downvotes, parserErr = strconv.Atoi(dislikes); parserErr != nil {
return
}
var upvotes int
if upvotes, parserErr = strconv.Atoi(likes); parserErr != nil {
return
}
percentage := int((float64(downvotes) / float64(upvotes)) * 100)
if percentage == 0 { // 100% upvote
percentage = 100
}
link.Extras["Percentage"] = strconv.Itoa(percentage) // Convert our percentage to a string
} else { // Score of 0
link.Extras["Percentage"] = "0" // Set to 0
}
}
// #endregion
return
}