-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (95 loc) · 3.06 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"flag"
"fmt"
"os"
"strings"
"tweety/classifier"
"tweety/twitter"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
)
func getTwitterAuthConfig() twitter.AuthConfig {
tckey := os.Getenv("TWITTER_CONSUMER_KEY")
tcsec := os.Getenv("TWITTER_CONSUMER_SECRET")
tatoken := os.Getenv("TWITTER_ACCESS_TOKEN")
tatokensecret := os.Getenv("TWITTER_ACCESS_TOKEN_SECRET")
return twitter.AuthConfig{
ConsumerKey: tckey,
ConsumerSecret: tcsec,
AccessToken: tatoken,
AccessTokenSecret: tatokensecret,
}
}
func classificationText(string string) {
}
func getFilterConfig(textFilter, locationFilter, userFilter *string) twitter.StreamFilter {
// likeOnTweet := flag.Bool("likeOnTweet", false, "Like on Tweet")
// retweetOnHashTagMention := flag.Bool("retweetOnTweet", false, "Like on Tweet")
// commentOnHashTagMention := flag.Bool("commentOnTweet", false, "Like on Tweet")
textFilterList := strings.Split((*textFilter), "|")
if len(*textFilter) == 0 {
textFilterList = []string{}
}
locationFilterList := strings.Split((*locationFilter), "|")
if len(*locationFilter) == 0 {
locationFilterList = []string{}
}
userFilterList := strings.Split((*userFilter), "|")
if len(*userFilter) == 0 {
userFilterList = []string{}
}
return twitter.StreamFilter{
TextKeywords: textFilterList,
Locations: locationFilterList,
UserIds: userFilterList,
}
}
type Action string
const (
RETWEET Action = "RETWEET"
)
type Rule interface {
evaluate(twitter.Tweet, classifier.Classification) bool
action() Action
}
type FirstStrategy struct{}
func getSentiment(score float64) string {
if score > 0.5 {
return "😀"
}
return "😟"
}
func printTweet(tweet *twitter.Tweet) {
cyan := color.New(color.FgCyan).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
fmt.Printf("\n%s\n@%s %s %s\n%s\n", blue("-----------"), cyan(tweet.User.ScreenName), yellow("::"), tweet.FullText, blue("-----------"))
}
func printSentiment(sentiment string) {
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Printf("%s\n", yellow("Sentiment ", sentiment))
}
func main() {
logrus.Info("Starting Twitter Sevice ...\n")
classify := flag.Bool("classify", false, "Hash tag list to filter by")
machineBoxURL := flag.String("mbHost", "http://localhost:8080", "Machine Box Url")
service := twitter.Service(getTwitterAuthConfig())
textFilter := flag.String("textFilter", "", "Hash tag list to filter by")
locationFilter := flag.String("locationFilter", "", "Location list to filter by")
userFilter := flag.String("userFilter", "", "UserIds list to filter by")
flag.Parse()
service.FilteredStream(getFilterConfig(textFilter, locationFilter, userFilter), func(tweet *twitter.Tweet) {
printTweet(tweet)
if *classify {
var cls classifier.Service
cls = &classifier.MachineBox{HostPort: *machineBoxURL}
classification, err := cls.Classify(tweet.FullText)
if err == nil {
sentiment := getSentiment(classification.SentimentScore)
printSentiment(sentiment)
}
}
})
logrus.Info("Service Stopped\n")
}