forked from rapidloop/mybot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.go
100 lines (92 loc) · 2.63 KB
/
weather.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
package main
import (
"encoding/json"
"database/sql"
"golang.org/x/net/websocket"
"fmt"
"net/url"
"net/http"
"log"
)
type WeatherInfo struct {
Query struct {
Count int `json:"count"`
Created string `json:"created"`
Lang string `json:"lang"`
Results struct {
Channel struct {
Item struct {
Condition struct {
Code string `json:"code"`
Date string `json:"date"`
Temp string `json:"temp"`
Text string `json:"text"`
} `json:"condition"`
Title string `json:"title"`
}
} `json:"channel"`
} `json:"results"`
} `json:"query"`
}
// Fetches Weather information from the database or Yahoo!'s YQL api.
func weather(config Config, db *sql.DB, ws *websocket.Conn, user string, channel string, city string) {
var m Message
m.Type = "message"
m.Channel = channel
// Read from database
rows, err := db.Query(fmt.Sprintf("SELECT title, text, temp FROM weather WHERE city='%s'", city))
if err != nil {
log.Print(err)
m.Text = fmt.Sprintf("<@%s>: %s", user, err)
postMessage(ws, m)
return
}
defer rows.Close()
if rows.Next() {
// Return cached data
var title, text, temp string
rows.Scan(&title, &text, &temp)
m.Text = fmt.Sprintf("<@%s>: The weather in %s is %s, %sF", user, title, text, temp)
postMessage(ws, m)
return
}
// Query Yahoo's API
query := fmt.Sprintf("select item.yweather:condition,item.title from weather.forecast where woeid in (" +
"select woeid from geo.places(1) where text=\"%s\")", city)
url := fmt.Sprintf("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", url.QueryEscape(query))
resp, err := http.Get(url)
if err != nil {
log.Print(err)
m.Text = fmt.Sprintf("<@%s>: %s", user, err)
postMessage(ws, m)
return
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
info := WeatherInfo{}
err = decoder.Decode(&info)
if err != nil {
log.Print(err)
m.Text = fmt.Sprintf("<@%s>: %s", user, err)
postMessage(ws, m)
return
}
// Insert result into database
title := info.Query.Results.Channel.Item.Title
text := info.Query.Results.Channel.Item.Condition.Text
temp := info.Query.Results.Channel.Item.Condition.Temp
_, err = db.Exec(fmt.Sprintf("INSERT INTO weather SET city='%s', title='%s', text='%s', temp='%s'",
city,
title,
text,
temp))
if err != nil {
log.Print(err)
m.Text = fmt.Sprintf("<@%s>: %s", user, err)
postMessage(ws, m)
return
}
// Return result
m.Text = fmt.Sprintf("<@%s>: The weather in %s is %s, %sF", user, title, text, temp)
postMessage(ws, m)
}