MCPcopy Index your code
hub / github.com/clips/pattern / Bing

Class Bing

pattern/web/__init__.py:1206–1273  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1204BING_LICENSE = api.license["Bing"]
1205
1206class Bing(SearchEngine):
1207
1208 def __init__(self, license=None, throttle=0.5, language=None):
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))

Callers 3

08-web.pyFile · 0.90
03-bing.pyFile · 0.90
03-date.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…