-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinder_api.py
160 lines (141 loc) · 5.74 KB
/
tinder_api.py
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
import json
import requests
from uuid import UUID
from config import Config
class TinderApi:
AUTH_URL = '/auth'
LIKE_URL = '/like/'
DISLIKE_URL = '/pass/'
RECOMMENDATIONS_URL = '/user/recs'
RECOMMENDATIONS_V2_URL = '/v2/recs/core?locale=en-US'
FAST_MATCH_INFO_URL = '/v2/fast-match/preview'
META_URL = '/meta'
PERSON_URL = '/user/'
SELF_URL = '/profile'
MATCH_INFO_URL = '/matches/'
SUPERLIKE_URL = LIKE_URL, '/super'
def __init__(self, fb_access_token=None, fb_id=None, tinder_token=None):
self.headers = {"content-type": "application/json"}
if not tinder_token:
self.get_auth_token(fb_access_token, fb_id)
else:
self.headers.update(self.valid_tinder_token(tinder_token))
@staticmethod
def valid_tinder_token(tinder_token):
if isinstance(tinder_token, dict):
if 'X-Auth-Token' in tinder_token:
return tinder_token
elif isinstance(tinder_token, str):
try:
UUID(tinder_token)
except TypeError:
raise Exception("Tinder token must be a valid UUID")
def get_auth_token(self, fb_access_token, fb_id):
url = Config.HOST + self.AUTH_URL
req = requests.post(url,
headers=self.headers,
data=json.dumps({'facebook_token': fb_access_token, 'facebook_id': fb_id}))
try:
self.check_response(req)
tinder_auth_token = req.json()["token"]
self.headers.update({"X-Auth-Token": tinder_auth_token})
except Exception as ex:
print("Can't receive tinder auth token")
raise ex
def get_recommendations(self):
'''
Returns a list of users that you can swipe on
'''
try:
r = requests.get(Config.HOST + self.RECOMMENDATIONS_URL, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong with getting recomendations:", e)
def get_person(self, person_id):
'''
Gets a user's profile via their id
'''
try:
url = Config.HOST + self.PERSON_URL + str(person_id)
r = requests.get(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not get that person:", e)
def get_self(self):
'''
Returns your own profile data
'''
try:
url = Config.HOST + self.SELF_URL
r = requests.get(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not get your data:", e)
def get_meta(self):
'''
Returns meta data on yourself. Including the following keys:
['globals', 'client_resources', 'versions', 'purchases',
'status', 'groups', 'products', 'rating', 'tutorials',
'travel', 'notifications', 'user']
'''
try:
url = Config.HOST + self.META_URL
r = requests.get(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not get your metadata:", e)
def get_recs_v2(self):
'''
This works more consistently then the normal get_recommendations becuase it seeems to check new location
'''
try:
url = Config.HOST + self.RECOMMENDATIONS_V2_URL
r = requests.get(url, headers=self.headers)
return r.json()
except Exception as e:
print('excepted')
def superlike(self, person_id):
try:
like_url = self.SUPERLIKE_URL[0]
super_url = self.SUPERLIKE_URL[1]
url = Config.HOST + like_url + person_id + super_url
r = requests.post(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not superlike:", e)
def like(self, person_id):
try:
url = Config.HOST + self.LIKE_URL + person_id
r = requests.get(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not like:", e)
def dislike(self, person_id):
try:
url = Config.HOST + self.DISLIKE_URL + person_id
r = requests.get(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not dislike:", e)
def match_info(self, match_id):
try:
url = Config.HOST + self.MATCH_INFO_URL + match_id
r = requests.get(url, headers=self.headers)
return r.json()
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not get your match info:", e)
def fast_match_info(self):
try:
url = Config.HOST + self.FAST_MATCH_INFO_URL
r = requests.get(url, headers=self.headers)
count = r.headers['fast-match-count']
# image is in the response but its in hex..
return count
except requests.exceptions.RequestException as e:
print("Something went wrong. Could not get your fast-match count:", e)
@staticmethod
def check_response(response):
if response.status_code != 200:
raise Exception(f"Response code {response.status_code}."
f" Reason: {response.reason}."
f" Error: {response.text}")