-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
428 lines (396 loc) · 14.3 KB
/
Copy pathmain.py
File metadata and controls
428 lines (396 loc) · 14.3 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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import pandas as pd
import requests
from sqlalchemy import create_engine
import sqlite3
import datetime
import psycopg2
engine = create_engine('postgresql://postgres:justjoin123@justjoin-1.cdg5aro86v8p.eu-north-1.rds.amazonaws.com:5432/just_init')
def check_if_valid_data(df: pd.DataFrame) -> bool:
# Check if dataframe is empty
if df.empty:
print("No new offers")
return False
# Primary Key Check
if pd.Series(df['id']).is_unique:
pass
else:
raise Exception("Primary Key check is violated")
return True
# if __name__ == '__main__':
def run_justjoin_etl():
response = requests.get('https://justjoin.it/api/offers')
data = response.json()
data_in = []
yesterday = datetime.date.today() - datetime.timedelta(days=1)
yesterday_formatted = yesterday.strftime('%Y-%m-%d')
for offer in data:
if offer["published_at"][:10] == yesterday_formatted:
if offer["display_offer"] == True:
data_in.append(offer)
# with open('offers.json', 'w') as f:
# json.dump(data_in, f)
# with open('offers.json', 'r') as f:
# data_in = json.loads(f.read())
def employment():
type = []
from_salary = []
to_salary = []
currency = []
id = []
for offer in data_in:
o_id = offer['id']
employment_types = offer["employment_types"]
for employment_type in employment_types:
o_type = employment_type["type"]
type.append(o_type)
salary = employment_type["salary"]
if salary is not None:
o_from_salary = int(salary["from"])
o_to_salary = int(salary["to"])
o_currency = salary["currency"]
else:
o_from_salary = None
o_to_salary = None
o_currency = None
from_salary.append(o_from_salary)
to_salary.append(o_to_salary)
currency.append(o_currency)
id.append(o_id)
employment_types_dict = {
'id': id,
'type': type,
'from_salary': from_salary,
'to_salary': to_salary,
'currency': currency
}
employment_types_df = pd.DataFrame(employment_types_dict, columns=[
'id', 'type', 'from_salary', 'to_salary', 'currency'
])
employment_types_df.fillna(0, inplace=True)
return employment_types_df
def skills():
name = []
level = []
id = []
for offer in data_in:
o_id = offer['id']
skills = offer["skills"]
for skill in skills:
o_name = skill["name"]
o_level = int(skill["level"])
name.append(o_name)
level.append(o_level)
id.append(o_id)
skills_dict = {
'id': id,
'name': name,
'level': level
}
skills_df = pd.DataFrame(skills_dict, columns=[
'id', 'name', 'level'
])
return skills_df
def offers():
company_name = []
id = []
title = []
city = []
country_code = []
marker_icon = []
experience_level = []
workplace_type = []
published_at = []
remote = []
for offer in data_in:
o_id = offer['id']
id.append(o_id)
o_title = offer['title']
o_title = o_title.replace(' - Remote ', '')
o_title = o_title.replace('Remote', '')
o_title = o_title.replace(' (REMOTE)', '')
o_title = o_title.replace(' (Remote)', '')
o_title = o_title.replace('Junior ', '')
o_title = o_title.replace('Junior/ ', '')
o_title = o_title.replace('(Junior) ', '')
o_title = o_title.replace(' (Junior)', '')
o_title = o_title.replace('Mid ', '')
o_title = o_title.replace('Mid-', '')
o_title = o_title.replace('Mid /', '')
o_title = o_title.replace('Mid / ', '')
o_title = o_title.replace('Mid/', '')
o_title = o_title.replace('Middle ', '')
o_title = o_title.replace('Middle/', '')
o_title = o_title.replace('Senior ', '')
o_title = o_title.replace('Senior / ', '')
o_title = o_title.replace('Senior/ ', '')
o_title = o_title.replace('(Senior) ', '')
o_title = o_title.replace(' (Senior)', '')
o_title = o_title.replace('Expert ', '')
o_title = o_title.replace('Lead ', '')
o_title = o_title.replace('Lead, ', '')
o_title = o_title.replace(' Lead', '')
o_title = o_title.replace(' (Automotive)', '')
o_title = o_title.replace(' (Azure)', '')
o_title = o_title.replace(' (GCP)', '')
o_title = o_title.replace(' (m/f/d)', '')
o_title = o_title.replace('👉 ', '')
o_title = o_title.replace('👉', '')
o_title = o_title.replace(' (Mid / Senior)', '')
title.append(o_title)
o_city = offer["city"]
city.append(o_city)
o_company_name = offer["company_name"]
company_name.append(o_company_name)
o_country_code = offer["country_code"]
country_code.append(o_country_code)
o_marker_icon = offer["marker_icon"]
marker_icon.append(o_marker_icon)
o_experience_level = offer["experience_level"]
experience_level.append(o_experience_level)
o_workplace_type = offer["workplace_type"]
workplace_type.append(o_workplace_type)
o_published_at = offer["published_at"][:10]
published_at.append(o_published_at)
o_remote = offer["remote"]
remote.append(o_remote)
offers_dict = {
'id': id,
'published_at': published_at,
'title': title,
'marker_icon': marker_icon,
'experience_level': experience_level,
'city': city,
'country_code': country_code,
'remote': remote,
'workplace_type': workplace_type,
'company_name': company_name
}
offers_df = pd.DataFrame(offers_dict, columns=[
'id', 'published_at', 'title', 'marker_icon', 'experience_level',
'city', 'country_code', 'remote', 'workplace_type', 'company_name'
])
return offers_df
def brands():
company_name = []
company_url = []
company_size = []
for offer in data_in:
o_company_name = offer["company_name"]
company_name.append(o_company_name)
o_company_url = offer["company_url"]
company_url.append(o_company_url)
o_company_size = offer["company_size"]
if o_company_size is not None:
o_company_size = o_company_size.replace(' ', '')
o_company_size = o_company_size.replace('.', '')
o_company_size = o_company_size.replace(',', '')
o_company_size = o_company_size.replace('+', '')
o_company_size = o_company_size.replace('>', '')
o_company_size = o_company_size.replace('<', '')
if o_company_size == "-":
o_company_size = None
else:
if '-' in o_company_size:
a, b = o_company_size.split("-")
o_company_size = int(b)
else:
o_company_size = None
company_size.append(o_company_size)
brands_dict = {
'company_name': company_name,
'company_size': company_size,
'company_url': company_url
}
brands_df = pd.DataFrame(brands_dict, columns=[
'company_name', 'company_size', 'company_url'
])
brands_df.fillna(0, inplace=True)
brands_df = brands_df.drop_duplicates('company_name')
return brands_df
def location():
id = []
office = []
slug = []
company_name = []
for offer in data_in:
multilocation = offer["multilocation"]
for location in multilocation:
o_office = location["city"]
o_slug = location["slug"]
slug.append(o_slug)
office.append(o_office)
o_id = offer['id']
id.append(o_id)
o_company_name = offer["company_name"]
company_name.append(o_company_name)
brands_office_dict = {
'company_name': company_name,
'slug': slug,
'office': office,
'id': id
}
brands_office_df = pd.DataFrame(brands_office_dict, columns=[
'company_name', 'slug', 'office', 'id'
])
return brands_office_df
# Validate
if check_if_valid_data(offers()):
print("Data valid, proceed to Load stage Offers")
## Load
#engine = create_engine('sqlite:///my_page/justjoin.sqlite3', echo=True)
#conn = sqlite3.connect('my_page/justjoin.sqlite3')
#cursor = conn.cursor()
# Connect to PostgreSQL
conn = psycopg2.connect(
dbname='just_init',
user='postgres',
password='justjoin123',
host='justjoin-1.cdg5aro86v8p.eu-north-1.rds.amazonaws.com',
port='5432'
)
conn.autocommit = True
# Create a cursor
cursor = conn.cursor()
brands_table_query = """
CREATE TABLE IF NOT EXISTS brands (
company_name VARCHAR(250) PRIMARY KEY,
company_size INTEGER,
company_url VARCHAR(500)
);
"""
offers_table_query = """
CREATE TABLE IF NOT EXISTS offers (
id VARCHAR(300) PRIMARY KEY,
published_at DATE,
title VARCHAR(100),
marker_icon VARCHAR(30),
experience_level VARCHAR(30),
city VARCHAR(100),
country_code VARCHAR(10),
remote VARCHAR(50),
workplace_type VARCHAR(50),
company_name VARCHAR(250),
FOREIGN KEY (company_name) REFERENCES brands(company_name)
);
"""
brands_office_table_query = """
CREATE TABLE IF NOT EXISTS brands_office (
slug VARCHAR(300) PRIMARY KEY,
company_name VARCHAR(250),
office VARCHAR(100),
id VARCHAR(300),
FOREIGN KEY (id) REFERENCES offers(id),
FOREIGN KEY (company_name) REFERENCES brands(company_name)
);
"""
skills_table_query = """
CREATE TABLE IF NOT EXISTS skills (
id VARCHAR(300),
name VARCHAR(150),
level INT,
FOREIGN KEY (id) REFERENCES offers(id)
);
"""
employment_types_query = """
CREATE TABLE IF NOT EXISTS employment_types (
id VARCHAR(300),
type VARCHAR(100),
from_salary INT,
to_salary INT,
currency VARCHAR(10),
FOREIGN KEY (id) REFERENCES offers(id)
);
"""
exiting_offers_query = "SELECT * FROM offers;"
exiting_brands_query = "SELECT * FROM brands;"
exiting_office_query = "SELECT * FROM brands_office;"
# offers_table_query = """
# CREATE TABLE IF NOT EXISTS offers(
# id VARCHAR(300) PRIMARY KEY,
# published_at DATE,
# title VARCHAR(100) ,
# marker_icon VARCHAR(30),
# experience_level VARCHAR(30),
# city VARCHAR(100),
# country_code VARCHAR(10),
# remote VARCHAR(50),
# workplace_type VARCHAR(50),
# company_name VARCHAR(250)
# );
# """
#
# brands_table_query = """
# CREATE TABLE IF NOT EXISTS brands(
# company_name VARCHAR(250) PRIMARY KEY,
# company_size INT,
# company_url VARCHAR(500)
# );
# """
#
# brands_office_table_query = """
# CREATE TABLE IF NOT EXISTS brands_office(
# slug VARCHAR(300) PRIMARY KEY,
# company_name VARCHAR(250),
# office VARCHAR(100),
# id VARCHAR(300)
# );
# """
#
# skills_table_query = """
# CREATE TABLE IF NOT EXISTS skills(
# id VARCHAR(300),
# name VARCHAR(50),
# level INT,
# FOREIGN KEY (id) REFERENCES offers(id)
# );
# """
#
# employment_types_query = """
# CREATE TABLE IF NOT EXISTS employment_types(
# id VARCHAR(300),
# type VARCHAR(100),
# from_salary INT,
# to_salary INT,
# currency VARCHAR(10),
# FOREIGN KEY (id) REFERENCES offers(id)
# );
# """
#
# # Verificate primary key
# exiting_offers_query = "SELECT * FROM offers"
# exiting_brands_query = "SELECT * FROM brands"
# exiting_office_query = "SELECT * FROM brands_office"
cursor.execute(offers_table_query)
try:
exiting_offers = pd.read_sql_query(exiting_offers_query, engine)
new_offers = offers()[~offers()['id'].isin(exiting_offers['id'])]
new_offers.to_sql("offers", engine, if_exists='append', index=False) #engine,
except:
print("Data already exists in the table offers")
cursor.execute(brands_table_query)
try:
exiting_brands = pd.read_sql_query(exiting_brands_query, engine)
new_brands = brands()[~brands()['company_name'].isin(exiting_brands['company_name'])]
new_brands.to_sql("brands", engine, index=False, if_exists='append') #engine,
except:
print("Data already exists in the table brands")
cursor.execute(brands_office_table_query)
try:
exiting_office = pd.read_sql_query(exiting_office_query, engine)
new_office = location()[~location()['slug'].isin(exiting_office['slug'])]
new_office.to_sql("brands_office", engine, index=False, if_exists='append') #engine,
except:
print("Data already exists in the table brands_office")
cursor.execute(skills_table_query)
try:
skills().to_sql("skills", engine, index=False, if_exists='append') #engine,
except:
print("Data already exists in the table skills")
cursor.execute(employment_types_query)
try:
employment().to_sql('employment_types', engine, index=False, if_exists='append') # engine,
except:
print("Data already exists in the table employment_types")
conn.close()
print("Close database successfully")
run_justjoin_etl()