summaryrefslogtreecommitdiff
path: root/profile.py
blob: eff89e15e84a7417f226e9dce3e8f097704f2e92 (plain)
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

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]

                if len(bio) == 0:
                    bio = None
                    break

            if bio:
                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 + self._name + '-info.txt', 'w') as f:
            f.write(str(self))

        counter = 0

        for p in self._photos:

            response = requests.get(p['url'], stream=True)

            if response.status_code == 200:

                name = self._id + '-' + self._name + '-'

                age = self.compute_age()

                if age:
                    name += '%d' % int(age) + '-'

                filename = os.path.basename(p['url'])

                name += '%d' % counter + filename[filename.find('.'):]

                with open(dest + os.sep + name, 'wb') as f:
                    for chunk in response:
                        f.write(chunk)

                counter += 1


    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