forked from rseymour/dreampitcher
/
dreampitcher.go
71 lines (67 loc) · 1.5 KB
/
dreampitcher.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
package main
import (
"bytes"
"code.google.com/p/gcfg"
"fmt"
"github.com/ChimeraCoder/anaconda"
"github.com/rseymour/markov"
"math/rand"
"net/url"
"regexp"
"strings"
"time"
)
func GetApi() *anaconda.TwitterApi {
type Config struct {
Api struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
}
var cfg Config
err := gcfg.ReadFileInto(&cfg, "config.gcfg")
if err != nil {
panic(err)
}
anaconda.SetConsumerKey(cfg.Api.ConsumerKey)
anaconda.SetConsumerSecret(cfg.Api.ConsumerSecret)
api := anaconda.NewTwitterApi(cfg.Api.AccessToken, cfg.Api.AccessTokenSecret)
return api
}
func GetVerb() string {
verbs := []string{"i dreamed",
"i wish",
"i hope"}
rand.Seed(time.Now().UTC().UnixNano())
return verbs[rand.Intn(len(verbs))]
}
func main() {
api := GetApi()
v := url.Values{}
v.Set("count", "200")
var sourceText bytes.Buffer
var verb string
re := regexp.MustCompile(`@.*?\W`)
for {
if sourceText.Len() > 24000 {
sourceText.Reset()
}
verb = GetVerb()
search_result, err := api.GetSearch("\""+verb+"\" -RT", v)
if err != nil {
panic(err)
}
for _, tweet := range search_result.Statuses {
sourceText.WriteString(strings.ToLower(tweet.Text))
sourceText.WriteString(" ")
}
sample := re.ReplaceAllString(sourceText.String(), " ")
for i := 0; i < 1200; i++ {
sample = strings.SplitAfterN(sample, " ", 2)[1]
fmt.Println(markov.Generate(26, 2, sample))
time.Sleep(75000 * time.Microsecond)
}
}
}