-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (63 loc) · 2.09 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type Credentials struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
func main() {
lambda.Start(HandleRequest)
}
func HandleRequest(ctx context.Context) (string, error) {
loc, _ := time.LoadLocation("Europe/London")
now := time.Now().In(loc)
start := time.Date(2017, time.March, 29, 12, 0, 0, 0, loc)
end := time.Date(2019, time.October, 31, 23, 0, 0, 0, loc)
duration_end := end.Sub(start)
duration_now := now.Sub(start)
perc := (duration_now.Seconds() * 100) / duration_end.Seconds()
if !(perc < 100) {
// nothing to do
// Brexit is here!
os.Exit(0)
}
duration_until := end.Sub(now)
hours, minutes, seconds := int(duration_until.Hours()), int(duration_until.Minutes()), int(duration_until.Seconds())
days := int(hours / 24)
hours = hours - (days * 24)
minutes = minutes - (days * 1440) - (hours * 60)
seconds = seconds - (days * 86400) - (hours * 3600) - (minutes * 60)
eu_emoji, wave_emoji, uk_emoji := "🇪🇺", "👋", "🇬🇧"
tweet := fmt.Sprintf("Time until 3rd #Brexit deadline on 31 Oct 2019: %dd %dh %dm %ds %s%s%s\n", days, hours, minutes, seconds, eu_emoji, wave_emoji, uk_emoji)
creds := Credentials{
AccessToken: os.Getenv("ACCESS_TOKEN"),
AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
ConsumerKey: os.Getenv("CONSUMER_KEY"),
ConsumerSecret: os.Getenv("CONSUMER_SECRET"),
}
client, err := getClient(&creds)
tw, resp, err := client.Statuses.Update(tweet, nil)
if err != nil {
log.Println(err)
}
log.Printf("%+v\n", resp)
log.Printf("%+v\n", tw)
return fmt.Sprintf("Am I done?"), nil
}
func getClient(creds *Credentials) (*twitter.Client, error) {
config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
return client, nil
}