-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawlRepo.py
195 lines (187 loc) · 7.04 KB
/
crawlRepo.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
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
from config import *
import mysql.connector
import json
class crawlRepo:
def __init__(self):
True
def insertEntity(self, entity, webSiteID):
query = '''INSERT INTO crawlContent
(
webSiteID
,title
,company
,pureContent
,location
,skills
,workType
,minExperience
,price
,tags
,link
,minDegree
,lastCrawlDate
,expirationDate
)
VALUES
(
{webSiteID}
,"{title}"
,"{company}"
,"{pureContent}"
,"{location}"
,'{skills}'
,'{workType}'
,"{minExperience}"
,"{price}"
,'{tags}'
,"{link}"
,"{minDegree}"
,NOW()
,"{expirationDate}"
)
ON DUPLICATE KEY
UPDATE
title="{title}",title="{title}",company="{company}",pureContent="{pureContent}",location="{location}",skills='{skills}',
workType='{workType}',minExperience="{minExperience}",price="{price}",tags='{tags}',link="{link}",minDegree="{minDegree}",lastCrawlDate=NOW(),
expirationDate="{expirationDate}"
;'''.format(webSiteID=webSiteID, title=entity['title'], company=entity['company'],
pureContent=entity['content'], location=entity['location'],
skills=json.dumps(entity['skills'], ensure_ascii=False),
workType=json.dumps(entity['work_type'], ensure_ascii=False),
minExperience=entity['minimum_experience'], price=entity['price'],
tags=json.dumps(entity['tags'], ensure_ascii=False), link=entity['url'],
minDegree=entity['minimum_degree'],expirationDate=entity['expiration_date'])
return self.setResult(query)
def setResult(self, query):
res = False
try:
cnx = mysql.connector.connect(**dbConfig)
cursor = cnx.cursor(buffered=True)
except mysql.connector.Error as err:
logging.error("Failed connect db: {}".format(err))
return res
try:
cursor.execute(query)
cnx.commit()
res = True
except mysql.connector.Error as err:
logging.error("Failed Insert Result member: {}".format(err))
res = False
finally:
cursor.close()
cnx.close()
return res
def checkExistRecord(self, entity, webSiteID):
response = False
try:
cnx = mysql.connector.connect(**dbConfig)
cursor = cnx.cursor(buffered=True)
except mysql.connector.Error as err:
logging.error("Failed connect db: {}".format(err))
return 'Error'
try:
query = '''
SELECT
ID
FROM crawlContent
WHERE link="{link}"
'''.format(link=entity['url'])
cursor.execute(query)
if cursor.rowcount == 0:
response = False # Not Exist
else:
response = True # Record Already Exist
except mysql.connector.Error as err:
logging.error("Failed check Exist: {}".format(err))
response = 'Error'
finally:
cursor.close()
cnx.close()
return response
def getRecordsBySkill(self, skill, paging_id=None, mode='next'):
response = False
try:
cnx = mysql.connector.connect(**dbConfig)
cursor = cnx.cursor(buffered=True)
except mysql.connector.Error as err:
logging.error("Failed connect db: {}".format(err))
return 'Error'
try:
query = 'SELECT * FROM crawlContent WHERE skills REGEXP "{skill}" AND expirationDate > NOW()'.format(skill=skill)
if paging_id:
query += ' AND expirationDate {mode} "{paging_id}"'.format(mode=paging_mode[mode], paging_id=paging_id)
query += ' ORDER BY expirationDate DESC'
cursor.execute(query)
if cursor.rowcount == 0:
response = False # Not Exist
else:
response = {
'feeds': [],
'is_next_page': False,
'next_max_id': False
}
records = cursor.fetchall()
if len(records) > paging_limit:
response['is_next_page'] = True
for item in records[:paging_limit]:
out = {
'ID': item[0],
'title': item[2],
'company': item[3],
'pureContent': item[4],
'location': item[5],
'skills': json.loads(item[6]),
'workType': json.loads(item[7]),
'minExperience': item[8],
'price': item[9],
'tags': json.loads(item[10]),
'link': item[11],
'minDegree': item[12],
'expirationDate': item[14],
}
response['feeds'].append(out)
if response['is_next_page']:
response['next_max_id'] = response['feeds'][-1]['expirationDate']
except mysql.connector.Error as err:
logging.error("Failed get Record: {}".format(err))
response = 'Error'
except Exception as err:
logging.error("Failed get Record: {}".format(err))
response = 'Error'
finally:
cursor.close()
cnx.close()
return response
def getRecordExpirationNull(self):
response = False
try:
cnx = mysql.connector.connect(**dbConfig)
cursor = cnx.cursor(buffered=True)
except mysql.connector.Error as err:
logging.error("Failed connect db: {}".format(err))
return 'Error'
try:
query = 'SELECT * FROM crawlContent WHERE expirationDate is NULL '
cursor.execute(query)
if cursor.rowcount == 0:
response = False # Not Exist
else:
response = []
records = cursor.fetchall()
for item in records:
out = {
'title': item[2],
'company': item[3],
'link': item[11],
}
response.append(out)
except mysql.connector.Error as err:
logging.error("Failed get Record: {}".format(err))
response = 'Error'
except Exception as err:
logging.error("Failed get Record: {}".format(err))
response = 'Error'
finally:
cursor.close()
cnx.close()
return response