Returns a MediaWikiArticle for the given query. The query is case-sensitive, for example on Wikipedia: - "tiger" = Panthera tigris, - "TIGER" = Topologically Integrated Geographic Encoding and Referencing.
(self, query, type=SEARCH, start=1, count=1, sort=RELEVANCY, size=None, cached=True, **kwargs)
| 1675 | raise StopIteration |
| 1676 | |
| 1677 | def search(self, query, type=SEARCH, start=1, count=1, sort=RELEVANCY, size=None, cached=True, **kwargs): |
| 1678 | """ Returns a MediaWikiArticle for the given query. |
| 1679 | The query is case-sensitive, for example on Wikipedia: |
| 1680 | - "tiger" = Panthera tigris, |
| 1681 | - "TIGER" = Topologically Integrated Geographic Encoding and Referencing. |
| 1682 | """ |
| 1683 | if type != SEARCH: |
| 1684 | raise SearchEngineTypeError |
| 1685 | if count < 1: |
| 1686 | return None |
| 1687 | # 1) Construct request URL (e.g., Wikipedia for a given language). |
| 1688 | url = URL(self._url, method=GET, query={ |
| 1689 | "action": "parse", |
| 1690 | "page": query.replace(" ", "_"), |
| 1691 | "redirects": 1, |
| 1692 | "format": "json" |
| 1693 | }) |
| 1694 | # 2) Parse JSON response. |
| 1695 | kwargs.setdefault("unicode", True) |
| 1696 | kwargs.setdefault("timeout", 30) # Parsing the article takes some time. |
| 1697 | kwargs.setdefault("throttle", self.throttle) |
| 1698 | data = url.download(cached=cached, **kwargs) |
| 1699 | data = json.loads(data) |
| 1700 | data = data.get("parse", {}) |
| 1701 | a = self._parse_article(data, query=query) |
| 1702 | a = self._parse_article_sections(a, data) |
| 1703 | a = self._parse_article_section_structure(a) |
| 1704 | if not a.html or "id=\"noarticletext\"" in a.html: |
| 1705 | return None |
| 1706 | return a |
| 1707 | |
| 1708 | def _parse_article(self, data, **kwargs): |
| 1709 | return self.MediaWikiArticle( |
no test coverage detected