-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (126 loc) · 3.73 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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/jinzhu/now"
"github.com/kurrik/oauth1a"
"github.com/kurrik/twittergo"
)
const (
// BANNER ...
BANNER = `
go-horoscope - Get your horoscope via the @poetastrologers twitter account
`
)
var (
twitterConsumerKey string
twitterConsumerSecret string
twitterAccount string
numTweets string
tweet []byte
astroSign string
)
func init() {
signs := []string{"Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"}
// declare flags for consumer key & secret (hint: register for a developer account at Twitter to receive keys)
flag.StringVar(&twitterConsumerKey, "consumer-key", os.Getenv("CONSUMER_KEY"), "Twitter consumer key")
flag.StringVar(&twitterConsumerSecret, "consumer-secret", os.Getenv("CONSUMER_KEY"), "Twitter consumer secret")
// print usage information when -h or -help flag is invoked
flag.Usage = func() {
fmt.Fprintf(os.Stderr, fmt.Sprintf(BANNER))
fmt.Println()
fmt.Println("Usage: go-horoscope [options] <sign>")
fmt.Println()
flag.PrintDefaults()
}
// parse flags
flag.Parse()
// check consumer key
if twitterConsumerKey == "" {
if twitterConsumerKey = os.Getenv("CONSUMER_KEY"); twitterConsumerKey == "" {
flag.PrintDefaults()
os.Exit(1)
}
}
// check consumer secret
if twitterConsumerSecret == "" {
if twitterConsumerSecret = os.Getenv("CONSUMER_SECRET"); twitterConsumerSecret == "" {
flag.PrintDefaults()
os.Exit(1)
}
}
// get argumemnt
initialArgument := flag.Args()[0]
// validate user input
for _, value := range signs {
if strings.EqualFold(initialArgument, value) == true {
astroSign = initialArgument
break
}
}
if astroSign == "" {
fmt.Println("Enter a valid astrological sign.")
os.Exit(1)
}
}
func main() {
// we'll hardcode the twitter account to pull from (for now) and the number of tweets to query
twitterAccount = "poetastrologers"
numTweets = "100"
// we use jinzhu's Now library to calcuate the beginning of the week (i.e. when @poetastrologers post their weekly horoscopes)
current := now.BeginningOfWeek()
month := current.Month()
day := current.Day()
// join strings from slice
stringAr := []string{"Week of ", strconv.Itoa(int(month)), "/", strconv.Itoa(day), " in ", astroSign}
dateStr := strings.Join(stringAr, "")
// create config
config := &oauth1a.ClientConfig{
ConsumerKey: twitterConsumerKey,
ConsumerSecret: twitterConsumerSecret,
}
// create client
client := twittergo.NewClient(config, nil)
if err := client.FetchAppToken(); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't fetch app token: %v\n", err)
os.Exit(2)
}
// set url
value := url.Values{}
value.Set("count", numTweets)
value.Set("screen_name", twitterAccount)
request, err := http.NewRequest("GET", "/1.1/statuses/user_timeline.json?"+value.Encode(), nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't parse request: %v\n", err)
os.Exit(2)
}
// send request
response, err := client.SendRequest(request)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't send request: %v\n", err)
os.Exit(2)
}
// parse response
results := &twittergo.Timeline{}
if err := response.Parse(results); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't parse response: %v\n", err)
os.Exit(2)
}
// encode json from response
for _, value := range *results {
if tweet, err = json.Marshal(*results); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't encode tweets: %v\n", err)
os.Exit(2)
}
// compare string from tweet.Text() to date, print resulting tweet
if (strings.Contains(value.Text(), dateStr)) == true {
fmt.Println(value.Text())
}
}
}