-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 1.53 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
88
89
90
91
92
93
94
95
package main
import (
"database/sql"
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
)
func main() {
sourceList := []string{
"https://v.firebog.net/hosts/lists.php?type=tick",
"https://v.firebog.net/hosts/lists.php?type=nocross",
}
var err error
defer func() {
if err != nil {
log.Fatal(err)
}
}()
if len(os.Args) < 2 {
fmt.Printf("no path provided to sqlite db")
return
}
path := os.Args[1]
var sources []string
for _, url := range sourceList {
resp, err := http.Get(url)
if err != nil {
fmt.Printf("unable to fetch adlists: %s", err)
return
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
for {
source, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("unable to parse adlists: %s", err)
return
}
if len(source) > 0 {
sources = append(sources, source[0])
}
}
}
db, err := sql.Open("sqlite3", path)
if err != nil {
fmt.Printf("unable to open sqlite db: %s", err)
}
defer db.Close()
for _, s := range sources {
query := `
INSERT OR IGNORE INTO adlist
(address)
VALUES (?)
`
stmt, err := db.Prepare(query)
if err != nil {
fmt.Printf("unable to prepare sql query: %s", err)
return
}
res, err := stmt.Exec(s)
if err != nil {
fmt.Printf("unable to execute sql query: %s", err)
return
}
if _, err := res.RowsAffected(); err != nil {
fmt.Printf("unable to check sql result: %s", err)
return
}
}
fmt.Printf("adlists updated successfully!")
}