-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (68 loc) · 1.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
package main
import (
"errors"
"log"
"math"
"os"
"strconv"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type DaysRemaining int
func main() {
lambda.Start(Handler)
}
func Handler() {
VACATION_DATE := "2023/06/29 23:00:00 -03"
config := oauth1.NewConfig(
os.Getenv("API_KEY"),
os.Getenv("API_KEY_SECRET"),
)
token := oauth1.NewToken(
os.Getenv("ACCESS_TOKEN"),
os.Getenv("ACCESS_TOKEN_SECRET"),
)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
vacationDateFormatted, err := getVacationDateFormatted(VACATION_DATE)
if err != nil {
log.Fatal(err)
}
daysToVacation := getDaysRemaining(vacationDateFormatted)
messageToTweet, err := generateMessageToTweet(daysToVacation)
if err != nil {
log.Fatal(err)
}
log.Println("tweet", messageToTweet)
client.Statuses.Update(messageToTweet, nil)
}
func getVacationDateFormatted(date string) (time.Time, error) {
const shortFormat = "2006/01/02 15:04:05 -07"
dateParsed, err := time.Parse(shortFormat, date)
if err != nil {
return dateParsed, err
}
return dateParsed, nil
}
func getDaysRemaining(date time.Time) DaysRemaining {
today := time.Now()
duration := date.Sub(today)
return DaysRemaining(math.Ceil(duration.Hours() / 24))
}
func generateMessageToTweet(d DaysRemaining) (string, error) {
var message string
if alreadyOnVacation(d) {
return "", errors.New("Already on vacation")
}
if d == 1 {
message = "Hoje é o ultimo dia dessa porra"
} else {
message = "Faltam " + strconv.Itoa(int(d)) + " dias para as férias"
}
return message, nil
}
func alreadyOnVacation(d DaysRemaining) bool {
return d < 0
}