-
Notifications
You must be signed in to change notification settings - Fork 23
/
twitter.go
45 lines (38 loc) · 1.15 KB
/
twitter.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
package htmlhouse
import (
"database/sql"
"fmt"
"github.com/ChimeraCoder/anaconda"
)
func tweet(app *app, houseID, title string) {
// Check for blacklisted titles
if title == "HTMLhouse" {
return
}
// Check if this has already been tweeted
var tweetID int64
err := app.db.QueryRow("SELECT tweet_id FROM tweetedhouses WHERE house_id = ?", houseID).Scan(&tweetID)
switch {
case err != nil && err != sql.ErrNoRows:
fmt.Printf("Error selecting from tweetedhouses: %v", err)
return
}
if tweetID != 0 {
return
}
// Post to Twitter
text := fmt.Sprintf("\"%s\" on #HTMLhouse - %s/%s.html #html #web #website", title, app.cfg.HostName, houseID)
anaconda.SetConsumerKey(app.cfg.TwitterConsumerKey)
anaconda.SetConsumerSecret(app.cfg.TwitterConsumerSecret)
api := anaconda.NewTwitterApi(app.cfg.TwitterToken, app.cfg.TwitterTokenSecret)
t, err := api.PostTweet(text, nil)
if err != nil {
fmt.Printf("Error posting tweet: %v", err)
}
// Mark it as "tweeted"
_, err = app.db.Exec("INSERT INTO tweetedhouses (house_id, tweet_id) VALUES (?, ?)", houseID, t.Id)
if err != nil {
fmt.Printf("Error noting house tweet status: %v", err)
return
}
}