-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_bing_search.py
More file actions
348 lines (289 loc) · 11.6 KB
/
py_bing_search.py
File metadata and controls
348 lines (289 loc) · 11.6 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Credits to: https://github.com/tristantao/py-bing-search
@author: tristantao
Some very minor changes have been made to this module
"""
import requests, requests.utils
import time
API_KEY = "YOUR_API_KEY_HERE"
class PyBingException(Exception):
pass
class PyBingSearch(object):
"""
Shell class for the individual searches
"""
def __init__(self, api_key, query, query_base, safe=False):
self.api_key = api_key
self.safe = safe
self.current_offset = 0
self.query = query
self.QUERY_URL = query_base
def search(self, limit=50, format='json'):
''' Returns the result list, and also the uri for next page (returned_list, next_uri) '''
return self._search(limit, format)
def search_all(self, limit=50, format='json'):
''' Returns a single list containing up to 'limit' Result objects'''
desired_limit = limit
results = self._search(limit, format)
limit = limit - len(results)
while len(results) < desired_limit:
more_results = self._search(limit, format)
if not more_results:
break
results += more_results
limit = limit - len(more_results)
time.sleep(1)
return results
class PyBingImageException(Exception):
pass
class PyBingImageSearch(PyBingSearch):
IMAGE_QUERY_BASE = 'https://api.datamarket.azure.com/Bing/Search/Image' \
+ '?Query={}&$top={}&$skip={}&$format={}'
def __init__(self, query, safe=False):
PyBingSearch.__init__(self, API_KEY, query, self.IMAGE_QUERY_BASE, safe=safe)
def _search(self, limit, format):
'''
Returns a list of result objects, with the url for the next page bing search url.
'''
url = self.QUERY_URL.format(requests.utils.quote("'{}'".format(self.query)), min(50, limit), self.current_offset, format)
r = requests.get(url, auth=("", self.api_key))
try:
json_results = r.json()
except ValueError as vE:
if not self.safe:
raise PyBingImageException("Request returned with code %s, error msg: %s" % (r.status_code, r.text))
else:
print ("[ERROR] Request returned with code %s, error msg: %s. \nContinuing in 5 seconds." % (r.status_code, r.text))
time.sleep(5)
packaged_results = [ImageResult(single_result_json) for single_result_json in json_results['d']['results']]
self.current_offset += min(50, limit, len(packaged_results))
return packaged_results
class ImageResult(object):
'''
The class represents a single image search result.
Each result will come with the following:
#For the actual image results#
self.id: id of the result
self.title: title of the resulting image
self.media_url: url to the full size image
self.source_url: url of the website that contains the source image
self.width: width of the image
self.height: height of the image
self.file_size: size of the image (in bytes) if available
self.content_type the MIME type of the image if available
self.meta: meta info
#Meta info#:
meta.uri: the search uri for bing
meta.type: for the most part ImageResult
'''
class _Meta(object):
'''
Holds the meta info for the result.
'''
def __init__(self, meta):
self.type = meta['type']
self.uri = meta['uri']
def __init__(self, result):
self.id = result['ID']
self.title = result['Title']
self.media_url = result['MediaUrl']
self.source_url = result['SourceUrl']
self.display_url = result['DisplayUrl']
self.width = result['Width']
self.height = result['Height']
self.file_size = result['FileSize']
self.content_type = result['ContentType']
self.meta = self._Meta(result['__metadata'])
def image_results_to_file(image_results, file_name, append=False):
action = 'w'
if append:
action = 'a'
with open(file_name, action) as myfile:
for item in image_results:
try:
myfile.write("{};;;{};;;{};;;{};;;{};;;{}\n".format(item.title,
item.media_url, item.width, item.height, item.file_size, item.content_type))
except:
try:
myfile.write("Title Unparsable;;;{};;;{};;;{};;;{};;;{}\n".format(item.media_url,
item.width, item.height, item.file_size, item.content_type))
except:
print item.title
print item.media_url
##
##
## Web Search
##
##
class PyBingWebException(Exception):
pass
class PyBingWebSearch(PyBingSearch):
SEARCH_WEB_BASE = 'https://api.datamarket.azure.com/Bing/Search/Web'
WEB_ONLY_BASE = 'https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web'
QUERYSTRING_TEMPLATE = '?Query={}&$top={}&$skip={}&$format={}'
def __init__(self, query, web_only=False, safe=False):
if web_only:
query_base = self.WEB_ONLY_BASE + self.QUERYSTRING_TEMPLATE
else:
query_base = self.SEARCH_WEB_BASE + self.QUERYSTRING_TEMPLATE
PyBingSearch.__init__(self, API_KEY, query, query_base, safe=safe)
def _search(self, limit, format):
'''
Returns a list of result objects, with the url for the next page bing search url.
'''
url = self.QUERY_URL.format(requests.utils.quote("'{}'".format(self.query)), min(50, limit), self.current_offset, format)
r = requests.get(url, auth=("", self.api_key))
try:
json_results = r.json()
except ValueError as vE:
if not self.safe:
raise PyBingWebException("Request returned with code %s, error msg: %s" % (r.status_code, r.text))
else:
print ("[ERROR] Request returned with code %s, error msg: %s. \nContinuing in 5 seconds." % (r.status_code, r.text))
time.sleep(5)
packaged_results = [WebResult(single_result_json) for single_result_json in json_results['d']['results']]
self.current_offset += min(50, limit, len(packaged_results))
return packaged_results
class WebResult(object):
'''
The class represents a SINGLE search result.
Each result will come with the following:
#For the actual results#
title: title of the result
url: the url of the result
description: description for the result
id: bing id for the page
#Meta info#:
meta.uri: the search uri for bing
meta.type: for the most part WebResult
'''
class _Meta(object):
'''
Holds the meta info for the result.
'''
def __init__(self, meta):
self.type = meta['type']
self.uri = meta['uri']
def __init__(self, result):
self.url = result['Url']
self.title = result['Title']
self.description = result['Description']
self.id = result['ID']
self.meta = self._Meta(result['__metadata'])
##
##
## Video Search
##
##
class PyBingVideoException(Exception):
pass
class PyBingVideoSearch(PyBingSearch):
VIDEO_QUERY_BASE = 'https://api.datamarket.azure.com/Bing/Search/Video' \
+ '?Query={}&$top={}&$skip={}&$format={}'
def __init__(self, query, safe=False):
PyBingSearch.__init__(self, API_KEY, query, self.VIDEO_QUERY_BASE, safe=safe)
def _search(self, limit, format):
'''
Returns a list of result objects, with the url for the next page bing search url.
'''
url = self.QUERY_URL.format(requests.utils.quote("'{}'".format(self.query)), min(50, limit), self.current_offset, format)
r = requests.get(url, auth=("", self.api_key))
try:
json_results = r.json()
except ValueError as vE:
if not self.safe:
raise PyBingVideoException("Request returned with code %s, error msg: %s" % (r.status_code, r.text))
else:
print ("[ERROR] Request returned with code %s, error msg: %s. \nContinuing in 5 seconds." % (r.status_code, r.text))
time.sleep(5)
packaged_results = [VideoResult(single_result_json) for single_result_json in json_results['d']['results']]
self.current_offset += min(50, limit, len(packaged_results))
return packaged_results
class VideoResult(object):
'''
The class represents a single Video search result.
Each result will come with the following:
#For the actual Video results#
self.id: id of the result
self.title: title of the resulting Video
self.media_url: url to the full size Video
self.display_url: url to display on the search result.
self.run_time: run time of the video
self.meta: meta info
#Meta info#:
meta.uri: the search uri for bing
meta.type: for the most part VideoResult
'''
class _Meta(object):
'''
Holds the meta info for the result.
'''
def __init__(self, meta):
self.type = meta['type']
self.uri = meta['uri']
def __init__(self, result):
self.id = result['ID']
self.title = result['Title']
self.media_url = result['MediaUrl']
self.display_url = result['DisplayUrl']
self.run_time = result['RunTime']
self.meta = self._Meta(result['__metadata'])
##
##
## News Search
##
##
class PyBingNewsException(Exception):
pass
class PyBingNewsSearch(PyBingSearch):
NEWS_QUERY_BASE = 'https://api.datamarket.azure.com/Bing/Search/News' \
+ '?Query={}&$top={}&$skip={}&$format={}'
def __init__(self, query, safe=False):
PyBingSearch.__init__(self, API_KEY, query, self.NEWS_QUERY_BASE, safe=safe)
def _search(self, limit, format):
'''
Returns a list of result objects, with the url for the next page bing search url.
'''
url = self.QUERY_URL.format(requests.utils.quote("'{}'".format(self.query)), min(50, limit), self.current_offset, format)
r = requests.get(url, auth=("", self.api_key))
try:
json_results = r.json()
except ValueError as vE:
if not self.safe:
raise PyBingNewsException("Request returned with code %s, error msg: %s" % (r.status_code, r.text))
else:
print ("[ERROR] Request returned with code %s, error msg: %s. \nContinuing in 5 seconds." % (r.status_code, r.text))
time.sleep(5)
packaged_results = [NewsResult(single_result_json) for single_result_json in json_results['d']['results']]
self.current_offset += min(50, limit, len(packaged_results))
return packaged_results
class NewsResult(object):
'''
The class represents a single News search result.
Each result will come with the following:
#For the actual News results#
self.id: id of the result
self.title: title of the resulting News
self.url: url to the News
self.description: description of the article
self.date: date of the News
self.meta: meta info
#Meta info#:
meta.uri: the search uri for bing
meta.type: for the most part NewsResult
'''
class _Meta(object):
'''
Holds the meta info for the result.
'''
def __init__(self, meta):
self.type = meta['type']
self.uri = meta['uri']
def __init__(self, result):
self.id = result['ID']
self.title = result['Title']
self.url = result['Url']
self.source = result['Source']
self.description = result['Description']
self.date = result['Date']
self.meta = self._Meta(result['__metadata'])