-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (95 loc) · 2.78 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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/genuinetools/pkg/cli"
"github.com/sirupsen/logrus"
)
var (
// Whether or not to enable debug logging
debug bool
// Where to store the downloaded favorites
directory string
// How often to run
interval time.Duration
// Whether we should run once or not
once bool
// Twitter consumer key
cKey string
// Twitter consumer secret
cSecret string
// Twitter access token
tToken string
// Twitter access secret
tSecret string
)
func main() {
p := cli.NewProgram()
p.Name = "twitback"
p.Description = "A bot to backup your favorites from Twitter"
p.GitCommit = GITCOMMIT
p.Version = VERSION
p.FlagSet = flag.NewFlagSet("twitback", flag.ExitOnError)
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
p.FlagSet.BoolVar(&debug, "debug", false, "enable debug logging")
p.FlagSet.BoolVar(&once, "once", false, "run once and exit")
p.FlagSet.DurationVar(&interval, "interval", 20*time.Hour, "update interval (ex. 5ms, 10s, 1m, 3h)")
p.FlagSet.StringVar(&directory, "dir", "downloads", "directory to store the downloaded favorites in")
p.FlagSet.StringVar(&cKey, "consumer-key", os.Getenv("CONSUMER_KEY"), "twitter consumer key")
p.FlagSet.StringVar(&cSecret, "consumer-secret", os.Getenv("CONSUMER_SECRET"), "twitter consumer secret")
p.FlagSet.StringVar(&tToken, "access-token", os.Getenv("ACCESS_TOKEN"), "twitter access token")
p.FlagSet.StringVar(&tSecret, "access-secret", os.Getenv("ACCESS_SECRET"), "twitter access secret")
p.Before = func(ctx context.Context) error {
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
p.Action = func(ctx context.Context, args []string) error {
if cKey == "" || cSecret == "" || tToken == "" || tSecret == "" {
return fmt.Errorf("consumer-key, consumer-secret, access-token, and access-secret are required")
}
ticker := time.NewTicker(interval)
if _, err := os.Stat(directory); os.IsNotExist(err) {
if err := os.MkdirAll(directory, 0755); err != nil {
return fmt.Errorf("creating directory %s failed: %v", directory, err)
}
}
// On ^C, or SIGTERM handle exit.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
for sig := range c {
logrus.Infof("Received %s, exiting.", sig.String())
os.Exit(0)
}
}()
if once {
run()
os.Exit(0)
}
logrus.Infof("starting bot to update every %s", interval)
for range ticker.C {
run()
}
return nil
}
// Run our program.
p.Run()
}
func run() error {
c := NewClient()
if err := c.VerifyCredentials(); err != nil {
return fmt.Errorf("could not authenticate with the Twitter API: %v", err)
}
if err := c.DownloadFavorites(); err != nil {
return err
}
return nil
}