-
Notifications
You must be signed in to change notification settings - Fork 0
/
godensity.go
183 lines (154 loc) · 4.98 KB
/
godensity.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package godensity
import (
"bytes"
"io"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// ComputeTextDensity calculates the text density of a node
func ComputeTextDensity(node *Node) float32 {
if node.T == 0 {
return 0
}
return float32(len(node.text)) / node.T
}
// ComputeDensity calculates the overall density of a node
func ComputeDensity(node *Node) float32 {
textDensity := ComputeTextDensity(node)
imageDensity := float32(len(node.images)) * 1000 // Assuming each image contributes 1000 to density
videoDensity := float32(len(node.videos)) * 2000 // Assuming each video contributes 2000 to density
return textDensity + imageDensity + videoDensity
}
// CalculateDensitySum computes the sum of densities for a node and its children
func CalculateDensitySum(node *Node) float32 {
if node == nil {
return 0
}
return node.density + node.densitySum
}
// Filtering removes unnecessary HTML elements from the page's body to simplify the DOM analysis.
func Filtering(body *goquery.Selection) {
// Remove common interactive and irrelevant elements from the DOM
toRemove := []string{
"script", "style", "br", "input", "button", "textarea", "label", "form", "iframe",
"header", "nav", ".comment", "footer", ".footer", "#footer",
}
// Iterate over the list and remove each element
for _, selector := range toRemove {
body.Find(selector).Remove()
}
}
// IsGIF checks if the provided URL points to a GIF image by examining its content
func IsGIF(src *url.URL, currentURL *url.URL) bool {
target := src.String()
if !src.IsAbs() {
target = currentURL.ResolveReference(src).String()
}
res, err := http.Get(target)
if err != nil {
log.Printf("Error fetching URL: %v, Source: %v, Target: %v", err, src, target)
return false
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Printf("Error reading response body: %v", err)
return false
}
// Check GIF magic number in first 4 bytes (GIF87a or GIF89a)
if bytes.HasPrefix(body, []byte{71, 73, 70, 56}) {
log.Printf("GIF detected! URL: %s", target)
return true
}
return false
}
// diveIntoDOM traverses the DOM structure recursively and calculates density metrics
func diveIntoDOM(selection *goquery.Selection, domain string) *Node {
// Base case: no children, calculate density for text or images
if selection.Children().Length() == 0 {
return createLeafNode(selection, domain)
}
// Recursive case: process children and calculate density metrics
return createInternalNode(selection, domain)
}
// createLeafNode creates a Node for elements without children
func createLeafNode(selection *goquery.Selection, domain string) *Node {
re := regexp.MustCompile(`\s+`)
textContent := re.ReplaceAllString(strings.TrimSpace(selection.Text()), " ")
var density float32
// Check if it's an image tag
if goquery.NodeName(selection) == "img" {
if src, exists := selection.Attr("src"); exists {
parsedSrc, _ := url.Parse(src)
parsedDomain, _ := url.Parse(domain)
if IsGIF(parsedSrc, parsedDomain) {
density = 1
}
} else {
log.Println("Image tag found with no src attribute")
}
}
return &Node{
goqueryNode: selection,
density: density,
densitySum: 0,
images: getMediaSources(selection, "img"),
videos: getMediaSources(selection, "video > source"),
T: 0,
text: textContent,
next: nil,
}
}
// createInternalNode creates a Node for elements with children
func createInternalNode(selection *goquery.Selection, domain string) *Node {
var (
densitySum float32
maxDensity float32 = -1
nextNode *Node
T float32
)
selection.Children().Each(func(_ int, child *goquery.Selection) {
childNode := diveIntoDOM(child, domain)
densitySum += childNode.density
T += childNode.T
if childNode.density > maxDensity {
nextNode = childNode
maxDensity = childNode.density
}
})
T += float32(selection.Children().Length())
re := regexp.MustCompile(`\s+`)
textLength := float32(len(re.ReplaceAllString(strings.TrimSpace(selection.Text()), " ")))
linkTextLength := float32(len(re.ReplaceAllString(strings.TrimSpace(selection.Find("a").Text()), " ")))
linkCount := float32(selection.Find("a").Length())
if T-linkCount <= 0 {
T = 1
linkCount = 0
}
density := (textLength - linkTextLength) / (T - linkCount)
currentNode := &Node{
goqueryNode: selection,
density: density,
densitySum: densitySum,
videos: getMediaSources(selection, "video > source"),
T: T,
text: re.ReplaceAllString(strings.TrimSpace(selection.Text()), " "),
next: nextNode,
}
// Append to the array for later use
array = append(array, *currentNode)
return currentNode
}
// getMediaSources extracts media sources from the given selection
func getMediaSources(selection *goquery.Selection, selector string) []string {
return selection.Find(selector).Map(func(_ int, s *goquery.Selection) string {
src, _ := s.Attr("src")
return src
})
}
// Global array to store Node structures
var array []Node