-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransform.py
144 lines (105 loc) · 4.27 KB
/
transform.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
import config
from database import Database
import urllib.request
from bs4 import BeautifulSoup
import time
import re
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s - %(lineno)d")
db = Database()
def modify_fields(data):
fields_to_delete = ['job_url', 'company_name', 'company_url']
key_rename_dict = {
"linkedin_job_url_cleaned" : "job_url",
"linkedin_company_url_cleaned" : "company_url",
"normalized_company_name" : "company_name"
}
for job in data:
for key in fields_to_delete: job.pop(key, None)
for old_key in key_rename_dict: job[key_rename_dict[old_key]] = job.pop(old_key)
job["applied"] = False
return data
def extract_description(data):
for job in data:
job_url = job.get("job_url")
if job_url:
try:
html = urllib.request.urlopen(job_url).read()
soup = BeautifulSoup(html, features="html.parser")
for script in soup(["script", "style"]):
script.extract()
text = soup.get_text()
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = "\n".join(chunk for chunk in chunks if chunk)
job["description"] = text
except Exception as e:
job["description"] = ""
logging.error("An error occurred while scraping %s: %s", job_url, str(e))
else:
job["description"] = ""
time.sleep(0.5)
return data
def extract_salaries(data):
for job in data:
description = job["description"]
salary_pattern_1 = r"\$([\d,]+)(?:\.(\d{2}))?"
salary_pattern_2 = r"\$([\d\.]+)(K)"
salary_pattern_3 = (
r"\$(?!401K)([\d,]+)(?:\.(\d{2}))?\s*(K)?\s*-"
r"\s*\$(?!401K)([\d,]+)(?:\.(\d{2}))?(K)?"
)
hourly_pattern = r"\$([\d\.]+)\s*to\s*\$([\d\.]+)\/hour"
million_pattern = r"\b\d+M\b"
# Search for patterns
match1 = re.search(salary_pattern_1, description)
match2 = re.search(salary_pattern_2, description)
match3 = re.search(salary_pattern_3, description)
match4 = re.search(hourly_pattern, description)
match5 = re.search(million_pattern, description)
salary_low, salary_high = None, None
if match3:
salary_low = (
float(match3.group(1).replace(",", "")) * 1000
if match3.group(3) == "K"
else float(match3.group(1).replace(",", ""))
)
salary_high = (
float(match3.group(4).replace(",", "")) * 1000
if match3.group(6) == "K"
else float(match3.group(4).replace(",", ""))
)
elif match2:
salary_low = salary_high = float(match2.group(1).replace(",", "")) * 1000
elif match1:
salary_low = salary_high = (
float(match1.group(1).replace(",", "") + "." + match1.group(2))
if match1.group(2)
else float(match1.group(1).replace(",", ""))
)
elif match4:
salary_low = float(match4.group(2)) * 40 * 52
salary_high = float(match4.group(3)) * 40 * 52
elif match5:
return (None, None)
if salary_low is not None and salary_low < 100:
salary_low *= 1000
if salary_high is not None and salary_high < 100:
salary_high *= 1000
job["salary_low"] = float(salary_low) if salary_low is not None else None
job["salary_high"] = float(salary_high) if salary_high is not None else None
return data
def process_data():
data = db.get_raw()
if data is not None:
logging.info("Modifying fields...")
data = modify_fields(data)
logging.info("Extracting descriptions...")
data = extract_description(data)
logging.info("Extracting salaries...")
data = extract_salaries(data)
logging.info("Storing processed data...")
db.store_processed(data)
logging.info("Processing data finished.")
else:
logging.warning("No data was processed.")