-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch.go
91 lines (81 loc) · 1.75 KB
/
search.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
package main
import (
"fmt"
"net/http"
"net/url"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
)
type SearchResult struct {
Users []string
Weibos []string
}
func getusers(key string ) []string{
db, err := sql.Open("mysql", "root:123321@/weibo")
if err != nil {
log.Fatalf("Open database error: %s\n", err)
}
defer db.Close()
words := fmt.Sprintf("select userid from user where name like '%%%s%%'", key)
rows, err := db.Query(words)
if err != nil {
log.Println(err)
}
defer rows.Close()
var id string
var users []string
for rows.Next() {
err := rows.Scan(&id)
if err != nil {
log.Fatal(err)
}
users = append(users, id)
}
return users
}
func getweibos(key string) []string{
db, err := sql.Open("mysql", "root:123321@/weibo")
if err != nil {
log.Fatalf("Open database error: %s\n", err)
}
defer db.Close()
words := fmt.Sprintf("select weiboid from weibo where msg like '%%%s%%'", key)
rows, err := db.Query(words)
if err != nil {
log.Println(err)
}
defer rows.Close()
var id string
var weibos[]string
for rows.Next() {
err := rows.Scan(&id)
if err != nil {
log.Fatal(err)
}
weibos = append(weibos, id)
}
return weibos
}
func search(key string) SearchResult {
var result SearchResult
result.Users = getusers(key)
result.Weibos = getweibos(key)
return result
}
func syncweibo(weiboid string) {
URL := "http://127.0.0.1:6666/syncweibo?"
value := url.Values{}
value.Set("weiboid", weiboid)
url := URL + value.Encode()
res, err := http.Get(url)
if err != nil {
}
res.Body.Close()
}
/*
func main(){
fmt.Println(search("测试"))
//syncweibo("3230")
fmt.Println(search("毒"))
}*/