-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtinder.ts
191 lines (170 loc) · 5.34 KB
/
tinder.ts
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
import { Browser, HTTPRequest, HTTPResponse, Page, Target } from 'puppeteer'
import { Subject } from 'rxjs'
import { injectFindElement, wait } from './util'
export class Tinder {
private page: Page
public like$: Subject<null>
public pass$: Subject<null>
public nbMatches = 0
public nbMsgMatches = 0
public nbLikedMe = 0
constructor(
private browser: Browser,
private googleLogin: string,
private googlePassword: string,
private latitude: number,
private longitude: number
) {
const context = browser.defaultBrowserContext()
context.overridePermissions('https://tinder.com/app/recs', ['geolocation'])
this.like$ = new Subject()
this.pass$ = new Subject()
}
log(msg: string, val?: any) {
console.log(`Tinder : ${msg}`, val)
}
async ready() {
this.page = await this.browser.newPage()
await this.page.setGeolocation({
latitude: this.latitude,
longitude: this.longitude,
})
await this.page.setRequestInterception(true)
this.createListeners()
await this.page.goto('https://tinder.com/app/recs', {
waitUntil: 'networkidle0',
})
await wait(3000)
await injectFindElement(this.page)
if (!this.isLoggedIn()) {
this.log('Not logged in.')
await this.loginFlow()
this.log('Finished logging in.')
} else {
this.log('Already logged in.')
}
}
createListeners() {
this.page.on('request', (request) => {
const likeUrl = 'https://api.gotinder.com/like'
const passUrl = 'https://api.gotinder.com/pass'
if (request.resourceType() === 'image') request.abort()
if (
request.method() === 'POST' &&
request.url().substr(0, likeUrl.length) === likeUrl
) {
this.like$.next()
}
if (
request.method() === 'GET' &&
request.url().substr(0, passUrl.length) === passUrl
) {
this.pass$.next()
}
request.continue().catch((e) => {})
})
this.page.on('response', async (response) => {
const matchesUrl =
'https://api.gotinder.com/v2/matches?locale=en&count=60&message=0'
const msgMatchesUrl =
'https://api.gotinder.com/v2/matches?locale=en&count=60&message=1'
const likedMeUrl =
'https://api.gotinder.com/v2/fast-match/teaser?locale=en'
if (response.request().method() !== 'GET' || response.status() !== 200)
return
if (response.url().substr(0, matchesUrl.length) === matchesUrl) {
try {
const res: any = await response.json()
this.nbMatches = res.data.matches.length
} catch (e) {
this.log('Matches error', e)
}
}
if (response.url().substr(0, msgMatchesUrl.length) === msgMatchesUrl) {
try {
const res: any = await response.json()
this.nbMsgMatches = res.data.matches.length
} catch (e) {
this.log('MatchesMsg error', e)
}
}
if (response.url().substr(0, likedMeUrl.length) === likedMeUrl) {
try {
const res: any = await response.json()
this.nbLikedMe = res.data.count
} catch (e) {
this.log('LikedMe error', e)
}
}
})
}
isLoggedIn() {
return this.page.url() !== 'https://tinder.com/'
}
async loginFlow() {
await this.page.evaluate(() => {
;(<any>window).findElement('button', 'log in').click()
setTimeout(() => {
;(<any>window).findElement('button', 'log in with google').click()
}, 2000)
})
const popupPage = await new Promise<Page>((x) =>
this.page.once('popup', (page) => x(page))
)
this.log('Found google login popup !')
await popupPage.waitForSelector('#identifierId')
await popupPage.$eval(
'#identifierId',
(el: HTMLInputElement, login: string) => (el.value = login),
this.googleLogin
)
await popupPage.$eval('#identifierNext button', (el: HTMLButtonElement) =>
el.click()
)
await popupPage.waitForSelector('#password input')
await popupPage.$eval(
'#password input',
(el: HTMLInputElement, password: string) => (el.value = password),
this.googlePassword
)
await popupPage.$eval('#passwordNext button', (el: HTMLButtonElement) =>
el.click()
)
await new Promise((x) => popupPage.once('close', (page) => x(null)))
await this.page.waitForNavigation({
waitUntil: 'networkidle0',
})
await wait(2000)
}
isOutOfLike() {
return this.page.evaluate(
() => !!(<any>window).findElement('h3', "you're out of likes!")
)
}
hidePopup() {
return this.page.evaluate(() => {
var noThanksBtn = (<any>window).findElement('button', 'no thanks')
if (noThanksBtn) noThanksBtn.click()
var maybeLaterBtn = (<any>window).findElement('button', 'maybe later')
if (maybeLaterBtn) maybeLaterBtn.click()
var notInterestedBtn = (<any>window).findElement(
'button',
'not interested'
)
if (notInterestedBtn) notInterestedBtn.click()
var backToTinderBtn: any = document.querySelector(
'button[title="Back to Tinder"'
)
if (backToTinderBtn) backToTinderBtn.click()
})
}
like() {
return this.page.keyboard.press('ArrowRight')
}
pass() {
return this.page.keyboard.press('ArrowLeft')
}
totalMatches() {
return this.nbLikedMe + this.nbMatches + this.nbMsgMatches
}
}