forked from Paxxs/Google-Blocklist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (75 loc) · 1.74 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"time"
)
func main() {
sub_original, err := loadSubscriptions("./config.yml")
if err != nil {
panic(err)
}
fmt.Println("original subscriptions size: ", len(sub_original))
sub := removeDuplicate(sub_original)
fmt.Println("removeDuplicated size: ", len(sub))
err = writeFile("uBlacklist_subscription.txt", sub)
if err != nil {
panic(err)
}
fmt.Println("done!")
}
func writeFile(path string, contents []string) error {
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
datewriter := bufio.NewWriter(file)
for _, lines := range contents {
datewriter.WriteString(lines + "\n")
}
datewriter.Flush()
return file.Close()
}
func readFile(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
lines, err := ioToArr(file)
return lines, err
}
func readNetworkFile(path string) ([]string, error) {
resp, err := http.Get(path)
if err != nil {
return nil, err
}
fmt.Println("response: ", resp.Status)
lines, err := ioToArr(resp.Body)
return lines, err
}
func ioToArr(r io.Reader) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func removeDuplicate(arr []string) []string {
start := time.Now()
resArray := make([]string, 0)
tmpArray := make(map[string]interface{})
for _, val := range arr {
if _, ok := tmpArray[val]; !ok {
// 不存在则创建
resArray = append(resArray, val)
tmpArray[val] = struct{}{}
}
}
fmt.Println("Removing duplicate time:", fmt.Sprintf("%vms", (time.Now().UnixNano()-start.UnixNano())/1e+6))
return resArray
}