-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (145 loc) · 5.55 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"flag"
"fmt"
"github.com/JosePedroDias/anaconda" // ChimeraCoder JosePedroDias
"github.com/JosePedroDias/follow_your_idol/config"
"github.com/JosePedroDias/follow_your_idol/persistence"
"github.com/JosePedroDias/follow_your_idol/print"
"github.com/JosePedroDias/follow_your_idol/twitter"
"time"
)
const ( // Mon Jan 2 15:04:05 MST 2006
HHMMSS = "15:04:05"
)
func main() {
// 0) READ CONFIG AND PREPARE MODULES
tCfg, err := config.TwitterFromFile("twitter_config.json")
if err != nil {
panic(err)
}
pCfg, err := config.PostgresqlFromFile("postgresql_config.json")
if err != nil {
panic(err)
}
persistence.Setup(pCfg)
twitter.Setup(tCfg)
// https://golang.org/pkg/flag/
// https://gobyexample.com/command-line-flags
cmdTimeline := flag.String("timeline", "", `command. fetches user timeline tweets using the twitter API. Accepts screenName ex:"Bhaenow"`)
cmdSearch := flag.String("search", "", `command. searches tweets using the twitter API. Accepts query string. ex:"from:Bhaenow"`)
cmdQuota := flag.Bool("quota", false, `command. returns relevant quotas using the twitter API. Does not accept argument.`)
cmdDbStats := flag.Bool("db_stats", false, `command. returns database status. Does not accept argument.`)
cmdGetTweet := flag.String("get_tweet", "", `command. returns cached tweet from the database. Accepts twitterId. ex:"592717057994686464"`)
cmdGetUser := flag.String("get_user", "", `command. returns cached twitter user from the database. Accepts screenName. ex:"Bhaenow"`)
cmdFTS := flag.String("fts", "", `command. does full text search on cached tweet bodies. accepts query string. ex:"signed"`)
optMoreRecentThan := flag.String("more_recent_than", "", `option. if passed, filters only tweets more recent than given tweet id. use "auto" to use newest. ex:"592717057994686464"`)
optOlderOrEqualTo := flag.String("older_or_equal_to", "", `option. if passed, filters only tweets older or equal to given tweet id. use "auto" to use oldest. ex:"592717057994686464"`)
flag.Parse()
if false {
fmt.Println("\nParsed arguments:")
fmt.Printf("timeline [%v]\n", *cmdTimeline)
fmt.Printf("search [%v]\n", *cmdSearch)
fmt.Printf("quota [%v]\n", *cmdQuota)
fmt.Printf("db_stats [%v]\n", *cmdDbStats)
fmt.Printf("get_tweet [%v]\n", *cmdGetTweet)
fmt.Printf("get_user [%v]\n", *cmdGetUser)
fmt.Println("----")
fmt.Printf("more_recent_than [%v]\n", *optMoreRecentThan)
fmt.Printf("older_or_equal_to [%v]\n", *optOlderOrEqualTo)
fmt.Println("")
}
moreRecentThan := *optMoreRecentThan
olderOrEqualTo := *optOlderOrEqualTo
// fetch edge tweet_ids if auto is passed
if len(*cmdTimeline) > 0 || len(*cmdSearch) > 0 {
var screenName string
if len(*cmdTimeline) > 0 {
screenName = *cmdTimeline
} else {
screenName = *cmdSearch
}
if moreRecentThan == "auto" {
moreRecentThan, err = persistence.GetEdgeTweetsForUser(screenName, true)
if err != nil {
panic(err)
}
}
if olderOrEqualTo == "auto" {
olderOrEqualTo, err = persistence.GetEdgeTweetsForUser(screenName, false)
if err != nil {
panic(err)
}
}
}
if *cmdQuota {
quota, err := twitter.GetQuota()
if err != nil {
panic(err)
}
utLim := quota.Resources.Statuses.UserTimeline
seLim := quota.Resources.Search.Tweets
fmt.Println(" service | quota | reset")
fmt.Println("----------+-----------+----------")
fmt.Printf(" search | %3d / %3d | %s\n", utLim.Remaining, utLim.Limit, time.Unix(utLim.Reset, 0).Format(HHMMSS))
fmt.Printf(" timeline | %3d / %3d | %s\n", seLim.Remaining, seLim.Limit, time.Unix(seLim.Reset, 0).Format(HHMMSS))
} else if *cmdDbStats {
statuses, err := persistence.GetTwitterUserStatus()
if err != nil {
panic(err)
}
fmt.Println(" user_id | screen_name | tweets | oldest | newest | oldest_id | newest_id")
fmt.Println("------------+-----------------+--------+------------+------------+--------------------+--------------------")
for _, s := range statuses {
fmt.Printf(" %-10s | %-15s | %6d | %s | %s | %s | %s\n", s.UserId, s.ScreenName, s.NumTweets, s.Oldest.Format("2006-01-02"), s.Newest.Format("2006-01-02"), s.OldestId, s.NewestId)
}
} else if len(*cmdTimeline) > 0 || len(*cmdSearch) > 0 {
var tweets []anaconda.Tweet
var err error
if len(*cmdTimeline) > 0 {
tweets, err = twitter.GetUserTimeline(*cmdTimeline, moreRecentThan, olderOrEqualTo)
} else {
tweets, err = twitter.GetSearch(*cmdSearch, moreRecentThan, olderOrEqualTo)
}
if err != nil {
panic(err)
}
count := len(tweets)
stored := 0
if count > 0 {
//print.DisplayUser()
err := persistence.SaveUser(&tweets[0].User)
if err != nil {
fmt.Println("user persistence skipped")
}
}
for _, tweet := range tweets {
print.DisplayTweet(&tweet)
//print.DisplayTweetAsJSON(&tweet)
err = persistence.SaveTweet(&tweet)
if err != nil {
fmt.Println("tweet persistence skipped")
} else {
stored += 1
}
}
fmt.Printf("\n# tweets: %d / %d\n\n", stored, count)
} else if len(*cmdGetTweet) > 0 {
tweet, _ := persistence.LoadTweet(*cmdGetTweet)
print.DisplayTweet(tweet)
} else if len(*cmdGetUser) > 0 {
user, _ := persistence.LoadUser(*cmdGetUser)
print.DisplayUser(user)
} else if len(*cmdFTS) > 0 {
results, err := persistence.FullTextSearchTweets(*cmdFTS)
if err != nil {
panic(err)
}
for _, r := range results {
fmt.Printf("%s | %s | %s\n", r.UserId, r.TweetId, r.Body)
}
fmt.Printf("# results: %d\n", len(results))
} else {
fmt.Println("No command passed - nothing to do. Use -help to learn the command-line API.")
}
}