mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-04-26 13:01:07 -05:00
Wikifetch: use MW OpenSearch API instead of fetching exact title
This commit is contained in:
parent
582a02f74e
commit
810f87de09
@ -1,20 +1,8 @@
|
|||||||
Grabs data from Wikipedia and other MediaWiki-powered sites. This plugin requires the [lxml](https://lxml.de/installation.html) Python module.
|
Fetch content from MediaWiki-powered sites (Wikipedia, Fandom). This plugin requires the [Beautiful Soup 4](http://www.crummy.com/software/BeautifulSoup/bs4/doc/) and [mwparserfromhell](https://mwparserfromhell.readthedocs.io/).
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
<jlu5> `wiki apple
|
<jlu5> `wiki apple
|
||||||
<Atlas> The apple tree (Malus domestica) is a deciduous tree in the rose family best known for its sweet, pomaceous fruit, the apple. It is cultivated worldwide as a fruit tree, and is the most widely grown species in the genus Malus. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were (1 more message)
|
<Atlas> An apple is an edible fruit produced by an apple tree (Malus domestica). Apple trees are cultivated worldwide and are the most widely grown species in the genus Malus. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe and were brought to North America by European colonists. Apples have religious and mythological significance in many cultures, including Norse, Greek, and European Christian tradition. - https://en.wikipedia.org/wiki/Apple
|
||||||
```
|
|
||||||
|
|
||||||
You can also add other sites using the `--site` option:
|
|
||||||
|
|
||||||
```
|
|
||||||
<jlu5> `wiki --site wiki.archlinux.org Bash
|
|
||||||
<Atlas> Bash (Bourne-again Shell) is a command-line shell/programming language by the GNU Project. Its name is a homaging reference to its predecessor: the long-deprecated Bourne shell. Bash can be run on most UNIX-like operating systems, including GNU/Linux. Retrieved from <https://wiki.archlinux.org/index.php?title=Bash>
|
|
||||||
```
|
|
||||||
|
|
||||||
```
|
|
||||||
<jlu5> `wiki --site community.wikia.com Help:Wikitext
|
|
||||||
<Atlas> Wikitext is the main markup language used to format content on wikias. It can be used to add photos, tables, bold styles, links, and other visual changes. Retrieved from <http://community.wikia.com/wiki/Help:Wikitext>
|
|
||||||
```
|
```
|
||||||
|
@ -55,7 +55,24 @@ class Wikifetch(callbacks.Plugin):
|
|||||||
"""Grabs data from Wikipedia and other MediaWiki-powered sites."""
|
"""Grabs data from Wikipedia and other MediaWiki-powered sites."""
|
||||||
threaded = True
|
threaded = True
|
||||||
|
|
||||||
def _mediawiki_fetch(self, baseurl, title):
|
def _mw_api_call(self, baseurl, params):
|
||||||
|
"""Performs a GET call to the MediaWiki API."""
|
||||||
|
url = f"{baseurl}?{params}"
|
||||||
|
|
||||||
|
self.log.debug('Wikifetch: fetching link %s', url)
|
||||||
|
with utils.web.getUrlFd(url, headers=HEADERS) as fd:
|
||||||
|
api_data = json.load(fd)
|
||||||
|
|
||||||
|
if isinstance(api_data, dict):
|
||||||
|
if error := api_data.get('error'):
|
||||||
|
error_code = error['code']
|
||||||
|
error_info = error['info']
|
||||||
|
raise callbacks.Error(f"MediaWiki API Error: {error_code} - {error_info} - {url}")
|
||||||
|
|
||||||
|
return api_data
|
||||||
|
|
||||||
|
def _mw_fetch_article(self, baseurl, title):
|
||||||
|
"""Return the first paragraph and canonical URL of a wiki page."""
|
||||||
params = urllib.parse.urlencode({
|
params = urllib.parse.urlencode({
|
||||||
'action': 'parse',
|
'action': 'parse',
|
||||||
'page': title,
|
'page': title,
|
||||||
@ -64,16 +81,7 @@ class Wikifetch(callbacks.Plugin):
|
|||||||
'format': 'json',
|
'format': 'json',
|
||||||
'redirects': True
|
'redirects': True
|
||||||
})
|
})
|
||||||
url = f"{baseurl}?{params}"
|
api_data = self._mw_api_call(baseurl, params)
|
||||||
|
|
||||||
self.log.debug('Wikifetch: fetching link %s', url)
|
|
||||||
with utils.web.getUrlFd(url, headers=HEADERS) as fd:
|
|
||||||
api_data = json.load(fd)
|
|
||||||
|
|
||||||
if error := api_data.get('error'):
|
|
||||||
error_code = error['code']
|
|
||||||
error_info = error['info']
|
|
||||||
raise callbacks.Error(f"MediaWiki API Error: {error_code} - {error_info} - {url}")
|
|
||||||
|
|
||||||
page_title = api_data['parse']['title']
|
page_title = api_data['parse']['title']
|
||||||
content = api_data['parse']['wikitext']
|
content = api_data['parse']['wikitext']
|
||||||
@ -101,8 +109,24 @@ class Wikifetch(callbacks.Plugin):
|
|||||||
|
|
||||||
return (text, url)
|
return (text, url)
|
||||||
|
|
||||||
def _wiki(self, irc, baseurl, title):
|
def _mw_search(self, baseurl, searchquery):
|
||||||
text, url = self._mediawiki_fetch(baseurl, title)
|
"""Return results from a MediaWiki search."""
|
||||||
|
params = urllib.parse.urlencode({
|
||||||
|
'action': 'opensearch',
|
||||||
|
'search': searchquery,
|
||||||
|
'format': 'json',
|
||||||
|
})
|
||||||
|
api_data = self._mw_api_call(baseurl, params)
|
||||||
|
|
||||||
|
search_result_titles = api_data[1]
|
||||||
|
if not search_result_titles:
|
||||||
|
raise callbacks.Error(f"No search results for {searchquery!r}")
|
||||||
|
return search_result_titles
|
||||||
|
|
||||||
|
def _wiki(self, irc, baseurl, query):
|
||||||
|
search_results = self._mw_search(baseurl, query)
|
||||||
|
page_title = search_results[0]
|
||||||
|
text, url = self._mw_fetch_article(baseurl, page_title)
|
||||||
if url:
|
if url:
|
||||||
irc.reply(utils.str.format("%s - %u", text, url))
|
irc.reply(utils.str.format("%s - %u", text, url))
|
||||||
else:
|
else:
|
||||||
@ -110,8 +134,8 @@ class Wikifetch(callbacks.Plugin):
|
|||||||
|
|
||||||
@internationalizeDocstring
|
@internationalizeDocstring
|
||||||
@wrap([getopts({'lang': 'somethingWithoutSpaces'}), 'text'])
|
@wrap([getopts({'lang': 'somethingWithoutSpaces'}), 'text'])
|
||||||
def wiki(self, irc, msg, args, optlist, title):
|
def wiki(self, irc, msg, args, optlist, searchquery):
|
||||||
"""<page title>
|
"""<search query>
|
||||||
|
|
||||||
Returns the first paragraph of a Wikipedia article.
|
Returns the first paragraph of a Wikipedia article.
|
||||||
"""
|
"""
|
||||||
@ -120,16 +144,16 @@ class Wikifetch(callbacks.Plugin):
|
|||||||
self.registryValue('wikipedia.lang', channel=msg.channel, network=irc.network)
|
self.registryValue('wikipedia.lang', channel=msg.channel, network=irc.network)
|
||||||
|
|
||||||
baseurl = f'https://{lang}.wikipedia.org/w/api.php'
|
baseurl = f'https://{lang}.wikipedia.org/w/api.php'
|
||||||
self._wiki(irc, baseurl, title)
|
self._wiki(irc, baseurl, searchquery)
|
||||||
|
|
||||||
@wrap(['somethingWithoutSpaces', 'text'])
|
@wrap(['somethingWithoutSpaces', 'text'])
|
||||||
def fandom(self, irc, msg, args, wiki_subdomain, title):
|
def fandom(self, irc, msg, args, wiki_subdomain, searchquery):
|
||||||
"""<wiki subdomain> <title>
|
"""<wiki subdomain> <title>
|
||||||
|
|
||||||
Returns the first paragraph of a Fandom article.
|
Returns the first paragraph of a Fandom article.
|
||||||
"""
|
"""
|
||||||
baseurl = f'https://{wiki_subdomain}.fandom.com/api.php'
|
baseurl = f'https://{wiki_subdomain}.fandom.com/api.php'
|
||||||
self._wiki(irc, baseurl, title)
|
self._wiki(irc, baseurl, searchquery)
|
||||||
|
|
||||||
|
|
||||||
Class = Wikifetch
|
Class = Wikifetch
|
||||||
|
@ -38,14 +38,15 @@ if network:
|
|||||||
self.assertRegexp('wiki Vancouver',
|
self.assertRegexp('wiki Vancouver',
|
||||||
r'^Vancouver.*Canada')
|
r'^Vancouver.*Canada')
|
||||||
self.assertRegexp('wiki Python (programming language)',
|
self.assertRegexp('wiki Python (programming language)',
|
||||||
'Python')
|
'Python.*programming language')
|
||||||
# Follow redirects
|
|
||||||
|
def testWikipediaFollowRedirects(self):
|
||||||
self.assertRegexp('wiki CYVR',
|
self.assertRegexp('wiki CYVR',
|
||||||
'Vancouver International Airport')
|
'Vancouver International Airport')
|
||||||
|
|
||||||
# Display MW errors
|
def testWikipediaSearch(self):
|
||||||
self.assertRegexp('wiki NotFoundTest',
|
self.assertRegexp('wiki Python language',
|
||||||
"missingtitle - The page you specified doesn't exist")
|
'Python.*programming language')
|
||||||
|
|
||||||
def testWikipediaLang(self):
|
def testWikipediaLang(self):
|
||||||
self.assertRegexp('wiki --lang fr Paris', 'Paris.*capitale')
|
self.assertRegexp('wiki --lang fr Paris', 'Paris.*capitale')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user