forked from RTCedu/CNA330
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobhunter.py
More file actions
139 lines (127 loc) · 6.94 KB
/
Copy pathJobhunter.py
File metadata and controls
139 lines (127 loc) · 6.94 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
# This script pulls from a job website and stores positions into a database. If there is a new posting it notifies the user.
# Fall 2019 CNA 330
# Alan Abdullah, aaabdullah@student.rtc.edu
# Sources: Robin Cunanan,Kevin Huynh (AT&T Dev), rest of sources can be found in README and throughout the code
import mysql.connector
import json
import urllib.request
import time
from datetime import datetime
# import os, not needed
# import sys, not needed
# Connect to database
# You may need to edit the connect function based on your local settings.
def connect_to_sql():
conn = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='cna330')
return conn
# Create the table structure
def create_tables(cursor, table):
# Creates table
# Must set Description to CHARSET utf8 unicode Source: http://mysql.rjweb.org/doc.php/charcoll
# Python is in latin-1 and error (Incorrect string value: '\xE2\x80\xAFAbi...') will occur if Description is not in unicode format due to the json data
cursor.execute('''CREATE TABLE IF NOT EXISTS jobs (id INT PRIMARY KEY auto_increment, Type varchar(10), Title varchar(100), Description text CHARSET utf8,
Job_id varchar(50), Created_at DATE, Company varchar(100), Location varchar(50), How_to_apply varchar(300)); ''')
return
# Query the database.
# You should not need to edit anything in this function
def query_sql(cursor, query):
cursor.execute(query)
return cursor
# Add a new job
def add_new_job(cursor, jobdetails):
# extract all required columns
type = jobdetails['type']
created_at = time.strptime(jobdetails['created_at'], "%a %b %d %H:%M:%S %Z %Y") # https://www.programiz.com/python-programming/datetime/strftime & https://docs.python.org/3/library/datetime.html
company = jobdetails['company']
location = jobdetails['location']
title = jobdetails['title']
description = jobdetails['description']
how_to_apply = jobdetails['how_to_apply']
job_id = jobdetails['id']
query = cursor.execute("INSERT INTO jobs(Type, Title, Description, Job_id, Created_at, Company, Location, How_to_apply" ") "
"VALUES(%s,%s,%s,%s,%s,%s,%s,%s)", (type, title, description, job_id, created_at, company, location, how_to_apply)) # https://stackoverflow.com/questions/20818155/not-all-parameters-were-used-in-the-sql-statement-python-mysql/20818201#20818201
return query_sql(cursor, query)
# Check if new job
def check_if_job_exists(cursor, jobdetails):
job_id = jobdetails['id']
query = "SELECT * FROM jobs WHERE Job_id = \"%s\"" % job_id # Help from Kevin Huynh
return query_sql(cursor, query)
# Deletes job
def delete_job(cursor, jobdetails):
job_id = jobdetails['id']
query = "DELETE FROM jobs WHERE Job_id = \"%s\"" % job_id # Help from Kevin Huynh & https://www.tutorialspoint.com/mysql/mysql-delete-query.htm
return query_sql(cursor, query)
# Grab new jobs from a website, Parses JSON code and inserts the data into a list of dictionaries
def fetch_new_jobs(arg_dict):
# Code from https://github.com/RTCedu/CNA336/blob/master/Spring2018/Sql.py
query = "https://jobs.github.com/positions.json?location=seattle" # "https://jobs.github.com/positions.json?" + "location=seattle" ## Add arguments here #Use & after seattle to do &description=python&full_time=no this is how to chain
jsonpage = 0
try:
contents = urllib.request.urlopen(query)
response = contents.read() # Loads from config file
jsonpage = json.loads(response) # checks database, any jobs that find
except:
pass
return jsonpage
# Load a text-based configuration file, not function needed per Zak
"""def load_config_file(filename):
argument_dictionary = 0
# Code from https://github.com/RTCedu/CNA336/blob/master/Spring2018/FileIO.py
rel_path = os.path.abspath(os.path.dirname(__file__))
file = 0
file_contents = 0
try:
file = open(filename, "r")
file_contents = file.read()
except FileNotFoundError:
print("File not found, it will be created.")
file = open(filename, "w")
file.write("")
file.close()
## Add in information for argument dictionary
return argument_dictionary"""
# Main area of the code.
def jobhunt(arg_dict, cursor):
# Fetch jobs from website
jobpage = fetch_new_jobs(arg_dict) # Gets github website and holds the json data in it as a list
# use below print statement to view list in json format
# print(jobpage)
add_or_delete_job(jobpage, cursor)
def add_or_delete_job(jobpage, cursor):
# Add your code here to parse the job page
for jobdetails in jobpage: # EXTRACTS EACH JOB FROM THE JOB LIST
# Add in your code here to check if the job already exists in the DB
check_if_job_exists(cursor, jobdetails)
is_job_found = len(cursor.fetchall()) > 0 # https://stackoverflow.com/questions/2511679/python-number-of-rows-affected-by-cursor-executeselect
if is_job_found: # Help from Kevin Huynh
# DELETE JOB
# EXTRA CREDIT: Add your code to delete old entries
now = datetime.now()
job_date = datetime.strptime(jobdetails['created_at'], "%a %b %d %H:%M:%S %Z %Y")
if (now - job_date).days > 30: # https://stackoverflow.com/questions/46563442/check-if-dates-on-a-list-are-older-than-2-days
print("Delete job: " + jobdetails["title"] + " from " + jobdetails["company"] + ", Created at: " + jobdetails["created_at"] + ", JobID: " + jobdetails['id'])
delete_job(cursor, jobdetails)
else:
# INSERT JOB
# Add in your code here to notify the user of a new posting
print("New job is found: " + jobdetails["title"] + " from " + jobdetails["company"] + ", Created at: " + jobdetails["created_at"] + ", JobID: " + jobdetails['id'])
add_new_job(cursor, jobdetails)
# Setup portion of the program. Take arguments and set up the script
# You should not need to edit anything here.
def main():
# Important, rest are supporting functions
# Connect to SQL and get cursor
conn = connect_to_sql()
cursor = conn.cursor()
create_tables(cursor, "table")
# Load text file and store arguments into dictionary
arg_dict = 0
while(1): # Infinite Loops. Only way to kill it is to crash or manually crash it. We did this as a background process/passive scraper
jobhunt(arg_dict, cursor) # arg_dict is argument dictionary,
time.sleep(3600) # Sleep for 1h, this is ran every hour because API or web interfaces have request limits. Your reqest will get blocked.
# Sleep does a rough cycle count, system is not entirely accurate
# If you want to test if script works change time.sleep() to 10 seconds and delete your table in MySQL
if __name__ == '__main__':
main()