-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlextractor.go
More file actions
67 lines (59 loc) · 1.21 KB
/
Copy pathurlextractor.go
File metadata and controls
67 lines (59 loc) · 1.21 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
60
61
62
63
64
65
66
67
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
)
const chunkSize = 50000
func main() {
if len(os.Args) != 2 {
fmt.Println("Please call this program with a single argument: the path to a file.")
return
}
f, err := os.Open(os.Args[1])
if err != nil {
fmt.Println(err.Error())
return
}
r := bufio.NewReader(f)
b := make([]byte, chunkSize)
t := make([]byte, chunkSize * 2)
urlDetector := regexp.MustCompile(
"(?:http|ftp|https)://[\\w_-]+(?:(?:\\.[\\w_-]+)+)(?:[\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?")
urls := make([]string, 0)
for {
n, err := r.Read(b)
if n > 0 {
t = append(t[chunkSize:], b...)
//fmt.Println(string(t))
urls = append(urls, urlDetector.FindAllString(string(t), -1)...)
}
if err != nil {
if err != io.EOF {
fmt.Println(err.Error())
return
}
break
}
}
urls = sliceUniqMap(urls)
for _, v := range urls {
fmt.Println(v)
}
}
//https://www.reddit.com/r/golang/comments/5ia523/idiomatic_way_to_remove_duplicates_in_a_slice/db6qa2e/
func sliceUniqMap(s []string) []string {
seen := make(map[string]struct{}, len(s))
j := 0
for _, v := range s {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
s[j] = v
j++
}
return s[:j]
}