/
main.go
128 lines (114 loc) · 2.86 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"database/sql"
"fmt"
"github.com/PuerkitoBio/goquery"
_ "github.com/lib/pq"
"strings"
"time"
"net/http"
"crypto/md5"
"io/ioutil"
"encoding/json"
"html/template"
"strconv"
"github.com/kylelemons/go-gypsy/yaml"
)
type Pic struct {
Id string
Link string
}
func query_sources(database *sql.DB, sources []string) {
c := make(chan string)
for _, source := range sources {
go func(document string) {
for {
doc, err := goquery.NewDocument(document)
if err != nil {
fmt.Println("Goquery error: " + err.Error())
continue
}
doc.Find("body div.pi_body a.thumb_item").Each(func(i int, s *goquery.Selection) {
data, e := s.Find("img").Attr("data-src_big")
if e {
c <- strings.Split(data, "|")[0]
}
})
fmt.Println(time.Now())
time.Sleep(3 * time.Minute)
}
}(source)
}
go func() {
for link := range c {
slug := get_md5(link)
database.Exec(`INSERT INTO posts(slug, link) VALUES($1, $2) RETURNING id`, slug, link)
}
}()
}
func get_md5(link string) string {
h := md5.New()
resp, _ := http.Get(link)
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
h.Write(data)
return fmt.Sprintf("%x", h.Sum(nil))
}
func db_params() string {
data, _ := yaml.ReadFile("conf.yml")
yaml_user, _ := yaml.Child(data.Root, "user")
yaml_dbname, _ := yaml.Child(data.Root, "dbname")
yaml_password, _ := yaml.Child(data.Root, "password")
user := yaml_user.(yaml.Scalar).String()
dbname := yaml_dbname.(yaml.Scalar).String()
password := yaml_password.(yaml.Scalar).String()
db_pars := "user=" + user + " dbname=" + dbname + " password=" + password
fmt.Println(db_pars)
return db_pars
}
func main() {
page_size := 8
db, err := sql.Open("postgres", db_params())
defer db.Close()
if err != nil {
fmt.Println(err)
return
}
db.SetMaxIdleConns(4)
query_sources(db, []string{"http://vk.com/thesmolny", "http://vk.com/mdk"})
rows, err := db.Query("SELECT id, link FROM posts")
if err != nil {
fmt.Println(err)
}
for rows.Next() {
var id int
var link string
err = rows.Scan(&id, &link)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
q, e := r.URL.Query()["q"]
offset_posts := 0
pics := make([]Pic, 0)
if e {
offset_id, _ := strconv.ParseInt(q[0], 10, 32)
offset, _ := db.Query("SELECT COUNT(id) FROM posts WHERE id >= $1", offset_id)
offset.Next()
offset.Scan(&offset_posts)
offset.Close()
}
rows, _ := db.Query("SELECT id, link FROM posts ORDER BY id DESC LIMIT $1 OFFSET $2", page_size, offset_posts)
var id, link string
for rows.Next() {
err = rows.Scan(&id, &link)
pics = append(pics, Pic{id, link})
}
if e {
resp, _ := json.Marshal(pics)
fmt.Fprintf(w, string(resp))
} else {
t, _ := template.ParseFiles("index.html")
t.Execute(w, struct{Pictures []Pic}{pics})
}
})
http.ListenAndServe(":4040", nil)
}