summaryrefslogtreecommitdiff
path: root/helper.go
blob: 866ec5a642b0a23641d3bf631295d8a36b11d35c (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main

import (
	_url "net/url"
	"strings"
	log "github.com/sirupsen/logrus"
	"regexp"
	xhtml "golang.org/x/net/html"
)

func stripHNPrefix(title string) string {
	title = strings.TrimPrefix(title, "Ask HN:")
	title = strings.TrimPrefix(title, "Show HN:")
	title = strings.TrimPrefix(title, "Tell HN:")
	title = strings.TrimPrefix(title, "Experiment HN:")
	title = strings.TrimPrefix(title, "Launch HN:")

	return strings.TrimSpace(title)
}

/**
 * removes given param from URL
 */
func _removeParam(url, key string) string {
	u, err := _url.Parse(url)
	if err != nil {
		log.Fatal(err)
	}
	q := u.Query()
	q.Del(key)
	u.RawQuery  = q.Encode()
	return u.String()
}

func normalizeUrl(url string) string {

	/**
	 * Redirect http:// to https://
	 */
	match, err := regexp.MatchString("^http://", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		log.Debug("normalize: ", "http:// ", url)
		r := regexp.MustCompile("^http://")
		url = r.ReplaceAllString(url, "https://")
	}

	/**
	 * add missing https:// if no scheme
	 * Fun fact: https://news.ycombinator.com/item?id=27351340 broke this part
	 */
	u, err := _url.Parse(url)
	if err != nil {
		log.Fatal(err)
	}

	if "" == u.Scheme {
		if strings.HasPrefix(url, "/") {
			url = "https:" + url
		} else {
			url = "https://" + url
		}
	}

	/**
	 * Apple TV accepts youtube:// scheme
	 */
	match, err = regexp.MatchString("youtube://", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		r := regexp.MustCompile("youtube://")
		url = r.ReplaceAllString(url, "https://")
	}

	/**
	 * Redirect youtu.be to desktop version
	 */
	match, err = regexp.MatchString("youtu.be/", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		log.Debug("normalize: ", "youtu.be ", url)

		u, err := _url.Parse(url)
		if err != nil {
			log.Fatal(err)
		}
		q := u.Query()
		q.Add("v", strings.TrimLeft(u.Path, "/"))

		u.Host = "www.youtube.com"
		u.Path = "watch"

		u.RawQuery = q.Encode()
		url = u.String()
		
		//r := regexp.MustCompile("youtu.be/")
		//url = r.ReplaceAllString(url, "youtube.com/watch?v=")
	}

	/**
	 * Redirect m.youtube.com to desktop version
	 */
	match, err = regexp.MatchString("/m.youtube.com/", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		log.Debug("normalize: ", "m.youtube.com ", url)

		/**
		 * remove tracking param "si"
		 */
		url = _removeParam(url, "si")

		r := regexp.MustCompile("/m.youtube.com/")
		url = r.ReplaceAllString(url, "/www.youtube.com/")
	}

	/**
	 * Redirect m.imdb.com to desktop version
	 */
	match, err = regexp.MatchString("/m.imdb.com/", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		log.Debug("normalize: ", "m.imdb.com ", url)

		r := regexp.MustCompile("/m.imdb.com/")
		url = r.ReplaceAllString(url, "/www.imdb.com")
	}

	/**
	 * Append www. to normalize URL. exclude relative URLs starting with // since this is not recognized by Go
	 * Screw that, wierd edge case. Someone pasted a
	 */
	u, err = _url.Parse(url)
	if err != nil {
		log.Fatal(err)
	}

	if ! strings.HasPrefix(u.Host, "www.") {
		u.Host = "www." + u.Host
	}

	url = u.String()

	/**
	 * Redirects youtube.com/c/<name> to youtube.com/@<name>
	 */
	match, err = regexp.MatchString("/.youtube.com/c/", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		log.Debug("normalize: ", "youtube.com/c/ -> @ ", url)

		/**
		 * remove tracking param "si"
		 */
		url = _removeParam(url, "si")

		r := regexp.MustCompile("youtube.com/c/")
		url = r.ReplaceAllString(url, "youtube.com/@")
	}

	/**
	 * remove tracking param "si", "feature" and "pp" from every youtube video
	 */
	match, err = regexp.MatchString("/www.youtube.com/", url)
	if err != nil {
		log.Fatal(err)
	}
	if match {
		url = _removeParam(url, "si")
		url = _removeParam(url, "pp")
		url = _removeParam(url, "feature")
	}

	/**
	 * remove tracking utm_ params
	 */
	url = _removeParam(url, "utm_source")
	url = _removeParam(url, "utm_medium")
	url = _removeParam(url, "utm_campaign")
	url = _removeParam(url, "utm_term")
	url = _removeParam(url, "utm_content")

	return url
}

func RemoveNode(root_node *xhtml.Node, remove_me *xhtml.Node) {
    found_node := false
    check_nodes := make(map[int]*xhtml.Node)
    i := 0

    // loop through siblings
    for n := root_node.FirstChild; n != nil; n = n.NextSibling {
	if n == remove_me {
	    found_node = true
	    n.Parent.RemoveChild(n)
	}

	check_nodes[i] = n
	i++
    }

    // check if removing node is found
    // if yes no need to check childs returning
    // if no continue loop through childs and so on
    if found_node == false {
	for _, item := range check_nodes {
	    RemoveNode(item, remove_me)
	}
    }
}