-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
193 lines (165 loc) · 4.7 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"math"
"net/http"
"net/url"
"os"
"strings"
"text/template"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/odeke-em/nasa"
)
var (
roverClient *nasa.Client
twitterAPI *anaconda.TwitterApi
)
var (
twitterConsumerKey = envValueAtInit("MARS_ROVER_TWITTER_CONSUMER_KEY")
twitterConsumerSecret = envValueAtInit("MARS_ROVER_TWITTER_CONSUMER_SECRET")
twitterAccessToken = envValueAtInit("MARS_ROVER_TWITTER_ACCESS_TOKEN")
twitterAccessSecret = envValueAtInit("MARS_ROVER_TWITTER_ACCESS_SECRET")
)
var initErrMsgList = []string{}
func envValueAtInit(key string) string {
value := os.Getenv(key)
if value == "" {
initErrMsgList = append(initErrMsgList, fmt.Sprintf("%q is not defined", key))
}
return value
}
func exitOnError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(-1)
}
}
func init() {
var err error
roverClient, err = nasa.New()
if err != nil {
exitOnError(err)
}
roverClient.SetAPIKey(os.Getenv("NASA_API_KEY"))
if len(initErrMsgList) > 0 {
msg := fmt.Sprintf("Errors Encountered:\n%s", strings.Join(initErrMsgList, "\n"))
exitOnError(fmt.Errorf("%s", msg))
}
anaconda.SetConsumerKey(twitterConsumerKey)
anaconda.SetConsumerSecret(twitterConsumerSecret)
twitterAPI = anaconda.NewTwitterApi(twitterAccessToken, twitterAccessSecret)
}
func tweetPhotos(offsetInHours uint) error {
offsetTime := time.Now().Add(-1 * time.Duration(offsetInHours) * time.Hour)
startTime := &offsetTime
marsPhotos, err := roverClient.MarsPhotos(startTime)
if err != nil {
return err
}
photos := marsPhotos.Photos
if len(photos) < 1 {
return fmt.Errorf("no photos for %q", startTime)
}
// Distribute the photos over a 24 hour period
rawHourlyDistribution := 24.0 / float32(len(photos))
hours := int(rawHourlyDistribution)
minutes := int(math.Ceil(60 * float64(rawHourlyDistribution-float32(hours))))
pausePeriod := (time.Duration(hours) * time.Hour) + (time.Duration(minutes) * time.Minute)
ticker := time.Tick(pausePeriod)
log.Printf("Total Photos: %d PausePeriod: %s\n", len(photos), pausePeriod)
for i := 0; i < len(photos); i++ {
// tweet the photo
photo := photos[i]
log.Printf("#%d: About to tweet %#v\n", i, photo)
select {
case err := <-tweetIt(photo, pausePeriod):
log.Printf("#%d: After tweeting err=%v\n", i, err)
case <-ticker:
log.Printf("#%d: Timedout", i)
}
}
return nil
}
func tweetIt(mphoto *nasa.MarsPhoto, period time.Duration) chan error {
resultChan := make(chan error)
go func() {
defer close(resultChan)
startTime := time.Now()
endTime := startTime.Add(period)
err := tweetPhoto(mphoto)
now := time.Now()
if endTime.After(now) {
<-time.Tick(endTime.Sub(now))
}
resultChan <- err
}()
return resultChan
}
const tweetMsg = `Taken by camera {{.Camera.ShortName}}, on {{.EarthDate}} by Rover {{.Rover.Name}} launched on {{.Rover.LaunchDate}}. PhotoID: {{.Id}}`
var tweetTmpl = template.Must(template.New("marsPhotoTweet").Parse(tweetMsg))
func composeTweet(mphoto *nasa.MarsPhoto) (string, error) {
buf := new(bytes.Buffer)
if err := tweetTmpl.Execute(buf, mphoto); err != nil {
return "", err
}
return string(buf.Bytes()), nil
}
func tweetPhoto(mphoto *nasa.MarsPhoto) error {
if mphoto.ImageURL == "" {
return fmt.Errorf("expecting an image URL")
}
statusMsg, err := composeTweet(mphoto)
log.Printf("composed tweet %q err=%v\n", statusMsg, err)
if err != nil {
return err
}
base64OfImage, err := imageBase64(mphoto.ImageURL)
log.Printf("making base64OfImage %s err=%v\n", mphoto.ImageURL, err)
if err != nil {
return err
}
media, err := twitterAPI.UploadMedia(base64OfImage)
if err != nil {
return err
}
// For sending the actual tweet:
// Thanks to http://stackoverflow.com/questions/30992532/go-anaconda-twitter-media-upload-with-tweet
tw := url.Values{}
tw.Set("media_ids", fmt.Sprintf("%v", media.MediaID))
result, err := twitterAPI.PostTweet(statusMsg, tw)
log.Printf("result: %v err: %v\n", result, err)
return err
}
type tweet struct {
MediaId uint64 `json:"media_ids"`
Status string `json:"status"`
}
func imageBase64(uri string) (string, error) {
res, err := http.Get(uri)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode > 299 {
return "", fmt.Errorf("%s", res.Status)
}
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, res.Body); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
func main() {
var offsetInHours uint
flag.UintVar(&offsetInHours, "offset-in-hours", 24, "the number of hours offset from right now from which we should retrieve photos")
flag.Parse()
for {
tweetPhotos(offsetInHours)
}
}