-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
288 lines (258 loc) · 6.33 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"fmt"
"flag"
"os"
"time"
"sync"
"github.com/jroimartin/gocui"
"github.com/zachlatta/tinder"
)
type Message struct {
ID string
MatchID string
Timestamp int64
To string
From string
Message string
Sent string
}
type Person struct {
ID string
Bio string
Birth string
Gender int
Name string
PingTime string
}
type Match struct {
ID string
CommonFriendCount int
CommonLikeCount int
MessageCount int
Messages []*Message
Person *Person
}
type TinderProfile struct {
Matches []*Match
Recommendations []tinder.Recommendation
}
var (
facebookUserID, facebookToken string
tinderClient *tinder.Tinder
profile TinderProfile
profileMut sync.Mutex
updateGUIChan chan struct{}
// gui
g *gocui.Gui
backgroundView *gocui.View
profilePictureView *gocui.View
swipeLeftView *gocui.View
swipeRightView *gocui.View
infoView *gocui.View
)
func showMatched(g *gocui.Gui, v *gocui.View) error {
maxX, maxY := g.Size()
if v, err := g.SetView("matched", maxX/2-8, maxY/2, maxX/2+8, maxY/2+2); err != nil {
if err != gocui.ErrorUnkView {
return err
}
fmt.Fprintln(v, "Matched!")
if err := g.SetCurrentView("matched"); err != nil {
return err
}
}
return nil
}
func hideMatched(g *gocui.Gui, v *gocui.View) error {
if err := g.DeleteView("matched"); err != nil {
return err
}
if err := g.SetCurrentView("background"); err != nil {
return err
}
return nil
}
func swipeLeft(g *gocui.Gui, v *gocui.View) error {
if err := tinderClient.Pass(profile.Recommendations[0].ID); err != nil {
fmt.Println(err)
}
profile.Recommendations = profile.Recommendations[1:]
updateGUIChan<-struct{}{}
return nil
}
func swipeRight(g *gocui.Gui, v *gocui.View) error {
matched, err := tinderClient.Like(profile.Recommendations[0].ID)
if err != nil {
fmt.Println(err)
}
profile.Recommendations = profile.Recommendations[1:]
if matched {
showMatched(g, v)
}
updateGUIChan<-struct{}{}
return nil
}
func layout(g *gocui.Gui) error {
var err error
maxX, maxY := g.Size()
if backgroundView, err = g.SetView("background", 0, 0, maxX, maxY); err != nil {
if err != gocui.ErrorUnkView {
return err
}
}
if profilePictureView, err = g.SetView("profilePicture", 1, 1, maxX-1, maxY-5); err != nil {
if err != gocui.ErrorUnkView {
return err
}
profilePictureView.Wrap = true
fmt.Fprintln(profilePictureView, "Loading...")
}
if swipeLeftView, err = g.SetView("swipeLeft", 6, maxY-4, maxX/2-5, maxY-1); err != nil {
if err != gocui.ErrorUnkView {
return err
}
fmt.Fprintln(swipeLeftView, "X")
}
if swipeRightView, err = g.SetView("swipeRight", maxX-(maxX/2-5), maxY-4, maxX-6, maxY-1); err != nil {
if err != gocui.ErrorUnkView {
return err
}
fmt.Fprintln(swipeRightView, "<3")
}
if infoView, err = g.SetView("info", maxX/2-4, maxY-4, maxX/2+4, maxY-1); err != nil {
if err != gocui.ErrorUnkView {
return err
}
fmt.Fprintln(infoView, "i")
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.Quit
}
func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}
if err := g.SetKeybinding("", 'h', gocui.ModNone, swipeLeft); err != nil {
return err
}
if err := g.SetKeybinding("", 'l', gocui.ModNone, swipeRight); err != nil {
return err
}
if err := g.SetKeybinding("", 'j', gocui.ModNone, hideMatched); err != nil {
return err
}
return nil
}
func runGUI() error {
g = gocui.NewGui()
if err := g.Init(); err != nil {
return err
}
defer g.Close()
g.SetLayout(layout)
if err := keybindings(g); err != nil {
return err
}
g.SelBgColor = gocui.ColorGreen
g.SelFgColor = gocui.ColorBlack
err := g.MainLoop()
if err != nil && err != gocui.Quit {
return err
}
return nil
}
func pollTinder(updateGUI chan struct{}, client *tinder.Tinder, profile *TinderProfile) {
for _ = range time.Tick(5 * time.Second) {
profileMut.Lock()
resp, err := client.GetUpdates()
if err != nil {
fmt.Fprintln(os.Stderr, "Error polling:", err)
}
matches := make([]*Match, len(resp.Matches))
for i, match := range resp.Matches {
matches[i] = &Match{
ID: match.ID,
CommonFriendCount: match.CommonFriendCount,
CommonLikeCount: match.CommonLikeCount,
MessageCount: match.MessageCount,
Person: &Person{
ID: match.Person.ID,
Bio: match.Person.Bio,
Birth: match.Person.Birth,
Gender: match.Person.Gender,
Name: match.Person.Name,
PingTime: match.Person.PingTime,
},
}
messages := make([]*Message, len(match.Messages))
for i, msg := range match.Messages {
messages[i] = &Message{
ID: msg.ID,
MatchID: msg.MatchID,
Timestamp: msg.Timestamp,
To: msg.To,
From: msg.From,
Message: msg.Message,
Sent: msg.Sent,
}
}
matches[i].Messages = messages
}
profile.Matches = matches
if len(profile.Recommendations) == 0 {
recsResp, err := client.GetRecommendations()
if err != nil {
if err != tinder.RecsExhausted && err != tinder.RecsTimeout {
fmt.Fprintln(os.Stderr, "Error polling:", err)
}
} else {
profile.Recommendations = recsResp.Results
}
}
profileMut.Unlock()
updateGUI<-struct{}{}
}
}
func updateGUI(updateGUI chan struct{}) {
for _ = range updateGUI {
profilePictureView.Clear()
if len(profile.Recommendations) > 0 {
topRec := profile.Recommendations[0]
fmt.Fprintf(profilePictureView, ` ___
/ \
| o o |
\_=_/
Name: %s
Distance (mi): %d
Age: %d
----------------
%s`, topRec.Name, topRec.DistanceInMiles, time.Now().Sub(topRec.Birth)/time.Hour/24/365, topRec.Bio)
} else {
fmt.Fprintln(profilePictureView, "There's no one new around you.")
}
g.Flush()
}
}
func main() {
flag.StringVar(&facebookUserID, "id", "", "facebook user id")
flag.StringVar(&facebookToken, "token", "", "access token")
flag.Parse()
if flag.NArg() == flag.NFlag() {
fmt.Fprintf(os.Stderr, "Error: all flags are required. Run with -help for options.\n")
os.Exit(1)
}
tinderClient = tinder.Init(facebookUserID, facebookToken)
if err := tinderClient.Auth(); err != nil {
fmt.Fprintln(os.Stderr, "Could not authenticate: ", err.Error())
os.Exit(1)
}
updateGUIChan = make(chan struct{})
go pollTinder(updateGUIChan, tinderClient, &profile)
go updateGUI(updateGUIChan)
if err := runGUI(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
}
}