-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
327 lines (247 loc) · 9.38 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import chromedriver
from search_controller import search_bp
import atexit
from selenium import webdriver
from flask import Flask, render_template, request, redirect, url_for, flash, session
import config
from logger import logger
from flask_cors import CORS
import mysql.connector
import time
logger.info("\n ############################### \n")
logger.info("Starting program")
logger.info("Logger initialized")
logger.info("Loading Configuration")
logger.info("Logger append : " + str(config.logger_append))
logger.info("Logger console print : " + str(config.logger_console_print))
logger.info("Logger tail : " + str(config.logger_tail))
logger.info("Environment OS : " + str(config.env_os))
logger.info("ChromeDriver path : \"" + str(config.chromedriver_path) + "\"")
logger.info("ChromeDriver mode : " + str(config.chromedriver_mode))
logger.info("ChromeDriver pool : " + str(config.chromedriver_pool_size))
logger.info("ChromeDriver timeout : " + str(config.chromedriver_timeout))
logger.info("Debug mode : " + str(config.debug_mode))
logger.info("Use reloader : " + str(config.use_reloader))
logger.info("App port : " + str(config.app_port))
logger.info("Threaded : " + str(config.threaded))
# from flask_mysqldb import MySQL
# from flask_mail import Mail, Message
logger.info("Imported all modules")
logger.info("Imported In-House modules")
app = Flask(__name__)
# CORS(app)
if (config.cors == True):
CORS(app, resources={r"/api/*": {"origins": config.cors_destination}})
#CORS(app, resources={r"/api/*": {"origins": "https://pro-scraper.vercel.app"}})
db = mysql.connector.connect(
host=config.database_host,
user=config.database_user,
password=config.database_password)
cursor = db.cursor(buffered=True)
sql = "CREATE DATABASE IF NOT EXISTS {s0}".format(s0=config.database_name)
cursor.execute(sql)
cursor.close()
db.close()
logger.info("MySQL database created")
db = mysql.connector.connect(
host=config.database_host,
user=config.database_user,
password=config.database_password,
database=config.database_name )
cursor = db.cursor(buffered=True)
logger.info("MySQL cursor created")
"""
{
"title": "Apple iPhone 13 (128GB, Midnight)",
"tags": ["Apple", "iPhone", "13", "128GB,", "Midnight"],
"market": "Reliance Digital",
"price": "54900.00",
"rating": {
"reviews": "8,723",
"score": "4.6"
},
"in_stock": "",
"shipping": "Free delivery by 3 Oct and free 5-day returns",
"verified": false,
"price_description": "",
"additional_info": "Smartphone Dual SIM 5G",
"img": "https://encrypted-tbn2.gstatic.com/shopping?q=tbn:ANd9GcSdbMcu_sYPXLZcOuSnCAAleUSFoclkU1qgAQmPdw_fDIGB7gFF4bHInfXkp9pt6e5l3lxuK2RVAQjVI2G3ji5KRMBR1XMJLfCUjeuQDB__&usqp=CAE",
"links": [
"https://www.google.com/url?url=https://www.reliancedigital.in/apple-iphone-13-128-gb-midnight-black-/p/491997699%3Fsrsltid%3DAfmBOopVnn4-1tzEQMo89wA9KWyO_OtVL7yHKBRSacTmY-TW_Esr82pJJ0I&rct=j&q=&esrc=s&opi=95576897&sa=U&ved=0ahUKEwiHnsSA68iBAxUcSmwGHUXJAxwQguUECMcM&usg=AOvVaw0NI9j7lMJZ6KwAeDoH_8N_",
"https://www.google.com/shopping/product/10864203085039380343?q=iphone+13&prds=eto:10603666471394521545_0,pid:7634124128593707086,rsk:PC_4079606304177687932&sa=X&ved=0ahUKEwiHnsSA68iBAxUcSmwGHUXJAxwQ8gIIvwwoAA",
"https://www.google.com/shopping/product/10864203085039380343/offers?q=iphone+13&prds=eto:10603666471394521545_0,pid:7634124128593707086,rsk:PC_4079606304177687932&sa=X&ved=0ahUKEwiHnsSA68iBAxUcSmwGHUXJAxwQ3q4ECMoM"
],
"scraped": 1695750538.90776
}"""
sql = ("""CREATE TABLE IF NOT EXISTS products (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
hash VARCHAR(32) UNIQUE,
img VARCHAR(255),
market VARCHAR(255),
shipping VARCHAR(255),
price DECIMAL(10, 2),
other_price DECIMAL(10, 2),
reviews INT,
score DECIMAL(3, 1),
link LONGTEXT,
in_stock VARCHAR(255),
verified BOOLEAN,
tags VARCHAR(255),
additional_info VARCHAR(255),
price_description VARCHAR(255),
other_links LONGTEXT,
scraped DATETIME,
last_scraped DATETIME
)""")
cursor.execute(sql)
sql = ("""CREATE TABLE IF NOT EXISTS product_links (
id INT AUTO_INCREMENT PRIMARY KEY,
hash VARCHAR(32),
store VARCHAR(255),
link LONGTEXT,
scraped DATETIME,
last_scraped DATETIME,
price DECIMAL(10, 2),
other_price DECIMAL(10, 2),
delivery VARCHAR(255),
returns VARCHAR(255),
new_hash VARCHAR(32) UNIQUE
)""")
cursor.execute(sql)
sql = ("""CREATE TABLE IF NOT EXISTS product_reviews (
id INT AUTO_INCREMENT PRIMARY KEY,
hash VARCHAR(32) UNIQUE,
rating DECIMAL(3, 1),
reviews_link LONGTEXT,
reviews INT,
one_star INT,
two_star INT,
three_star INT,
four_star INT,
five_star INT,
tags LONGTEXT,
scraped DATETIME,
last_scraped DATETIME
)""")
cursor.execute(sql)
cursor.close()
db.close()
logger.info("MySQL tables created")
#app.secret_key = config.secret_key
#logger.info("Secret key set")
app.register_blueprint(search_bp)
logger.info("Registered Blueprints")
chromedriver.init_chromedriver_pool()
@app.route('/', methods=['GET'])
def index2():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
return render_template('main.html')
@app.route('/deep', methods=['GET'])
def index():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
return render_template('index.html')
@app.route('/quick', methods=['GET'])
def index_db_search():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
return render_template('searchdb.html')
@app.route('/product', methods=['GET'])
def index_product():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
return render_template('product.html')
@app.route('/reviews', methods=['GET'])
def index_reviews():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
return render_template('rev.html')
@app.route('/restart', methods=['GET'])
def restart():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
chromedriver.restart_all_chrome_drivers()
return "Restarted all ChromeDriver instances"
@app.route('/restart_db', methods=['GET'])
def restart_db():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.info("GET \"" + path + "\" " + str(ip))
db = mysql.connector.connect(
host=config.database_host,
user=config.database_user,
password=config.database_password,
database=config.database_name )
cursor = db.cursor(buffered=True)
logger.info("MySQL cursor created")
time.sleep(1)
cursor.close()
db.close()
logger.info("MySQL connection closed")
return "Restarted a connection"
"""
driver = chromedriver.get_chromedriver()
if driver:
try:
driver.get("https://www.google.com")
return driver.page_source
finally:
chromedriver.release_chromedriver(driver)
else:
logger.error("Failed to get chromedriver instance")
"""
@app.route('/searchProduct', methods=['POST'])
def searchProduct():
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
form_data = request.form.to_dict()
logger.info("POST \"" + path + "\" " + str(ip) + " " + str(form_data))
return "true"
@app.route("/productPage", methods=['GET'])
def productPage():
return "True"
@app.errorhandler(404)
def page_not_found(e):
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
path = request.path
logger.error("\""+path + "\" 404 " + str(ip) + " " + str(e))
return render_template('404.html'), 404
def cleanup():
logger.info("Cleaning up")
chromedriver.chrome_driver_queue.join()
chromedriver.close_chromedriver_pool()
logger.info("ChromeDriver pool closed")
logger.info("Exiting program")
atexit.register(cleanup)
if __name__ == "__main__":
debug_mode = config.debug_mode or False
app_port = config.app_port or 5000
use_reloader = config.use_reloader
threaded = config.threaded
app.run(debug=debug_mode, port=app_port)
#use_reloader=use_reloader, threaded=threaded)
# Pro Scraper | By Jesvi Jonathan
# / | Home | Deep Search Console
# /quick | Quick Search Console
# /restart | Restart all ChromeDriver instances
# /product | Product Console
# /reviews | Reviews Console
# /api/search | Deep Search API
# /api/quick_search | Quick Search API
# /api/deep_search | Deep Search API
# /api/product | Product API
# /api/product_quick | Product API
# /api/product_deep | Product API
# you can pass searchQuery as a GET parameter or as a POST parameter via form data or JSON data
# use searchQuery with search API
# exmaple : /api/search?searchQuery=iphone+13
# use product Hash with product API | you can get product hash from search resopnse
# exmaple : /api/product_quick?searchQuery=01e54b2ceb0307cfa650615282ec8239