LeoWalker commited on
Commit
4fad2af
1 Parent(s): 47cc304

set up to dump to MongoDB instead of PostgreSQL

Browse files
notebooks/gj_error.ipynb ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from multiprocessing import process\n",
10
+ "import pandas as pd\n",
11
+ "import datetime as dt\n",
12
+ "import http.client\n",
13
+ "import json\n",
14
+ "import urllib.parse\n",
15
+ "import os\n",
16
+ "from pymongo import MongoClient\n",
17
+ "from concurrent.futures import ThreadPoolExecutor, as_completed\n",
18
+ "\n",
19
+ "from dotenv import load_dotenv\n",
20
+ "load_dotenv()\n",
21
+ "\n",
22
+ "mongodb_conn = os.getenv('MONGODB_CONNECTION_STRING')\n",
23
+ "\n",
24
+ "# Global variables to keep track of searched job titles and cities\n",
25
+ "searched_jobs = set()\n",
26
+ "searched_cities = set()\n",
27
+ "\n",
28
+ "def google_job_search(job_title, city_state, start=0):\n",
29
+ " '''\n",
30
+ " job_title(str): \"Data Scientist\", \"Data Analyst\"\n",
31
+ " city_state(str): \"Denver, CO\"\n",
32
+ " '''\n",
33
+ " query = f\"{job_title} {city_state}\"\n",
34
+ " params = {\n",
35
+ " \"api_key\": os.getenv('WEBSCRAPING_API_KEY'),\n",
36
+ " \"engine\": \"google_jobs\",\n",
37
+ " \"q\": query,\n",
38
+ " \"hl\": \"en\",\n",
39
+ " # \"google_domain\": \"google.com\",\n",
40
+ " # \"start\": start,\n",
41
+ " # \"chips\": f\"date_posted:{post_age}\",\n",
42
+ " }\n",
43
+ "\n",
44
+ " query_string = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)\n",
45
+ "\n",
46
+ " conn = http.client.HTTPSConnection(\"serpapi.webscrapingapi.com\")\n",
47
+ " try:\n",
48
+ " conn.request(\"GET\", f\"/v1?{query_string}\")\n",
49
+ " print(f\"GET /v1?{query_string}\")\n",
50
+ " res = conn.getresponse()\n",
51
+ " try:\n",
52
+ " data = res.read()\n",
53
+ " finally:\n",
54
+ " res.close()\n",
55
+ " finally:\n",
56
+ " conn.close()\n",
57
+ "\n",
58
+ " try:\n",
59
+ " json_data = json.loads(data.decode(\"utf-8\"))\n",
60
+ " jobs_results = json_data['google_jobs_results']\n",
61
+ " return jobs_results\n",
62
+ " except (KeyError, json.JSONDecodeError) as e:\n",
63
+ " print(f\"Error occurred for search: {job_title} in {city_state}\")\n",
64
+ " print(f\"Error message: {str(e)}\")\n",
65
+ " print(f\"Data: {data}\")\n",
66
+ " return None\n",
67
+ "\n",
68
+ "def mongo_dump(jobs_results, collection_name):\n",
69
+ " client = MongoClient(mongodb_conn)\n",
70
+ " db = client.job_search_db\n",
71
+ " collection = db[collection_name]\n",
72
+ " \n",
73
+ " for job in jobs_results:\n",
74
+ " job['retrieve_date'] = dt.datetime.today().strftime('%Y-%m-%d')\n",
75
+ " collection.insert_one(job)\n",
76
+ " \n",
77
+ " print(f\"Dumped {len(jobs_results)} documents to MongoDB collection {collection_name}\")\n",
78
+ "\n",
79
+ "def process_batch(job, city_state, start=0):\n",
80
+ " global searched_jobs, searched_cities\n",
81
+ "\n",
82
+ " # Check if the job title and city have already been searched\n",
83
+ " if (job, city_state) in searched_jobs:\n",
84
+ " print(f'Skipping already searched job: {job} in {city_state}')\n",
85
+ " return\n",
86
+ "\n",
87
+ " jobs_results = google_job_search(job, city_state, start)\n",
88
+ " if jobs_results is not None:\n",
89
+ " print(f'City: {city_state} Job: {job} Start: {start}')\n",
90
+ " mongo_dump(jobs_results, 'sf_bay_test_jobs')\n",
91
+ "\n",
92
+ " # Add the job title and city to the searched sets\n",
93
+ " searched_jobs.add((job, city_state))\n",
94
+ " searched_cities.add(city_state)\n",
95
+ "\n",
96
+ "def main(job_list, city_state_list):\n",
97
+ " for job in job_list:\n",
98
+ " for city_state in city_state_list:\n",
99
+ " output = process_batch(job, city_state)"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "metadata": {},
106
+ "outputs": [],
107
+ "source": [
108
+ "job_list = [\"Data Scientist\", \"Machine Learning Engineer\", \"AI Gen Engineer\", \"ML Ops\"]\n",
109
+ "city_state_list = [\"Atlanta, GA\", \"Austin, TX\", \"Boston, MA\", \"Chicago, IL\", \n",
110
+ " \"Denver CO\", \"Dallas-Ft. Worth, TX\", \"Los Angeles, CA\",\n",
111
+ " \"New York City NY\", \"San Francisco, CA\", \"Seattle, WA\",\n",
112
+ " \"Palo Alto CA\", \"Mountain View CA\", \"San Jose, CA\"]\n",
113
+ "simple_city_state_list: list[str] = [\"Palo Alto CA\", \"San Francisco CA\", \"Mountain View CA\"]\n",
114
+ "main(job_list, simple_city_state_list)"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": 4,
120
+ "metadata": {},
121
+ "outputs": [
122
+ {
123
+ "name": "stdout",
124
+ "output_type": "stream",
125
+ "text": [
126
+ "Skipping already searched job: Data Scientist in San Francisco, CA\n"
127
+ ]
128
+ }
129
+ ],
130
+ "source": [
131
+ "process_batch(\"Data Scientist\", \"San Francisco, CA\", 10)"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "code",
136
+ "execution_count": null,
137
+ "metadata": {},
138
+ "outputs": [],
139
+ "source": []
140
+ },
141
+ {
142
+ "cell_type": "code",
143
+ "execution_count": null,
144
+ "metadata": {},
145
+ "outputs": [],
146
+ "source": []
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": null,
151
+ "metadata": {},
152
+ "outputs": [],
153
+ "source": [
154
+ "client = MongoClient(mongodb_conn)\n",
155
+ "db = client.job_search_db\n",
156
+ "collection = db['sf_bay_test_jobs']"
157
+ ]
158
+ },
159
+ {
160
+ "cell_type": "code",
161
+ "execution_count": null,
162
+ "metadata": {},
163
+ "outputs": [],
164
+ "source": []
165
+ }
166
+ ],
167
+ "metadata": {
168
+ "kernelspec": {
169
+ "display_name": "datajobs",
170
+ "language": "python",
171
+ "name": "python3"
172
+ },
173
+ "language_info": {
174
+ "codemirror_mode": {
175
+ "name": "ipython",
176
+ "version": 3
177
+ },
178
+ "file_extension": ".py",
179
+ "mimetype": "text/x-python",
180
+ "name": "python",
181
+ "nbconvert_exporter": "python",
182
+ "pygments_lexer": "ipython3",
183
+ "version": "3.11.9"
184
+ }
185
+ },
186
+ "nbformat": 4,
187
+ "nbformat_minor": 2
188
+ }
notebooks/parse_description_test.ipynb CHANGED
@@ -90,7 +90,7 @@
90
  },
91
  {
92
  "cell_type": "code",
93
- "execution_count": 14,
94
  "metadata": {},
95
  "outputs": [
96
  {
@@ -295,7 +295,7 @@
295
  "[495 rows x 7 columns]"
296
  ]
297
  },
298
- "execution_count": 14,
299
  "metadata": {},
300
  "output_type": "execute_result"
301
  }
 
90
  },
91
  {
92
  "cell_type": "code",
93
+ "execution_count": 5,
94
  "metadata": {},
95
  "outputs": [
96
  {
 
295
  "[495 rows x 7 columns]"
296
  ]
297
  },
298
+ "execution_count": 5,
299
  "metadata": {},
300
  "output_type": "execute_result"
301
  }
utils/google_mongo_jobs.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from multiprocessing import process
2
+ import pandas as pd
3
+ import datetime as dt
4
+ import http.client
5
+ import json
6
+ import urllib.parse
7
+ import os
8
+ from pymongo import MongoClient
9
+ from concurrent.futures import ThreadPoolExecutor, as_completed
10
+
11
+ from dotenv import load_dotenv
12
+ load_dotenv()
13
+
14
+ mongodb_conn = os.getenv('MONGODB_CONNECTION_STRING')
15
+
16
+ # Global variables to keep track of searched job titles and cities
17
+ searched_jobs = set()
18
+ searched_cities = set()
19
+
20
+ def google_job_search(job_title, city_state, start=0):
21
+ '''
22
+ job_title(str): "Data Scientist", "Data Analyst"
23
+ city_state(str): "Denver, CO"
24
+ '''
25
+ query = f"{job_title} {city_state}"
26
+ params = {
27
+ "api_key": os.getenv('WEBSCRAPING_API_KEY'),
28
+ "engine": "google_jobs",
29
+ "q": query,
30
+ "hl": "en",
31
+ # "google_domain": "google.com",
32
+ # "start": start,
33
+ # "chips": f"date_posted:{post_age}",
34
+ }
35
+
36
+ query_string = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
37
+
38
+ conn = http.client.HTTPSConnection("serpapi.webscrapingapi.com")
39
+ try:
40
+ conn.request("GET", f"/v1?{query_string}")
41
+ print(f"GET /v1?{query_string}")
42
+ res = conn.getresponse()
43
+ try:
44
+ data = res.read()
45
+ finally:
46
+ res.close()
47
+ finally:
48
+ conn.close()
49
+
50
+ try:
51
+ json_data = json.loads(data.decode("utf-8"))
52
+ jobs_results = json_data['google_jobs_results']
53
+ return jobs_results
54
+ except (KeyError, json.JSONDecodeError) as e:
55
+ print(f"Error occurred for search: {job_title} in {city_state}")
56
+ print(f"Error message: {str(e)}")
57
+ print(f"Data: {data}")
58
+ return None
59
+
60
+ def mongo_dump(jobs_results, collection_name):
61
+ client = MongoClient(mongodb_conn)
62
+ db = client.job_search_db
63
+ collection = db[collection_name]
64
+
65
+ for job in jobs_results:
66
+ job['retrieve_date'] = dt.datetime.today().strftime('%Y-%m-%d')
67
+ collection.insert_one(job)
68
+
69
+ print(f"Dumped {len(jobs_results)} documents to MongoDB collection {collection_name}")
70
+
71
+ def process_batch(job, city_state, start=0):
72
+ global searched_jobs, searched_cities
73
+
74
+ # Check if the job title and city have already been searched
75
+ if (job, city_state) in searched_jobs:
76
+ print(f'Skipping already searched job: {job} in {city_state}')
77
+ return
78
+
79
+ jobs_results = google_job_search(job, city_state, start)
80
+ if jobs_results is not None:
81
+ print(f'City: {city_state} Job: {job} Start: {start}')
82
+ mongo_dump(jobs_results, 'sf_bay_test_jobs')
83
+
84
+ # Add the job title and city to the searched sets
85
+ searched_jobs.add((job, city_state))
86
+ searched_cities.add(city_state)
87
+
88
+ def main(job_list, city_state_list):
89
+ for job in job_list:
90
+ for city_state in city_state_list:
91
+ output = process_batch(job, city_state)
92
+
93
+ if __name__ == "__main__":
94
+ job_list = ["Data Scientist", "Machine Learning Engineer", "AI Gen Engineer", "ML Ops"]
95
+ city_state_list = ["Atlanta, GA", "Austin, TX", "Boston, MA", "Chicago, IL",
96
+ "Denver CO", "Dallas-Ft. Worth, TX", "Los Angeles, CA",
97
+ "New York City NY", "San Francisco, CA", "Seattle, WA",
98
+ "Palo Alto CA", "Mountain View CA", "San Jose, CA"]
99
+ simple_city_state_list: list[str] = ["Palo Alto CA", "San Francisco CA", "Mountain View CA"]
100
+ main(job_list, simple_city_state_list)