summaryrefslogtreecommitdiff
path: root/db.py
blob: ab52c189bc2c9b8d853507d3585d76b114403ef4 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

import sqlite3

from profile import TinderProfile



# See 'row_factory' in https://docs.python.org/3/library/sqlite3.html#connection-objects
def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d


class HumanKind(object):

    def __init__(self):
        """Create a database for storing Tinder profiles."""

        self._db = sqlite3.connect('profiles.db', detect_types=sqlite3.PARSE_DECLTYPES)

        sqlite3.register_adapter(bool, int)
        sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))

        self._db.row_factory = dict_factory

        sql = '''
            CREATE TABLE IF NOT EXISTS People(
                uid TEXT PRIMARY KEY,
                name TEXT,
                gender INTEGER,
                birth_date TEXT,
                ping_time TEXT,
                school TEXT,
                job_title TEXT,
                job_company TEXT,
                distance_mi INTEGER,
                bio TEXT,
                liked BOOLEAN,
                superliked BOOLEAN,
                passed BOOLEAN,
                extracted BOOLEAN
            )
        '''

        cursor = self._db.cursor()
        cursor.execute(sql)
        self._db.commit()

        sql = '''
            CREATE TABLE IF NOT EXISTS Photos(
                uid TEXT,
                url TEXT,
                main BOOLEAN
            )
        '''

        cursor = self._db.cursor()
        cursor.execute(sql)
        self._db.commit()


    def has_profile_id(self, profile):
        """Tell is a given Tinder profile is known in the database."""

        values = (profile._id, )

        cursor = self._db.cursor()
        cursor.execute('SELECT uid FROM People WHERE uid = ?', values)

        found = cursor.fetchone()

        return not(found is None)


    def store_profile(self, profile):
        """Store a Tinder profile into the database."""

        exist = self.has_profile_id(profile)

        if exist:

            fields = 'name = ?, gender = ?, birth_date = ?, ping_time = ?, school = ?,' \
                     ' job_title = ?, job_company = ?, distance_mi = ?, bio = ?'

            values = (profile._name, profile._gender, profile._birth_date,
                      profile._ping_time, profile._school, profile._job_title,
                      profile._job_company, profile._distance_mi, profile._bio,
                      profile._id)

            cursor = self._db.cursor()
            cursor.execute('UPDATE People SET ' + fields + ' WHERE uid = ?', values)

        else:

            values = (profile._id, profile._name, profile._gender, profile._birth_date,
                      profile._ping_time, profile._school, profile._job_title,
                      profile._job_company, profile._distance_mi, profile._bio,
                      False, False, False, False)

            cursor = self._db.cursor()
            cursor.execute('INSERT INTO People VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', values)

        for p in profile._photos:

            values = (profile._id, p['url'])

            cursor = self._db.cursor()
            cursor.execute('SELECT uid FROM Photos WHERE uid = ? AND url = ?', values)

            found = cursor.fetchone()

            if found is None:

                values = (profile._id, p['url'], p['main'])

                cursor = self._db.cursor()
                cursor.execute('INSERT INTO Photos VALUES (?, ?, ?)', values)

        self._db.commit()


    def load_profile(self, uid):
        """Load a Tinder profile from the database."""

        values = (uid, )

        cursor = self._db.cursor()
        cursor.execute('SELECT * FROM People WHERE uid = ?', values)

        found = cursor.fetchone()

        if not(found):
            profile = None

        else:

            data = {}
            data['jobs'] = {}
            data['schools'] = {}
            data['photos'] = []

            data['ping_time'] = found['ping_time']

            if found['job_title']:
                data['jobs']['title'] = found['job_title']

            if found['job_company']:
                data['jobs']['company'] = found['job_company']

            if found['school']:
                data['schools']['name'] = found['school']

            data['gender'] = found['gender']

            data['distance_mi'] = found['distance_mi']

            data['_id'] = found['uid']

            if found['bio'] is None:
                data['bio'] = ''
            else:
                data['bio'] = found['bio']

            data['name'] = found['name']

            data['birth_date'] = found['birth_date']

            values = (uid, )

            cursor = self._db.cursor()
            cursor.execute('SELECT url, main FROM Photos WHERE uid = ?', values)

            for found in cursor.fetchall():

                photo = { 'url': found['url'], 'width': 1, 'height': 1 }

                rec = {}

                rec['processedFiles'] = [ photo ]

                if not(found['main']):
                    rec['main'] = False

                data['photos'].append(rec)

            profile = TinderProfile(data)

        return profile


    def load_all_profiles(self):
        """Load all stored Tinder profiles."""

        new = []

        cursor = self._db.cursor()
        cursor.execute('SELECT uid FROM People')

        for found in cursor.fetchall():

            profile = self.load_profile(found['uid'])

            new.append(profile)

        return new


    def load_unknown_profiles(self):
        """Load all new Tinder profiles."""

        new = []

        values = (False, False, False)

        cursor = self._db.cursor()
        cursor.execute('SELECT uid FROM People WHERE liked = ? AND passed = ? AND superliked = ?', values)

        for found in cursor.fetchall():

            profile = self.load_profile(found['uid'])

            new.append(profile)

        return new


    def load_new_profiles(self):
        """Load all new Tinder profiles."""

        new = []

        values = (False, )

        cursor = self._db.cursor()
        cursor.execute('SELECT uid FROM People WHERE extracted = ?', values)

        for found in cursor.fetchall():

            profile = self.load_profile(found['uid'])

            new.append(profile)

        return new


    def mark_profile_as_liked(self, profile):
        """Update information about a Tinder profile in the database."""

        values = (True, profile._id)

        cursor = self._db.cursor()
        cursor.execute('UPDATE People SET liked = ? WHERE uid = ?', values)
        self._db.commit()


    def mark_profile_as_superliked(self, profile):
        """Update information about a Tinder profile in the database."""

        values = (True, profile._id)

        cursor = self._db.cursor()
        cursor.execute('UPDATE People SET superliked = ? WHERE uid = ?', values)
        self._db.commit()


    def mark_profile_as_passed(self, profile):
        """Update information about a Tinder profile in the database."""

        values = (True, profile._id)

        cursor = self._db.cursor()
        cursor.execute('UPDATE People SET passed = ? WHERE uid = ?', values)
        self._db.commit()


    def mark_profile_as_extracted(self, profile):
        """Update information about a Tinder profile in the database."""

        values = (True, profile._id)

        cursor = self._db.cursor()
        cursor.execute('UPDATE People SET extracted = ? WHERE uid = ?', values)
        self._db.commit()