diff options
Diffstat (limited to 'profile.py')
-rw-r--r-- | profile.py | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/profile.py b/profile.py new file mode 100644 index 0000000..e4d772b --- /dev/null +++ b/profile.py @@ -0,0 +1,193 @@ + +from datetime import datetime +import os +import requests + +import config + + +class TinderProfile(object): + + def _parse_processed_files(self, data, main): + """Keep the biggest photo from a list.""" + + best_size = 0 + best_url = None + + for p in data: + + size = p['width'] * p['height'] + + if size > best_size: + best_url = p['url'] + best_size = size + + if best_url: + self._photos.append({'url': best_url, 'main': main}) + + + def __init__(self, data): + """Load information about a Tinder user from its extracted data.""" + + self._ping_time = data['ping_time'] + + if 'title' in data['jobs']: + self._job_title = data['jobs']['title'] + else: + self._job_title = None + + if 'company' in data['jobs']: + self._job_company = data['jobs']['company'] + else: + self._job_company = None + + if 'name' in data['schools']: + self._school = data['schools']['name'] + else: + self._school = None + + self._photos = [] + + for p in data['photos']: + self._parse_processed_files(p['processedFiles'], not('main' in p.keys())) + + self._gender = data['gender'] + + self._distance_mi = data['distance_mi'] + + self._id = data['_id'] + + if 'bio' in data: + self._bio = data['bio'] if len(data['bio']) > 0 else None + else: + self._bio = None + + self._name = data['name'] + + self._birth_date = data['birth_date'] + + + def __str__(self): + """Output a summary of the profile.""" + + desc = self._name + + age = self.compute_age() + + if age: + desc += '\n age: %.1f years old' % age + + activity = self.compute_last_activity() + + unit = activity['unit'] if activity['value'] < 2 else activity['unit'] + 's' + + desc += '\n last seen: %.1f %s ago' % (activity['value'], unit) + + if self._school: + desc += '\n school: %' % self._school + + if self._job_title: + desc += '\n job: %s' % self._job_title + + if self._job_company: + desc += '\n company: %s' % self._job_company + + dist = self.compute_distance() + + unit = 'km' if config.DISTANCE_IN_KM else 'mile' + + if dist < 2: + desc += '\n distance: %d %s' % (dist, unit) + else: + desc += '\n distance: %d %ss' % (dist, unit) + + count = len(self._photos) + + if count < 2: + desc += '\n %d photo' % count + else: + desc += '\n %d photos' % count + + if self._bio: + + bio = self._bio.replace('\n', '\n ') + + while bio[-1] == '\n' or bio[-1] == ' ': + bio = bio[:-1] + + desc += '\n\n' + desc += ' ' + bio + + return desc + + + def output(self): + """Output the profile in a directory.""" + + dest = config.OUTPUT_DIR + os.sep + self._id + + if not os.path.exists(dest): + os.makedirs(dest) + + with open(dest + os.sep + 'info.txt', 'w') as f: + f.write(str(self)) + + for p in self._photos: + + response = requests.get(p['url'], stream=True) + + if response.status_code == 200: + + name = os.path.basename(p['url']) + + with open(dest + os.sep + name, 'wb') as f: + for chunk in response: + f.write(chunk) + + + def compute_age(self): + """Provide the age of the Tinder user.""" + + if not(self._birth_date): + age = None + + else: + + birth = datetime.strptime(self._birth_date, "%Y-%m-%dT%H:%M:%S.%fZ") + + diff = datetime.now() - birth + + age = float("%.1f" % round(diff.days / 365.0, 1)) + + return age + + + def compute_last_activity(self): + """Compute the last activity of the Tinder user.""" + + seen = datetime.strptime(self._ping_time, "%Y-%m-%dT%H:%M:%S.%fZ") + + diff = datetime.now() - seen + + if diff.days < (365.0 / 12): + activity = { 'value': diff.days, 'unit': 'day' } + + elif diff.days < 365: + activity = { 'value': float("%.1f" % round(diff.days / (365.0 / 12), 1)), 'unit': 'month' } + + else: + activity = { 'value': float("%.1f" % round(diff.days / 365.0, 1)), 'unit': 'year' } + + return activity + + + def compute_distance(self): + """Provide the distance in kms if needed.""" + + if config.DISTANCE_IN_KM: + dist = int(self._distance_mi * 1.60934) + + else: + dist = self._distance_mi + + return dist |