-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuser.py
More file actions
224 lines (191 loc) · 7.97 KB
/
user.py
File metadata and controls
224 lines (191 loc) · 7.97 KB
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
from tinder_api import session
from tinder_api.utils import request_handlers as r
from tinder_api.utils.wrapper import JsonWrapper
import json
import datetime
import dateutil.parser
class UserModel():
def __init__(self, uid, name, bio, age, birth_date, photos,
gender, distance, job_name, job_title, school_name,
school_id, ping_time, top_song, instagram_photos):
self.id = uid
self.name = name
self.bio = bio
self.age = age
self.birth_date = birth_date
self.photos = photos
self.gender = gender
self.distance = distance
self.job_name = job_name
self.job_title = job_title
self.school_name = school_name
self.school_id = school_id
self.ping_time = ping_time
self.top_song = top_song
self.instagram_photos = instagram_photos
def report(self, cause, text=''):
"""Reports the user
Cause:
0 : 'other' requires text
1 : 'spam'
4 : 'inappropriate photos'
"""
return r.post('/report/{}'.format(uid),
{"cause": cause, "text": text})
class NormalUser(UserModel):
def __init__(self, uid, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos):
super().__init__(uid, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos)
def like(self):
"""Likes (swipes right) the user"""
resp = r.get('/like/{}'.format(self.id))
return resp['match']
def super_like(self):
"""Super likes (swipes up) the user"""
resp = r.post('/like/{}/super'.format(self.id), {})
return resp['match']
def dislike(self):
"""Dislikes (swipes left) the user"""
resp = r.post('/pass/{}'.format(self.id))
return 'passed'
class MatchUser(UserModel):
def __init__(self, uid, match_id, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos):
super().__init__(uid, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos)
self.match_id = match_id
self.match_data = self.get_match_data()
def get_match_data(self):
"""Returns a [] of match data"""
return [x for x in session.Session().list_matches() if x['_id'] == self.match_id][0]
def message(self, body):
"""Messages the user"""
resp = r.post('/user/matches/{}'.format(self.match_id),
{"message": str(body)})
return resp['sent_date']
def get_messages(self):
"""Constructs a Message() object for each message"""
return [Message(x, self.id, self.name) for x in self.match_data['messages']]
class Message():
def __init__(self, data, uid, name):
self.message_id = data['_id']
self.data = data
self.sent = dateutil.parser.parse(data['sent_date'])
self.body = data['message']
self.sender = name if data['from'] == uid else "Me"
self.to = name if data['to'] == uid else "Me"
def like_message(self):
"""Likes a message"""
resp = r.post('/message/{}/like'.format(self.message_id), {})
if 'error' in resp:
return "Error, unable to like message"
return resp
def unlike_message(self):
"""Unlikes a message"""
resp = r.delete('/message/{}/like'.format(self.message_id))
if resp.status_code == 204:
return resp
return "Error, unable to unlike the message"
def is_liked(self):
"""Returns True if the messages is liked, otherwise False"""
liked_messages = [x['message_id'] for x in session.Session().get_updates()['liked_messages']]
return self.message_id in liked_messages
def __unicode__(self):
return self.body
def __str__(self):
return self.body.encode('utf-8')
def __repr__(self):
return repr(self.body)
class UserController:
def __init__(self, uid):
self.id = uid
self.me_id = session.Session().get_id()
self.user_type = self._decode_user_type()
self.data = self.get_data()
self.const = JsonWrapper(self.data, iter_keys_only=False)
def get_data(self):
"""Returns the data of the user"""
if self.user_type == 'Me':
data = r.get('/profile')
return data
else:
data = r.get('/user/{}'.format(self.id))
if 'error' in data:
print('Error user was not found')
return data['results']
def _decode_user_type(self):
"""Returns the user_type (Me, Match, Normal) based on uid"""
if self.me_id == self.id:
return 'Me'
elif self.me_id in self.id:
self.match_id = self.id
self.id = self.id.replace(self.me_id, '')
return 'Match'
else:
return 'Normal'
def get_user(self):
"""Constructs the correct User Object based on user_type"""
name = self.const.name
bio = self.const.bio
birth_date = self._decode_birth_date()
age = self._decode_age()
photos = [photo.url for photo in self.const.photos]
gender = self._decode_gender()
distance = self._decode_distance()
job_name = self.const.jobs[0].company.name
job_title = self.const.jobs[0].title.name
school_name = self.const.schools[0].name
school_id = self.const.schools[0].id
ping_time = self.const.ping_time
top_song = self._decode_theme_song()
instagram_photos = [photo.image for photo in self.const.instagram.photos]
if self.user_type == 'Normal':
return NormalUser(self.id, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos)
elif self.user_type == 'Match':
return MatchUser(self.id, self.match_id, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos)
elif self.user_type == 'Me':
return UserModel(self.id, name, bio, age, birth_date, photos, gender,
distance, job_name, job_title, school_name, school_id,
ping_time, top_song, instagram_photos)
def _decode_birth_date(self):
"""Returns the human readable birth_date"""
return dateutil.parser.parse(self.const.birth_date)
def _decode_age(self):
"""Returns age as an int"""
today = datetime.date.today()
return (today.year - self._decode_birth_date().year -
((today.month, today.day) <
(self._decode_birth_date().month,
self._decode_birth_date().day)))
def _decode_gender(self):
"""Converts gender to a human readable format"""
gender = self.const.gender
if gender == 1:
return 'female'
elif gender == 0:
return 'male'
def _decode_distance(self):
"""Returns distance in miles"""
if 'distance_mi' in self.data:
return self.const.distance_mi
elif 'distance_km' in self.data:
return self.const.distance_km * 0.621371
def _decode_jobs(self):
"""Returns a [] of job names"""
return [job.company.name for job in self.const.jobs]
def _decode_theme_song(self):
"""Returns a {name, id, artist} of the user's spotify theme"""
theme_s = self.const.spotify_theme_track
return {'name': theme_s.name,
'id': theme_s.id,
'artist': theme_s.artists[0].name}
pass