/
jbbs2twitter.go
53 lines (49 loc) · 1.73 KB
/
jbbs2twitter.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
package main
import (
"flag"
"github.com/ChimeraCoder/anaconda"
"github.com/mogepiyo/jbbsreader"
"log"
"time"
)
var (
category = flag.String("category", "game", "your JBBS board category")
banchi = flag.String("banchi", "9358", "your JBBS board banchi")
apiKey = flag.String("api_key", "", "your api key")
apiSecret = flag.String("api_secret", "", "your api secret")
accessToken = flag.String("access_token", "", "your access token")
accessTokenSecret = flag.String("access_token_secret", "", "your access token secret")
errorRetryDurationMins = flag.Int("error_retry_duration_mins", 10, "how many minutes to wait after an error")
jbbsRPM = flag.Int("jbbs_rpm", 1, "how many request per minutes to send to JBBS")
)
func main() {
flag.Parse()
log.Println("Generating Twitter Client")
anaconda.SetConsumerKey(*apiKey)
anaconda.SetConsumerSecret(*apiSecret)
api := anaconda.NewTwitterApi(*accessToken, *accessTokenSecret)
log.Println("Generating JBBS Board Client")
board := jbbsreader.NewBoard(*category, *banchi)
jbbsreader.SetGlobalRateLimitRPM(*jbbsRPM, *jbbsRPM)
log.Println("Feeding JBBS to Twitter!")
for ; ; <-time.Tick(time.Duration(*errorRetryDurationMins) * time.Minute) {
err := feedJBBS2Twitter(board, api)
if err != nil {
log.Println(err.Error())
}
}
}
func feedJBBS2Twitter(board *jbbsreader.Board, api *anaconda.TwitterApi) error {
done := make(chan struct{})
defer close(done)
respc, errc := board.FeedNewResponses(done)
for resp := range respc {
log.Printf("Fetched %#v\n", resp)
tweet, err := api.PostTweet(resp.Content, nil)
if err != nil {
return err
}
log.Printf("Sent tweet %#v\n", tweet)
}
return <-errc
}