/
yudenbot_twitter.go
95 lines (83 loc) · 2.09 KB
/
yudenbot_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
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 (
"crypto/md5"
"fmt"
"log"
"reflect"
"time"
"github.com/ChimeraCoder/anaconda"
)
var twitterAPI *anaconda.TwitterApi
var apiHash []byte
type TwitterAuth struct {
ConsumerKey string `yaml:"consumerKey" json:"consumerKey"`
ConsumerSecret string `yaml:"consumerSecret" json:"consumerSecret"`
AccessToken string `yaml:"accessToken" json:"accessToken"`
AccessSecret string `yaml:"accessSecret" json:"accessSecret"`
}
func getTwitterAPI(auth *TwitterAuth) *anaconda.TwitterApi {
// calc hash
str := fmt.Sprintf("%v", *auth)
s := md5.New()
hash := s.Sum([]byte(str))
if twitterAPI == nil || reflect.DeepEqual(apiHash, hash) == false {
// (re)Authnication
log.Println("TwitterAPI Authnication")
log.Println("new authtoken hash : ", hash)
anaconda.SetConsumerKey(auth.ConsumerKey)
anaconda.SetConsumerSecret(auth.ConsumerSecret)
twitterAPI = anaconda.NewTwitterApi(auth.AccessToken, auth.AccessSecret)
apiHash = hash
}
return twitterAPI
}
func tweet(message string, auth *TwitterAuth) (err error) {
api := getTwitterAPI(auth)
if api == nil {
log.Println("Can't Get TwitterAPI Object")
return *new(error)
}
tweet, err := api.PostTweet(message, nil)
if err != nil {
log.Println("Error while post tweet : ", err)
return err
}
log.Println("tweet success")
log.Println(tweet.Text)
return nil
}
// Schedule
type tweetSchedule struct {
Event EventData
Time time.Time
Message string
Executed bool
Hash []byte
}
type tweetSchedules []tweetSchedule
var hasher = md5.New()
func (s *tweetSchedules) append(e EventData, t time.Time, msg string) {
h := hasher.Sum([]byte(fmt.Sprintf("%v%v", e, t)))
if !s.already(h) {
*s = append(*s,
tweetSchedule{
Event: e,
Time: t,
Message: msg,
Executed: false,
Hash: h,
})
log.Printf("Schedule append : %v\n%v\n", t.In(jst), msg)
} else {
log.Printf("Schedule append skip : %v\n%v\n", t.In(jst), msg)
}
}
func (s *tweetSchedules) already(hash []byte) bool {
for _, t := range *s {
if reflect.DeepEqual(t.Hash, hash) {
return true
}
continue
}
return false
}