-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (57 loc) · 1.32 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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
)
var LIST_URL = "https://v.firebog.net/hosts/lists.php?type=tick"
func readReader(r io.ReadCloser) []string {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanRunes)
var buf bytes.Buffer
for scanner.Scan() {
buf.WriteString(scanner.Text())
}
listStr := buf.String()
urlSlice := strings.Split(listStr, "\n")
return urlSlice
}
func readUrl(url string) []string {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
fmt.Println("fetch complete", url)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("ioutils failed, trying to read from buffer")
urlSlice2 := readReader(resp.Body)
return urlSlice2
}
listStr := string(body)
urlSlice := strings.Split(listStr, "\n")
fmt.Println("parsed", url)
return urlSlice
}
func main() {
// UploadToGithub("this is me")
urlSlice := readUrl(LIST_URL)
urlSlice = urlSlice[:len(urlSlice)-1] // removes last empty line
var urls []string
for _, url := range urlSlice {
fmt.Println("fetching", url)
blockedUrls := readUrl(url)
urls = append(urls, blockedUrls...)
}
fmt.Println("all urls are fetched")
urlsJoined := strings.Join(urls, "\n")
if !IsBlockListSame(urlsJoined) {
UploadToGithub(urlsJoined)
}
}