-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
138 lines (119 loc) · 4.27 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
from flask import Flask
from flask import render_template
from predict_text_meaning import predict,load_map,load_model,load_data
from flask import *
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import os
import re
from pyvirtualdisplay import Display
from requests import get
from bs4 import BeautifulSoup
from flask_caching import Cache
app = Flask(__name__)
o=""
#@app.route('/')
# def index():
# return render_template('index.html',pred=predict)
# @app.route('/pred', methods=['POST'])
# def pred():
# data=request.form['inp'];
# return predict(data)
def scrap_image(word):
path = str(os.popen("pwd").read()).strip("\n") + "/geckodriver" #/home/abin/PycharmProjects/Essence/geckodriver
print('geck',path)
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Firefox(executable_path=path) # /usr/local/bin/geckodriver
word = word.encode('utf-8').decode('utf-8')
word = word.replace(" ", "+")
driver.get(
"https://www.google.com/search?safe=active&tbm=isch&source=hp&biw=1366&bih=638&ei=LHLZW5_pIMye9QOAz6_YCg&q="+word+"&oq=special+&gs_l=img.1.0.35i39k1j0l9.1980.4399.0.5731.9.9.0.0.0.0.181.1041.0j7.7.0....0...1ac.1.64.img..2.7.1038.0...0.vIIm7tH2ToI"
)
try:
html = driver.find_element_by_tag_name("html").get_attribute("innerHTML")
driver.close()
display.stop()
return html
# org = []
# for w in l:
# org.append(w.replace('\u200c', '').replace('\u200d', ''))
# return org
except WebDriverException as e:
driver.close()
display.stop()
return -1
def scrap_search(word):
path = str(os.popen("pwd").read()).strip("\n") + "/geckodriver"
print('geck', path)
display = Display(visible=0, size=(800, 600))
display.start()
#driver=None
driver = webdriver.Firefox(executable_path=path) # /usr/local/bin/geckodriver
#driver=webdriver.Chrome(executable_path=path)
word = word.encode('utf-8').decode('utf-8')
word=word.replace(" ","+")
driver.get( "https://www.google.co.in/search?q="+word+"&oq="+word+"&aqs=chrome.0.69i59.8573j1j8&sourceid=chrome&ie=UTF-8")
try:
print('result found')
html = driver.find_element_by_tag_name("html").get_attribute("innerHTML")
driver.close()
display.stop()
print('result found')
return html
# org = []
# for w in l:
# org.append(w.replace('\u200c', '').replace('\u200d', ''))
# return org
except WebDriverException as e:
driver.close()
display.stop()
print('error no result')
return -1
# def scrap_by_bs(word):
# url = "https://www.google.co.in/search?q="+word+"&oq="+word+"&aqs=chrome.0.69i59.8573j1j8&sourceid=chrome&ie=UTF-8"
#
# response = get(url)
# html_soup = BeautifulSoup(response.text, 'html.parser')
# containers = html_soup.find_all('html')
# return str(containers[0])
@app.route('/')
def first():
return render_template('Search.html')#predict.html
@app.route('/req', methods=['POST'])
def req():
inp = request.form['inp'];
if(len(inp)>0):
pr=predict([inp.strip()],model,word_map,data_train)
if(pr=='അത്'):
pr=inp.strip()
html=scrap_search(pr)
if(html==-1):
out = json.dumps({'status': 'NOTOK', 'suggestion': '', 'result': ''});
else:
# print(html)
out= json.dumps({'status':'OK','suggestion':pr,'result':html});
return out
else:
print("no inp")
return json.dumps({'status':'OK','suggestion':''});
@app.route('/images' ,methods=['POST'])
def image():
pr=request.form['inp']
print(pr)
html = scrap_image(pr)
if (html == -1):
out = json.dumps({'status': 'NOTOK', 'suggestion': '', 'result': ''});
else:
# print(html)
out = json.dumps({'status': 'OK', 'suggestion': pr, 'result': html});
return out
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == '__main__':
model=load_model()
word_map=load_map()
data_train=load_data()
app.run(debug=True)