Returns a list of results from Bing for the given query. - type : SEARCH, IMAGE or NEWS, - start: maximum 1000 results => start 1-100 with count=10, 1000/count, - count: maximum 50, or 15 for news, - size : for images, either SMALL, MEDIUM or LARG
(self, query, type=SEARCH, start=1, count=10, sort=RELEVANCY, size=None, cached=True, **kwargs)
| 1209 | SearchEngine.__init__(self, license or BING_LICENSE, throttle, language) |
| 1210 | |
| 1211 | def search(self, query, type=SEARCH, start=1, count=10, sort=RELEVANCY, size=None, cached=True, **kwargs): |
| 1212 | """" Returns a list of results from Bing for the given query. |
| 1213 | - type : SEARCH, IMAGE or NEWS, |
| 1214 | - start: maximum 1000 results => start 1-100 with count=10, 1000/count, |
| 1215 | - count: maximum 50, or 15 for news, |
| 1216 | - size : for images, either SMALL, MEDIUM or LARGE. |
| 1217 | There is no daily query limit. |
| 1218 | """ |
| 1219 | if type not in (SEARCH, IMAGE, NEWS): |
| 1220 | raise SearchEngineTypeError |
| 1221 | if type == SEARCH: |
| 1222 | src = "Web" |
| 1223 | if type == IMAGE: |
| 1224 | src = "Image" |
| 1225 | if type == NEWS: |
| 1226 | src = "News" |
| 1227 | if not query or count < 1 or start < 1 or start > 1000 / count: |
| 1228 | return Results(BING + src + "?", query, type) |
| 1229 | # 1) Construct request URL. |
| 1230 | url = URL(BING + "Composite", method=GET, query={ |
| 1231 | "Sources": "'" + src.lower() + "'", |
| 1232 | "Query": "'" + query + "'", |
| 1233 | "$skip": 1 + (start-1) * count, |
| 1234 | "$top": min(count, type==NEWS and 15 or 50), |
| 1235 | "$format": "json", |
| 1236 | }) |
| 1237 | # 2) Restrict image size. |
| 1238 | if size in (TINY, SMALL, MEDIUM, LARGE): |
| 1239 | url.query["ImageFilters"] = { |
| 1240 | TINY: "'Size:Small'", |
| 1241 | SMALL: "'Size:Small'", |
| 1242 | MEDIUM: "'Size:Medium'", |
| 1243 | LARGE: "'Size:Large'" }[size] |
| 1244 | # 3) Restrict language. |
| 1245 | if type in (SEARCH, IMAGE) and self.language is not None: |
| 1246 | url.query["Query"] = url.query["Query"][:-1] + " language: %s'" % self.language |
| 1247 | #if self.language is not None: |
| 1248 | # market = locale.market(self.language) |
| 1249 | # if market: |
| 1250 | # url.query["market"] = market |
| 1251 | # 4) Parse JSON response. |
| 1252 | kwargs["authentication"] = ("", self.license) |
| 1253 | kwargs.setdefault("unicode", True) |
| 1254 | kwargs.setdefault("throttle", self.throttle) |
| 1255 | try: |
| 1256 | data = url.download(cached=cached, **kwargs) |
| 1257 | except HTTP401Authentication: |
| 1258 | raise HTTP401Authentication, "Bing %s API is a paid service" % type |
| 1259 | data = json.loads(data) |
| 1260 | data = data.get("d", {}) |
| 1261 | data = data.get("results", [{}])[0] |
| 1262 | results = Results(BING, query, type) |
| 1263 | results.total = int(data.get(src+"Total", 0)) |
| 1264 | for x in data.get(src, []): |
| 1265 | r = Result(url=None) |
| 1266 | r.url = self.format(x.get("MediaUrl", x.get("Url"))) |
| 1267 | r.title = self.format(x.get("Title")) |
| 1268 | r.text = self.format(x.get("Description", x.get("Snippet"))) |