-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
155 lines (129 loc) · 3.31 KB
/
Copy pathapp.py
File metadata and controls
155 lines (129 loc) · 3.31 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
import os
import json
import sys
import urllib
import tweepy
from flask import Flask, jsonify
from tweepy.parsers import JSONParser
from multiprocessing import Process, Queue
import pprint
app = Flask(__name__)
twitter_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
twitter_api_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
google_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
google_secret = "xxxxxxxxxxxxxxxx"
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(twitter_api_key, twitter_api_secret)
api = tweepy.API(auth,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
parser=tweepy.parsers.JSONParser()
)
if (not api):
print ("Authentication Error")
sys.exit (-1)
#Storing results
final_data = Queue()
#DuckDuckGo instant API
def DuckduckGo(query):
url = "https://api.duckduckgo.com/?q=%s&format=json&pretty=1" % query
duckduckgo_response = urllib.urlopen(url)
data = json.loads(duckduckgo_response.read())
final_data.put(data)
#Google Search API
def Google(query):
google_result = {
"google": {
"url": "",
"text": ""
}
}
google_url = "https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s&fields=items/pagemap/website/description" % (google_key,google_secret,query)
google_response = urllib.urlopen(google_url)
data = json.loads(google_response.read())
try:
google_result = {
"google": {
"url": google_url,
"text": data["items"][0]["pagemap"]["website"][0]["description"]
}
}
except Exception:
pass
final_data.put(google_result)
#Twitter API
def Twitter(query):
twitter_text=""
twitter_result = {
"twitter": {
"url": "",
"text": ""
}
}
twitter_response = api.search(q=query, count=1)
try:
twitter_data = twitter_response["statuses"][0]["text"]
except Exception:
pass
final_data.put(
{
"twitter": {
"url": "",
"text": twitter_data
}
}
)
@app.route("/")
def index():
return """
<html>
<body style='font-family="Helvetica, sans-serif"'>
<h1>Multiple Search</h1>
<h4>Multiple Resource Search REST API using Python</h4> <br>
<h3>About</h3>
<p>This API searches for a query on Google, Twitter and DuckDuckGo in parallel
using multiprocessing module of python and gives a combined result quickly which
contains the description from these three engines in JSON format.
</p>
<br>
<h3>Usage</h3>
steps for run it on herokuapp : <br>
<p>https://multiple-search.herokuapp.com/query <br>
curl https://quick-search.herokuapp.com/query</p>
<br>
<br>
<h3><a href="https://github.com/jaykam/Multiple_search/">github link code</a></h3>
<br>
<br>
<footer class="footer">
<br>
Contact : <br>
Name : Jatin Kumar Mittal<br>
Email : jatinmittal52@gmail.com<br>
</footer>
</body>
</html>
"""
@app.route("/<query>")
def main(query):
p1 = Process(target=Google, args=(query,))
p1.start()
p2 = Process(target=DuckduckGo, args=(query,))
p2.start()
p3 = Process(target=Twitter, args=(query,))
p3.start()
p1.join()
p2.join()
p3.join()
result = {"query": query,
"results": [
final_data.get(),
final_data.get(),
final_data.get()
]
}
return jsonify(result)
if __name__ == "__main__":
app.run(debug=True)