-
Notifications
You must be signed in to change notification settings - Fork 49
General Improvements to the Wikipedia Module #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
examknow
wants to merge
13
commits into
bitbot-irc:develop
Choose a base branch
from
examknow:enwiki-disambig
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3b4112e
Merge pull request #2 from jesopo/develop
examknow 540cb76
make wikipedia module work with disambig
examknow ce89add
i'm kind of an idiot
examknow c56d2da
rm unneeded print line
examknow 3562c64
some regex fixes
examknow 90ec07c
make listify even smarter
examknow e6d7d44
add a max disambig config option
examknow fdcc9d7
add wikipedia-lang chan config
examknow 9ee569a
+autolink feature
examknow 065ce54
we almost broke pm!
examknow 6a151c2
turns out I can do it this way instead
examknow 4be9878
apply various bug fixes
examknow 2b223b5
make disambigs detected by the api
examknow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,109 @@ | ||
#--depends-on commands | ||
|
||
from src import ModuleManager, utils | ||
import re | ||
import json | ||
|
||
URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php" | ||
URL_WIKIPEDIA = "https://$lang.wikipedia.org/w/api.php" | ||
|
||
@utils.export("channelset", utils.IntSetting("wikipedia-disambig-max", | ||
"Set the number disambiguation pages to show in a message")) | ||
|
||
@utils.export("channelset", utils.Setting("wikipedia-lang", | ||
"Choose which language to use for Wikipedia", | ||
example="en")) | ||
|
||
@utils.export("channelset", utils.BoolSetting("wikipedia-autolink", | ||
"Auto-translate to wiki-links")) | ||
|
||
|
||
@utils.export("set", utils.Setting("wikipedia-lang", | ||
"Choose which language to use for Wikipedia", | ||
example="en")) | ||
|
||
class Module(ModuleManager.BaseModule): | ||
def listify(self, items): | ||
if type(items) != list: | ||
items = list(items) | ||
|
||
return len(items) > 2 and ', '.join(items[:-1]) + ', or ' + items[-1] or len(items) > 1 and items[0] + ' or ' + items[1] or items and items[0] or '' | ||
|
||
def disambig(self, title, event): | ||
api = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["target"].get_setting("wikipedia-lang", "en")), get_params={ | ||
"action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() | ||
if api: | ||
text = api['parse']['wikitext']['*'] | ||
links = [] | ||
links.extend(re.findall('\* \[\[(.*)\]\]', text)) | ||
links.extend(re.findall('\*\[\[(.*)\]\]', text)) | ||
disambigs = [] | ||
if links: | ||
for link in links: | ||
# parse through the wikitext adventure | ||
if '|' in link: | ||
d = link.split('|')[1] | ||
else: | ||
d = link | ||
d = d.replace('\'', '').replace('\'', '').replace('"', '') | ||
disambigs.append(d) | ||
else: | ||
return 'Unable to parse disambiguation page. You may view the page at' | ||
return len(disambigs) > event["target"].get_setting("wikipedia-disambig-max", 10) and 'Sorry, but this page is too ambiguous. You may view the page at' or '%s could mean %s -' % (title, self.listify(disambigs)) | ||
|
||
|
||
@utils.hook("received.message.channel") | ||
def handle_chanmsg(self, event): | ||
if not event["channel"].get_setting("wikipedia-autolink", False): | ||
return | ||
wikilink = re.search("\[\[(.*)\]\]", event["message"]) | ||
if wikilink: | ||
page = wikilink.group(1) | ||
api = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["target"].get_setting("wikipedia-lang", "en")), get_params={ | ||
"action": "query", "prop": "extracts|info", "inprop": "url", | ||
"titles": page, "exintro": "", "explaintext": "", | ||
"exchars": "500", "redirects": "", "format": "json"}).json() | ||
|
||
if api: | ||
pages = api["query"]["pages"] | ||
article = list(pages.items())[0][1] | ||
if not "missing" in article: | ||
title, info = article["title"], article["extract"] | ||
title = article["title"] | ||
info = utils.parse.line_normalise(article["extract"]) | ||
url = article["fullurl"].replace(' ', '_') | ||
if 'may refer to' in info: | ||
event["channel"].send_message("%s %s" % (self.disambig(title, event), url)) | ||
else: | ||
event["channel"].send_message("%s: %s - %s" % (title, info, url)) | ||
else: | ||
event["channel"].send_message("No results found") | ||
else: | ||
raise utils.EventResultsError() | ||
|
||
|
||
@utils.hook("received.command.wi", alias_of="wiki") | ||
@utils.hook("received.command.wiki", alias_of="wikipedia") | ||
@utils.hook("received.command.wikipedia") | ||
@utils.kwarg("help", "Get information from wikipedia") | ||
@utils.spec("!<term>lstring") | ||
def wikipedia(self, event): | ||
page = utils.http.request(URL_WIKIPEDIA, get_params={ | ||
page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["target"].get_setting("wikipedia-lang", "en")), get_params={ | ||
"action": "query", "prop": "extracts|info", "inprop": "url", | ||
"titles": event["spec"][0], "exintro": "", "explaintext": "", | ||
"exchars": "500", "redirects": "", "format": "json"}).json() | ||
|
||
if page: | ||
pages = page["query"]["pages"] | ||
article = list(pages.items())[0][1] | ||
if not "missing" in article: | ||
title, info = article["title"], article["extract"] | ||
title = article["title"] | ||
info = utils.parse.line_normalise(article["extract"]) | ||
url = article["fullurl"] | ||
|
||
event["stdout"].write("%s: %s - %s" % (title, info, url)) | ||
url = article["fullurl"].replace(' ', '_') | ||
if 'may refer to' in info: | ||
event["stdout"].write("%s %s" % (self.disambig(title, event), url)) | ||
else: | ||
event["stdout"].write("%s: %s - %s" % (title, info, url)) | ||
else: | ||
event["stderr"].write("No results found") | ||
else: | ||
raise utils.EventResultsError() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.