summaryrefslogtreecommitdiff
path: root/helper.go
diff options
context:
space:
mode:
Diffstat (limited to 'helper.go')
-rw-r--r--helper.go57
1 files changed, 30 insertions, 27 deletions
diff --git a/helper.go b/helper.go
index 866ec5a..52fb003 100644
--- a/helper.go
+++ b/helper.go
@@ -2,9 +2,10 @@ package main
import (
_url "net/url"
+ "regexp"
"strings"
+
log "github.com/sirupsen/logrus"
- "regexp"
xhtml "golang.org/x/net/html"
)
@@ -24,16 +25,17 @@ func stripHNPrefix(title string) string {
func _removeParam(url, key string) string {
u, err := _url.Parse(url)
if err != nil {
- log.Fatal(err)
+ log.Fatal("_removeParam: parse: ", err)
}
q := u.Query()
q.Del(key)
- u.RawQuery = q.Encode()
+ u.RawQuery = q.Encode()
return u.String()
}
func normalizeUrl(url string) string {
+ url = strings.ToLower(url)
/**
* Redirect http:// to https://
*/
@@ -53,7 +55,8 @@ func normalizeUrl(url string) string {
*/
u, err := _url.Parse(url)
if err != nil {
- log.Fatal(err)
+ log.Warn("normalizeUrl: parse: ", err)
+ return url
}
if "" == u.Scheme {
@@ -98,7 +101,7 @@ func normalizeUrl(url string) string {
u.RawQuery = q.Encode()
url = u.String()
-
+
//r := regexp.MustCompile("youtu.be/")
//url = r.ReplaceAllString(url, "youtube.com/watch?v=")
}
@@ -142,10 +145,10 @@ func normalizeUrl(url string) string {
*/
u, err = _url.Parse(url)
if err != nil {
- log.Fatal(err)
+ log.Fatal("normalizeUrl: parse: append www: ", err)
}
- if ! strings.HasPrefix(u.Host, "www.") {
+ if !strings.HasPrefix(u.Host, "www.") && u.Host != "music.youtube.com" {
u.Host = "www." + u.Host
}
@@ -173,7 +176,7 @@ func normalizeUrl(url string) string {
/**
* remove tracking param "si", "feature" and "pp" from every youtube video
*/
- match, err = regexp.MatchString("/www.youtube.com/", url)
+ match, err = regexp.MatchString("/(www|music).youtube.com/", url)
if err != nil {
log.Fatal(err)
}
@@ -196,27 +199,27 @@ func normalizeUrl(url string) string {
}
func RemoveNode(root_node *xhtml.Node, remove_me *xhtml.Node) {
- found_node := false
- check_nodes := make(map[int]*xhtml.Node)
- i := 0
+ 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)
+ }
- // 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_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)
+ // 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)
+ }
}
- }
}