-
Notifications
You must be signed in to change notification settings - Fork 1
/
WuzzufDatabaseConversion.py
163 lines (136 loc) · 5.14 KB
/
WuzzufDatabaseConversion.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
###############################################################################################################
# This code aims to help update the database by saving old database files into a new one
#
###############################################################################################################
# open the sqlite and set the connection on the database
import sqlite3
# in sqlite the only option to convert table is to rename temporary table, create new table, and then drop old table
#SCHEMA FOR THE RELEVANT TABLES
querycreate = {}
querycreate['urltable']='''CREATE TABLE IF NOT EXISTS urltable
(uniqueid INTEGER,
urls VARCHAR(200),
urlpostdatetime VARCHAR(100),
postdate DATE,
PRIMARY KEY(uniqueid,postdate));'''
querycreate['pagedata']='''CREATE TABLE IF NOT EXISTS pagedata (
uniqueid INTEGER,
postdate DATE,
posttime VARCHAR(5),
downloaddate DATE,
downloadtime VARCHAR(5),
stat VARCHAR(10),
jobtitle VARCHAR(50),
company VARCHAR(50),
location VARCHAR(50),
num_applicants INTEGER,
num_vacancies INTEGER,
num_seen INTEGER,
num_shortlisted INTEGER,
num_rejected INTEGER,
experience_needed VARCHAR(50),
career_level VARCHAR(50),
job_type VARCHAR(50),
salary VARCHAR(50),
education_level VARCHAR(50),
gender VARCHAR(10),
travel_frequency VARCHAR(20),
languages VARCHAR(30),
vacancies VARCHAR(15),
roles VARCHAR(300),
keywords VARCHAR(100),
requirements VARCHAR(5000),
industries VARCHAR(100),
PRIMARY KEY(uniqueid,postdate,downloaddate));
'''
querycreate['archivedpagedata'] = '''CREATE TABLE IF NOT EXISTS archivedpagedata (
uniqueid INTEGER,
postdate DATE,
posttime VARCHAR(5),
downloaddate DATE,
downloadtime VARCHAR(5),
stat VARCHAR(10),
jobtitle VARCHAR(50),
company VARCHAR(50),
location VARCHAR(50),
num_applicants INTEGER,
num_vacancies INTEGER,
num_seen INTEGER,
num_shortlisted INTEGER,
num_rejected INTEGER,
experience_needed VARCHAR(50),
career_level VARCHAR(50),
job_type VARCHAR(50),
salary VARCHAR(50),
education_level VARCHAR(50),
gender VARCHAR(10),
travel_frequency VARCHAR(20),
languages VARCHAR(30),
vacancies VARCHAR(15),
roles VARCHAR(300),
keywords VARCHAR(100),
requirements VARCHAR(5000),
industries VARCHAR(100),
PRIMARY KEY(uniqueid,postdate,downloaddate));
'''
tables = ['urltable','pagedata','archivedpagedata']
#Report standard statistics and counts of the data that is in each of the tables
def report_statistics():
conn = sqlite3.connect("wuzzuf.db")
c = conn.cursor()
for i, t in enumerate(tables):
query ='''SELECT * FROM {};'''
temp = c.execute(query.format(t)).fetchall()
print("Number of observations in table {}: {}".format(t,len(temp)))
query ='''PRAGMA table_info({});'''
schema = c.execute(query.format(t)).fetchall()
print(schema)
if t == "urltable":
query = '''SELECT MAX(DATE(postdate)) FROM {};'''.format(t)
else:
query = '''SELECT MAX(DATE(downloaddate)) FROM {};'''.format(t)
print(c.execute(query).fetchall())
lastdate = c.execute(query).fetchall()[0][0]
print("Last date to query: {}".format(lastdate))
#Check the most recent data and report counts, means for each of the data points in the dataset
for j, row in enumerate(schema):
var = row[1]
if t == "jobadpageurls":
querystats = '''SELECT DATE(postdate), AVG({}), COUNT({}) FROM {} GROUP BY DATE(postdate);'''.format(var,var,t,lastdate)
else:
querystats = '''SELECT DATE(downloaddate), AVG({}), COUNT({}) FROM {} GROUP BY DATE(downloaddate);'''.format(var,var,t,lastdate)
print(querystats)
queryresults = {}
print("{}: {}".format(var,c.execute(querystats).fetchall()))
print("\n")
c.close()
#function resets all of the tables
def reset_tables():
for i, t in enumerate(tables):
deletequery = '''DROP TABLE IF EXISTS {};'''
c.execute(deletequery.format(table[i]))
c.execute(querycreate[t])
query ='''PRAGMA table_info({});'''
print(c.execute(query.format(table[i])).fetchall())
conn.commit()
conn.close()
def update_table(tablename,newtablequery):
conn = sqlite3.connect("wuzzuf.db")
c = conn.cursor()
query = "ALTER TABLE {} RENAME TO {}_temp;"
c.execute(query.format(tablename,tablename))
c.execute(newtablequery)
query = "INSERT INTO {} SELECT * FROM {}_temp;"
c.execute(query.format(tablename,tablename))
query ='''PRAGMA table_info({});'''
print(c.execute(query.format(tablename)).fetchall())
query = '''DROP TABLE IF EXISTS {}_temp;'''
c.execute(query.format(tablename))
temp = c.execute('''SELECT * FROM {};'''.format(tablename)).fetchall()
print("Number of entries in converted table {}: {}".format(tablename,len(temp)))
conn.commit()
conn.close()
# Comment or uncomment as needed
reset_tables()
# report_statistics()
# update_table()