-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMain.py
300 lines (229 loc) · 9.13 KB
/
Main.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
import re
from flask import Flask, render_template, request, redirect, session, url_for
from flask_sqlalchemy import SQLAlchemy
from bs4 import BeautifulSoup as soup
#from urllib2 import Request, urlopen as uReq as syntax got changed in python3
from urllib.request import urlopen as uReq
import tweepy
import pandas as pd
#to hash the password and to check the password
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
app.config['SECRET_KEY'] = 'the random string'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(50))
password = db.Column(db.String(50))
def scrapedata(data):
print("entered scrapedata fn")
try:
my_url = 'https://www.goodguide.com/products?filter=' + data
print(my_url)
uClient = uReq(my_url)
page_soup = soup(uClient.read(), "html.parser")
uClient.close()
data=page_soup.findAll("a", {"class": "entity-link"})[0:8]
#print(data)
name=[]
link=[]
for i in data:
name.append(i.get('title'))
link.append(i.get('href'))
return (name,link)
except:
print("Error opening the URL")
def ScrapeResult(data):
print("entered ScrapeResult fn")
try:
my_url = 'https://www.goodguide.com' + data
print(my_url)
uClient = uReq(my_url)
page_soup = soup(uClient.read(), "html.parser")
uClient.close()
title = page_soup.find("h1")
print(title.text)
imgParent=page_soup.find("p", {"class": "text-center product-highlight-image"})
img= imgParent.find("img")
i=img['src']
scoreParent = page_soup.find("p", {"class": "ring-value number"})
score = scoreParent.find("a")
print(score.text)
contentParent = page_soup.find("p", {"class": "rating-explained-ingredient-count number high"})
HighHazardConcern = contentParent.find("a")
print(HighHazardConcern.text)
contentParent2 = page_soup.find("p", {"class": "rating-explained-ingredient-count number medium"})
MediumHazardConcern = contentParent2.find("a")
print(MediumHazardConcern.text)
contentParent3 = page_soup.find("p", {"class": "rating-explained-ingredient-count number low"})
LowHazardConcern = contentParent3.find("a")
print(LowHazardConcern.text)
print("END")
return (title.text, i,score.text,HighHazardConcern.text ,MediumHazardConcern.text,LowHazardConcern.text)
except:
print("Error opening the URL: Error in scrape result")
################################################ Price Scrapper ##############################
def PriceNameSuggestion(name):
print("entered Price name suggestion fn")
try:
my_url = 'http://scandid.in/search?q='+name+'&type=products'
print(my_url)
uClient = uReq(my_url)
page_soup = soup(uClient.read(), "html.parser")
uClient.close()
data = page_soup.findAll("a", {"class": "ellipsis multiline"})[0:8]
name=[]
link=[]
for i in data:
name.append(i.text)
link.append('http://scandid.in/'+i['href'])
print("name is ", name )
print("link is ", link)
return (name,link)
except:
print("Error opening the URL")
def ScrapePrice(data):
url= data
print(url)
#uClient = uReq(url)
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
print("start flipkart")
FinalResult=[]
p = page_soup.find("span", {"id": "best_price"})
FinalResult.append(p.text)
seller=page_soup.find("span", {"class": "btn-span"})
FinalResult.append(seller.text)
print("found result")
return FinalResult
########################### Sentimental Analysis ########################################
def sentimentalAnalysis(review):
print('sa')
try:
from textblob import TextBlob
consumer_key = "Your consmer key "
consumer_secret = "Your consumer secret key"
access_token = "Your acess token"
access_token_secret = "Your access token secret"
def twitter():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
tw = twitter()
search = tw.user_timeline(screen_name="TheEllenShow", count=200, lang="en")
# Printing last 10 tweets
print("10 recent tweets:\n")
for tweets in search[:10]:
print(tweets.text + '\n')
df = pd.DataFrame([tweets.text for tweets in search], columns=['Tweets'])
df.head(10)
polarity = lambda x: TextBlob(x).sentiment.polarity
subjectivity = lambda x: TextBlob(x).sentiment.subjectivity
df['polarity'] = df['Tweets'].apply(polarity)
df['subjectivity'] = df['Tweets'].apply(subjectivity)
except:
print("invalid credientials")
################################## ROUTES ###################################
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
email = request.form['email']
password = request.form['password']
#query based on email and to check whether the password entered by the user is correct or not
data = User.query.filter_by(email=email).first()
if data is not None and check_password_hash(data.password, password):
session['user'] = data.id
print(session['user'])
return redirect(url_for('home'))
return render_template('incorrect_login.html')
@app.route('/', methods=['GET', 'POST'])
@app.route('/register/', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
new_user = User(email=request.form['email'],
password=generate_password_hash(request.form['password'],method='sha256'))
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/logout', methods=['GET', 'POST'])
def logout():
session.pop('email', None)
return redirect(url_for('login'))
#############################################################################
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/show')
def show():
show_user = User.query.all()
return render_template('show.html', show_user=show_user)
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
product = request.form['product']
print("prodict is ", product)
try:
print("fn call")
suggestions,link= scrapedata(product)
return render_template('suggestions.html', suggestions=suggestions, link=link)
except:
return render_template('404.html', display_content='Invalid symbol. No quote available' )
else:
# GET method
return render_template('search.html')
@app.route('/result', methods=['GET', 'POST'])
def result():
result = request.args
data=result.get('forwardBtn')
name,img, score,Hconcern, Mconvern, lconcern= ScrapeResult(data)
return render_template('result.html', name=name, img=img, score=score,Hconcern=Hconcern, Mconvern=Mconvern, lconcern=lconcern)
@app.route('/price', methods=['GET', 'POST'])
def price():
if request.method == 'POST':
price = request.form['price']
print("prodict is ", price)
try:
print("price call")
Listname,link=PriceNameSuggestion(price)
print("Inside 123 : Listname",Listname,link)
return render_template('PriceSuggestion.html', l=Listname, link=link)
except:
return render_template('404.html', display_content='Invalid symbol. No quote available' )
else:
# GET method
return render_template('price.html')
@app.route('/reviews', methods=['GET', 'POST'])
def reviews():
if request.method == 'POST':
review = request.form['review']
try:
review= sentimentalAnalysis(review)
return render_template('Reviews.html', reviews=reviews)
except:
return render_template('404.html', display_content='Invalid credientials' )
else:
# GET method
return render_template('Reviews.html')
@app.route('/priceresult', methods=['GET', 'POST'])
def priceresult():
result = request.args
data=result.get('forwardBtn')
print(data)
price,seller= ScrapePrice(data)
return render_template('showLowPrice.html', price=price, seller= seller)
@app.route('/laws')
def laws():
return render_template('laws.html')
#############################################################################
if __name__ == '__main__':
#Added secret key that the top app.secret_key = 'super secret key'
db.create_all()
app.run(debug=True)