-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterdata.go
63 lines (51 loc) · 1.43 KB
/
twitterdata.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
package main
import (
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/ChimeraCoder/anaconda"
Conf "github.com/mattga/SocialData/Config"
)
func main() {
anaconda.SetConsumerKey(<twitter key>)
anaconda.SetConsumerSecret(<twitter secret>)
api := anaconda.NewTwitterApi(<>,<>)
api.EnableThrottling(15*time.Minute/180, 10000) // 180 Queries / 15 Min
for _, hashtag := range Conf.Hashtags {
v := url.Values{}
v.Set("count", "100")
v.Set("include_entities", "0")
v.Set("lang", "en")
v.Set("since", "2015-11-16")
result, err := api.GetSearch(fmt.Sprintf("%%23%s -filter:retweets", hashtag), v)
if err != nil {
fmt.Println(err)
}
count := result.Metadata.Count
fmt.Printf("New query for tweets with #%s (%d) \n", hashtag, count)
f, err := os.OpenFile(fmt.Sprintf("data/%s.txt", hashtag), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
for k := 2; ; k++ {
for _, tweet := range result.Statuses {
text := strings.Replace(tweet.Text, "\n", " ", -1)
data := fmt.Sprintf("%s, %s, %s\n", tweet.IdStr, tweet.CreatedAt, text)
if _, err = f.WriteString(data); err != nil {
panic(err)
}
}
if result.Metadata.NextResults == "" {
break
}
if result, err = result.GetNext(api); err != nil {
panic(err)
}
count = count + result.Metadata.Count
fmt.Printf("\tFetched tweets up to id %s (%d)\n", result.Metadata.MaxIdString, count)
}
f.Close()
}
}