/
covfefe.go
139 lines (119 loc) · 3.37 KB
/
covfefe.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
package covfefe
import (
"context"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"crawshaw.io/sqlite"
"crawshaw.io/sqlite/sqlitex"
"github.com/dghubble/oauth1"
"github.com/golang/groupcache/lru"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2/clientcredentials"
"golang.org/x/sync/errgroup"
)
type Credentials struct {
APIKey string `json:"API_KEY"`
APISecret string `json:"API_SECRET"`
Accounts []Account
}
type Account struct {
Token string `json:"TOKEN"`
TokenSecret string `json:"TOKEN_SECRET"`
}
type Covfefe struct {
withConn func(f func(conn *sqlite.Conn) error) error
wg sync.WaitGroup
httpClient *http.Client
msgIDs *lru.Cache
mediaPath string
rescan bool
}
func Run(dbPath, mediaPath string, creds *Credentials) error {
db, err := sqlitex.Open("file:"+dbPath, 0, 5)
if err != nil {
return errors.Wrap(err, "failed to open database")
}
defer db.Close()
c := &Covfefe{
withConn: func(f func(conn *sqlite.Conn) error) error {
conn := db.Get(context.Background())
defer db.Put(conn)
if err := sqlitex.Exec(conn, "PRAGMA foreign_keys = ON;", nil); err != nil {
return err
}
return f(conn)
},
msgIDs: lru.New(1 << 16),
mediaPath: mediaPath,
}
c.httpClient = (&clientcredentials.Config{
ClientID: creds.APIKey,
ClientSecret: creds.APISecret,
TokenURL: "https://api.twitter.com/oauth2/token",
}).Client(context.Background())
c.httpClient.Timeout = 1 * time.Minute
if err := c.initDB(); err != nil {
return err
}
log.Info("Starting...")
messages := make(chan *Message)
c.wg.Add(1)
go func() {
c.HandleChan(messages)
c.wg.Done()
}()
ctx, _ := contextWithSignal(context.Background(), func(s os.Signal) {
log.WithField("signal", s).Info("Received signal, stopping...")
}, syscall.SIGINT, syscall.SIGTERM)
g, ctx := errgroup.WithContext(ctx)
config := oauth1.NewConfig(creds.APIKey, creds.APISecret)
for i, account := range creds.Accounts {
token := oauth1.NewToken(account.Token, account.TokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
httpClient.Timeout = 10 * time.Second
user, err := verifyCredentials(ctx, httpClient)
if err != nil {
return errors.Wrapf(err, "invalid credetials at position %d", i)
}
for _, timeline := range []string{"home", "mentions", "user", "likes"} {
timeline := timeline
g.Go(func() error {
log.WithFields(log.Fields{
"account": user.ScreenName, "id": user.ID, "timeline": timeline,
}).Info("Starting to monitor timeline")
m := &timelineMonitor{c: httpClient, u: user, m: messages}
return errors.Wrapf(m.followTimeline(ctx, timeline),
"%s of %d", timeline, user.ID)
})
}
}
log.WithError(g.Wait()).Error("Stopped following timelines")
close(messages)
c.wg.Wait()
return nil
}
// contextWithSignal acts like context.WithCancel, but the returned Context is
// also cancelled when one of the passed signals is received. A function f, if
// not nil, is called before cancelling the Context.
func contextWithSignal(parent context.Context, f func(os.Signal), sig ...os.Signal) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)
ch := make(chan os.Signal, 1)
signal.Notify(ch, sig...)
go func() {
select {
case <-ctx.Done():
case s := <-ch:
if f != nil {
f(s)
}
cancel()
}
signal.Stop(ch)
}()
return ctx, cancel
}