generated from BloomTech-Labs/template-ds
-
Notifications
You must be signed in to change notification settings - Fork 6
/
dbfunctions.py
373 lines (340 loc) · 7.97 KB
/
dbfunctions.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import psycopg2
import re
def get_details(job_id, db):
job_listings_query = """
SELECT id, title, EXTRACT(epoch FROM post_date_utc), pay_min, pay_max, pay_exact, seniority FROM job_listings WHERE id = %(job_id)s
"""
cur = db.cursor()
cur.execute(job_listings_query, {'job_id': job_id})
results = cur.fetchone()
output = {
'job_id': results[0],
'title': results[1],
'post_timestamp': results[2],
'pay_min': results[3],
'pay_max': results[4],
'pay_exact': results[5],
'seniority': results[6]
}
try:
job_descriptions_query = """
SELECT description FROM job_descriptions WHERE job_id = %(job_id)s
"""
cur.execute(job_descriptions_query, {'job_id': job_id})
results = cur.fetchone()
output.update({
'description': results[0]
})
except Exception:
output.update({
'description': None
})
try:
job_keyphrases_query = """
SELECT keyphrase FROM job_keyphrases WHERE job_id = %(job_id)s
"""
cur.execute(job_keyphrases_query, {'job_id': job_id})
results = [result[0] for result in cur.fetchall()]
output.update({
'keyphrases': results,
})
except Exception:
output.update({
'keyphrases': []
})
try:
job_companies_query = """
SELECT name, description, size, revenue, logo_url
FROM job_companies
INNER JOIN companies
ON job_companies.company_id = companies.id
WHERE job_id = %(job_id)s
LIMIT 1
"""
cur.execute(
job_companies_query,
{'job_id': job_id}
)
results = cur.fetchone()
output.update({
'company_name': results[0],
'company_description': results[1],
'company_size': results[2],
'company_revenue': results[3],
'company_logo_url': results[4],
})
except Exception:
output.update({
'company_name': None,
'company_description': None,
'company_size': None,
'company_revenue': None,
'company_logo_url': None,
})
try:
job_locations_query = """
SELECT city, state_province, country
FROM job_locations
INNER JOIN locations
ON job_locations.location_id = locations.id
WHERE job_id = %(job_id)s
LIMIT 1;
"""
cur.execute(
job_locations_query,
{'job_id': job_id}
)
results = cur.fetchone()
output.update({
'location_city': results[0],
'location_state_province': results[1],
'location_country': results[2]
})
except Exception:
output.update({
'location_city': None,
'location_state_province': None,
'location_country': None
})
try:
job_links_query = """
SELECT external_url
FROM job_links
WHERE job_id = %(job_id)s
LIMIT 1;
"""
cur.execute(
job_links_query,
{'job_id': job_id}
)
results = cur.fetchone()
output.update({
'link': results[0],
})
except Exception:
output.update({
'link': None
})
cur.close()
return output
def get_jobs(db, count=100, city=None, state_province=None, country='US', title=None, before=None, after=None, salary_min=None, salary_max=None, seniority=None):
state_province = handle_state_province(state_province)
cur = db.cursor()
exact_location_subquery = '''
WHERE TRUE
'''
if city is not None:
exact_location_subquery += '''
AND city = %(city)s
'''
if state_province is not None:
exact_location_subquery += '''
AND state_province = %(state_province)s
'''
if country is not None:
exact_location_subquery += '''
AND country ILIKE %(country)s
'''
location_subquery = f'''
INNER JOIN (
SELECT *
FROM job_locations
INNER JOIN (
SELECT *
FROM locations
{exact_location_subquery}
) AS loc
ON job_locations.location_id = loc.id
) AS jobs_locs
ON job_listings.id = jobs_locs.job_id
'''
# filters only for key words that are probably in tech jobs
# can probably be removed after cron job to filter database is done
# ILIKE is Postgres specific and makes things case insensitive
job_details_subquery = '''
WHERE
title ILIKE '%%developer'
OR title ILIKE '%%designer'
OR title ILIKE '%%programmer'
OR title ILIKE '%%data'
OR title ILIKE '%%engineer'
OR title ILIKE '%%analyst'
OR title ILIKE '%%QA'
OR title ILIKE '%%UX'
OR title ILIKE '%%UI'
OR title ILIKE '%%dev'
OR title ILIKE '%%HCI'
OR title ILIKE '%%software'
OR title ILIKE '%%database'
OR title ILIKE '%%web'
OR title ILIKE '%%iOS'
OR title ILIKE '%%Android'
OR title ILIKE '%%mobile'
'''
if before is not None:
job_details_subquery += '''
AND post_date_utc < TO_TIMESTAMP(%(before)s)
'''
if after is not None:
job_details_subquery += '''
AND post_date_utc > TO_TIMESTAMP(%(after)s)
'''
if seniority is not None:
job_details_subquery += '''
AND seniority ILIKE %(seniority)s
'''
if salary_min is not None:
job_details_subquery += '''
AND (
pay_min > %(salary_min)s
OR pay_exact > %(salary_min)s
)
'''
if salary_max is not None:
job_details_subquery += '''
AND (
pay_max > %(salary_max)s
OR pay_exact > %(salary_max)s
)
'''
title_params = {}
if title is not None:
title_parts = title.split()
for i, title_part in enumerate(title_parts):
key = f'title_part_{i}'
title_params[key] = f'%{title_part}%'
job_details_subquery += f'''
AND title ILIKE %({key})s
'''
# final combined query, filtered by most recent post date
job_results_query = f"""
SELECT job_listings.id, job_listings.title, EXTRACT(epoch FROM job_listings.post_date_utc)
FROM job_listings
{location_subquery}
{job_details_subquery}
ORDER BY job_listings.post_date_utc DESC
LIMIT %(count)s;
"""
if city is not None:
city = titlecase(city)
if state_province is not None:
state_province = titlecase(state_province)
params = {
'count': count,
'city': city,
'state_province': state_province,
'country': country,
'before': before,
'after': after,
'seniority': seniority,
'salary_min': salary_min,
'salary_max': salary_max,
'title': title,
}
params.update(title_params)
try:
cur.execute(
job_results_query,
params
)
results = cur.fetchall()
resultList = []
for result in results:
try:
resultsjson = {
'job_id': result[0],
'title': result[1],
'post_timestamp': result[2],
'relevance': None,
'resume_score': None,
}
resultsjson.update(get_details(result[0], db))
resultList.append(resultsjson)
except Exception:
pass
except Exception as e:
resultList = []
finally:
cur.close()
return resultList
def handle_state_province(state_province):
if state_province is not None and len(state_province) < 4:
try:
state_province = abbr_to_state(state_province)
except Exception as e:
pass
return state_province
def titlecase(s: str) -> str:
"""
Titlecases a string in a stricter manner.
Does not fail on symbols and apostrophes.
Args:
s (str): string to titlecase
Returns:
str: titlecased string
"""
return (
re.sub(
r"^[A-Za-z]+",
lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),
re.sub(
r"(?<=[\n ])[A-Za-z]+",
lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),
s
)
)
)
def abbr_to_state(abbr):
return {
'DC': 'District of Columbia',
'AL': 'Alabama',
'MT': 'Montana',
'AK': 'Alaska',
'NE': 'Nebraska',
'AZ': 'Arizona',
'NV': 'Nevada',
'AR': 'Arkansas',
'NH': 'New Hampshire',
'CA': 'California',
'NJ': 'New Jersey',
'CO': 'Colorado',
'NM': 'New Mexico',
'CT': 'Connecticut',
'NY': 'New York',
'DE': 'Delaware',
'NC': 'North Carolina',
'FL': 'Florida',
'ND': 'North Dakota',
'GA': 'Georgia',
'OH': 'Ohio',
'HI': 'Hawaii',
'OK': 'Oklahoma',
'ID': 'Idaho',
'OR': 'Oregon',
'IL': 'Illinois',
'PA': 'Pennsylvania',
'IN': 'Indiana',
'RI': 'Rhode Island',
'IA': 'Iowa',
'SC': 'South Carolina',
'KS': 'Kansas',
'SD': 'South Dakota',
'KY': 'Kentucky',
'TN': 'Tennessee',
'LA': 'Louisiana',
'TX': 'Texas',
'ME': 'Maine',
'UT': 'Utah',
'MD': 'Maryland',
'VT': 'Vermont',
'MA': 'Massachusetts',
'VA': 'Virginia',
'MI': 'Michigan',
'WA': 'Washington',
'MN': 'Minnesota',
'WV': 'West Virginia',
'MS': 'Mississippi',
'WI': 'Wisconsin',
'MO': 'Missouri',
'WY': 'Wyoming',
}[abbr]