-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
145 lines (120 loc) · 5.37 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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 09 19:09:53 2016
@author: RaoUmer
"""
from flask import Flask, render_template, request
import os
import requests
import formasaurus as ffd
import urllib
from urllib2 import Request, urlopen, URLError
app = Flask(__name__)
cur_dir = os.path.dirname(__file__)
class DictQuery(dict):
def get(self, path, default = None):
keys = path.split("/")
val = None
for key in keys:
if val:
if isinstance(val, list):
val = [ v.get(key, default) for v in val]
else:
val = val.get(key, default)
else:
val = dict.get(self, key, default)
if not val:
break;
return val
def formdetect(url):
fd = []
html = requests.get(url).text
fdetection = ffd.extract_forms(html)
for i in range(len(fdetection)):
for j in range(len(fdetection[i])):
fd.append(fdetection[i][j])
#fd = [{'fields': {'s': 'search query'}, 'form': u'search'}]
return fd
# Flask App
@app.route('/', methods=['GET','POST'])
def index():
return render_template('index.html')
@app.route('/search', methods=['GET','POST'])
def search():
return render_template('search.html')
@app.route('/contact', methods=['GET','POST'])
def contact():
return render_template('contact.html')
@app.route('/results', methods=['GET','POST'])
def results():
# url_list = ["http://autos.columnpk.net/", "http://driver.pk/"]
#url = "http://autos.columnpk.net/"
#url = "http://www.google.com/"
#url = "https://www.olx.com.pk/vehicles/"
#url = "http://autos.columnpk.net/"
url = "http://driver.pk/"
#url = "http://www.pkmotors.com/"
#url = "http://cardealer.com.pk/"
#url = "http://sastigari.com/search/cars/"
if request.method == 'POST':
fd = formdetect(url)
for item in fd:
if "fields" in item:
if DictQuery(item).get("fields/s") == 'search query' or DictQuery(item).get("fields/q") == 'search query' or DictQuery(item).get("fields/full_search") == 'search query' or DictQuery(item).get("fields/txtKeyword") == 'search query' or DictQuery(item).get("fields/cmbPMax") == 'search category / refinement' or DictQuery(item).get("fields/cmbPMin") == 'search category / refinement':
if DictQuery(item).get("fields/s"):
values = {'s' : str(request.form['query'])}
elif DictQuery(item).get("fields/txtKeyword"):
values = {'txtKeyword' : str(request.form['query'])}
else:
values = {'q' : str(request.form['query'])}
# if DictQuery(item).get("fields/cmbPMax"):
# values = {'cmbPMax' : str(request.form['max'])}
#
# if DictQuery(item).get("fields/cmbPMin"):
# values = {'cmbPMin' : str(request.form['min'])}
data = urllib.urlencode(values)
req = Request(url, data)
try:
response = urlopen(req)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
content = response.read()
with open("templates/query_results.html", "w") as f:
f.write(content)
else:
continue
return render_template('results.html')
# else:
# fd = formdetect(url)
# for item in fd:
# if "fields" in item:
# if DictQuery(item).get("fields/mk") == 'search category / refinement':
# if DictQuery(item).get("fields/mk"):
# #values = {'condition' : 'used', 'md':'','mk' : '2578', 'price[t]' : '2000000', 'price[f]' : '1000000' }
# data = {}
# #data['condition'] = 'used'
# #data['md'] = ''
# data['mk'] = str(request.form['query'])
# data['price[t]'] = str(request.form['max'])
# data['price[f]'] = str(request.form['min'])
#
# url_values = urllib.urlencode(data)
# full_url = url + '?' + url_values
# response = urlopen(full_url)
# content = response.read()
# with open("templates/query_results.html", "w") as f:
# f.write(content)
# else:
# continue
# return render_template('results.html')
@app.route('/queryresults', methods=['GET','POST'])
def queryresults():
return render_template('query_results.html')
if __name__ == '__main__':
app.run(debug=True)