-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawlRepo.py
More file actions
210 lines (195 loc) · 8.71 KB
/
crawlRepo.py
File metadata and controls
210 lines (195 loc) · 8.71 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from config import *
import mysql.connector
class crawlRepo:
def __init__(self):
True
def insertEntity(self, entity, search_input, status=status_in_queue):
query = 'INSERT INTO place(category,image,geoPoint,title,subtitle,' \
'phone,website,hours,score,address_en,address_fa,lastCrawlDate,numberOfTry,searchInput,ProfileShahrestanID,status)' \
'VALUES("{category}","{image}","{geoPoint}","{title}","{subtitle}","{phone}",' \
'"{website}","{hours}","{score}","{address_en}","{address_fa}",NOW(),1,"{searchInput}",{ProfileShahrestanID},{status}) ' \
'ON DUPLICATE KEY ' \
'UPDATE ' \
'category="{category}",image="{image}",geoPoint="{geoPoint}",title="{title}",subtitle="{subtitle}",phone="{phone}"' \
',website="{website}",hours="{hours}",score="{score}",address_en="{address_en}",ProfileShahrestanID={ProfileShahrestanID},status={status},' \
'address_fa="{address_fa}",lastCrawlDate=NOW(),numberOfTry=numberOfTry+1;' \
.format(category=entity['category'], image=entity['image'], geoPoint=entity['geo_point'].strip(),
title=entity['title'], subtitle=entity['subtitle'], phone=entity['phone'],
website=entity['website'], hours=entity['hours'], score=entity['score'],
address_en=entity['address_en'], address_fa=entity['address_fa'], searchInput=search_input,
ProfileShahrestanID=entity['ProfileShahrestanID'], status=status)
return self.setResult(query)
def checkEntityExist(self, entity):
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 place WHERE geoPoint = "{geoPoint}"'.format(geoPoint=entity['geo_point'].strip())
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 checkTitleExist(self, title, profileShahrestanID, score=None):
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:
title = title.strip()
query = 'SELECT ID FROM place WHERE (title = "{title}" OR subtitle="{subtitle}") AND profileShahrestanID="{profileShahrestanID}"'.format(
title=title,
subtitle=title, profileShahrestanID=profileShahrestanID)
if score:
query += 'AND score="{score}"'.format(score=score)
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 insertPointQueue(self, geoPoint, search_input):
query = 'INSERT INTO pointsqueue ' \
'(geoPoint,search_input,status,lastCrawlDate,numberOfTry)' \
'VALUES' \
'("{geoPoint}","{searchInput}",1,NOW() ,1)' \
'ON DUPLICATE KEY ' \
'UPDATE ' \
'lastCrawlDate=NOW(),numberOfTry=numberOfTry+1;' \
.format(geoPoint=geoPoint.strip(), searchInput=search_input)
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 checkGeoInTownship(self, geoPoint, profileShahrestanID):
townshipID = self.getTownship(geoPoint)
if townshipID:
if int(townshipID) == profileShahrestanID:
return True
else:
return False
else:
return False
def getTownship(self, geoPoint):
import requests
import json
url = find_gps_point_service + '?gps_point=' + geoPoint
res = requests.get(url)
if res.status_code == 200:
response = json.loads(res.content)
return response['data']['township_id']
else:
logging.error("Failed to call service: {}".format(res.content))
return False
def getCategories(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 onvan FROM profileghalebsakhtarmajmooeh'
cursor.execute(query)
if cursor.rowcount == 0:
response = False # Not Exist
else:
response = [item[0] for item in cursor.fetchall()] # query List
except mysql.connector.Error as err:
logging.error("Failed get Categories: {}".format(err))
response = 'Error'
finally:
cursor.close()
cnx.close()
return response
def checkCategoryExistForShahrestan(self, category, profileShahrestanID):
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 place WHERE ProfileShahrestanID={ProfileShahrestanID} AND searchInput="{searchInput}"'.format(
searchInput=category.strip(), ProfileShahrestanID=profileShahrestanID)
cursor.execute(query)
if cursor.rowcount == 0:
response = False # Not Exist
else:
response = True # query List
except mysql.connector.Error as err:
logging.error("Failed get Categories: {}".format(err))
response = 'Error'
finally:
cursor.close()
cnx.close()
return response
def getSearchPoints(self, category, profileShahrestanID):
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 geoPoint FROM place WHERE ProfileShahrestanID={ProfileShahrestanID} AND searchInput="{searchInput}" AND status != {status} ORDER BY ID ASC'.format(
searchInput=category.strip(), ProfileShahrestanID=profileShahrestanID, status=status_did_progress)
cursor.execute(query)
if cursor.rowcount == 0:
response = False # Not Exist
else:
response = [item[0] for item in cursor.fetchall()] # query List
except mysql.connector.Error as err:
logging.error("Failed get Categories: {}".format(err))
response = 'Error'
finally:
cursor.close()
cnx.close()
return response
def updatePointStatus(self, geoPoint, status):
query = 'UPDATE place SET status={status},numberOfTry=numberOfTry+1 where geoPoint="{geoPoint}"'.format(
status=status, geoPoint=geoPoint)
return self.setResult(query)
def pointInProgress(self, geoPoint):
return self.updatePointStatus(geoPoint, status_in_progress)
def pointDidProgress(self, geoPoint):
return self.updatePointStatus(geoPoint, status_did_progress)