Returns a list of results from DuckDuckGo for the given query.
(self, query, type=SEARCH, start=None, count=None, sort=RELEVANCY, size=None, cached=True, **kwargs)
| 1292 | SearchEngine.__init__(self, license or DUCKDUCKGO_LICENSE, throttle, language) |
| 1293 | |
| 1294 | def search(self, query, type=SEARCH, start=None, count=None, sort=RELEVANCY, size=None, cached=True, **kwargs): |
| 1295 | """" Returns a list of results from DuckDuckGo for the given query. |
| 1296 | """ |
| 1297 | if type != SEARCH: |
| 1298 | raise SearchEngineTypeError |
| 1299 | # 1) Construct request URL. |
| 1300 | url = URL(DUCKDUCKGO, method=GET, query={ |
| 1301 | "q": query, |
| 1302 | "o": "json" |
| 1303 | }) |
| 1304 | # 2) Restrict language. |
| 1305 | if type == SEARCH and self.language is not None: |
| 1306 | url.query["kl"] = self.language |
| 1307 | # 3) Parse JSON response. |
| 1308 | kwargs.setdefault("unicode", True) |
| 1309 | kwargs.setdefault("throttle", self.throttle) |
| 1310 | data = url.download(cached=cached, **kwargs) |
| 1311 | data = json.loads(data) |
| 1312 | results = Results(DUCKDUCKGO, query, type) |
| 1313 | results.total = None |
| 1314 | for x in data.get("Results", []): |
| 1315 | if x.get("FirstURL"): |
| 1316 | r = Result(url=None) |
| 1317 | # Parse official website link. |
| 1318 | r.url = self.format(x.get("FirstURL")) |
| 1319 | r.title = self.format(data.get("Heading")) |
| 1320 | r.text = self.format(data.get("Abstract")) |
| 1321 | r.author = self.format(data.get("AbstractSource")) |
| 1322 | r.type = self.format(REFERENCE) |
| 1323 | results.append(r) |
| 1324 | for topic in data.get("RelatedTopics", []): |
| 1325 | for x in topic.get("Topics", [topic]): |
| 1326 | r = Result(url=None) |
| 1327 | r.url = x.get("FirstURL") |
| 1328 | # Parse title and type from URL (e.g., http://duckduckgo.com/d/Cats?kl=en). |
| 1329 | m = re.match(r"^http://duckduckgo.com/([a-z]/)?(.*?)(\?|$)", r.url) |
| 1330 | # Parse title: "Cats". |
| 1331 | s1 = m and m.group(2) or "" # Title: "Cats" |
| 1332 | s1 = u(decode_url(s1.encode("utf-8"))) |
| 1333 | s1 = s1.strip().replace("_", " ") |
| 1334 | s1 = s1[:1].upper() + s1[1:] |
| 1335 | # Parse description; the part before the first "-" or "," was the link. |
| 1336 | s2 = x.get("Text", "").strip() |
| 1337 | s2 = re.sub(r" +", " ", s2) |
| 1338 | s2 = s2[:1].upper() + s2[1:] or "" |
| 1339 | s2 = s2.startswith(s1) \ |
| 1340 | and "<a href=\"%s\">%s</a>%s" % (r.url, s1, s2[len(s1):]) \ |
| 1341 | or re.sub(r"^(.*?)( - | or |, )(.*?)", "<a href=\"%s\">\\1</a>\\2\\3" % r.url, s2) |
| 1342 | # Parse type: "d/" => "definition". |
| 1343 | s3 = m and m.group(1) or "" |
| 1344 | s3 = {"c": CATEGORY, "d": DEFINITION}.get(s3.rstrip("/"), "") |
| 1345 | s3 = topic.get("Name", "").lower() or s3 |
| 1346 | s3 = re.sub("^in ", "", s3) |
| 1347 | # Format result. |
| 1348 | r.url = self.format(r.url) |
| 1349 | r.title = self.format(s1) |
| 1350 | r.text = self.format(s2) |
| 1351 | r.type = self.format(s3) |
nothing calls this directly
no test coverage detected