From 540cb766b91ee85030172503a62384ea729ebacc Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 12:54:24 -0500 Subject: [PATCH 01/12] make wikipedia module work with disambig --- modules/wikipedia.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index a2f35ce0..247f294d 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -5,6 +5,33 @@ URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php" class Module(ModuleManager.BaseModule): + def listify(self, items): + if type(items) != list: + items = list(items) + return len(items) > 1 and ', '.join(items[:-1]) + ', or ' + items[-1] or items and items[0] or '' + + def disambig(self, title): + api = utils.http.request(URL_WIKIPEDIA, get_params={ + "action": "parse", "format": "json", "page": page, "prop": "wikitext"}).json() + if api: + text = api['parse']['wikitext']['*'] + links = 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' + if len(disambigs) > 15: + return 'Sorry, but this page is too ambiguous. You may view the page at' + else: + return '%s could mean %s -' % (title, self.listify(disambigs)) @utils.hook("received.command.wi", alias_of="wiki") @utils.hook("received.command.wiki", alias_of="wikipedia") @utils.hook("received.command.wikipedia") @@ -24,7 +51,8 @@ def wikipedia(self, event): title = article["title"] info = utils.parse.line_normalise(article["extract"]) url = article["fullurl"] - + if 'may refer to' in info: + event["stdout.write("%s %s" % (self.disambig(title), url) event["stdout"].write("%s: %s - %s" % (title, info, url)) else: event["stderr"].write("No results found") From ce89add626a0c99367c70431532567f9ea8b38f9 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 15:48:46 -0500 Subject: [PATCH 02/12] i'm kind of an idiot --- modules/wikipedia.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 247f294d..743af578 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -1,6 +1,8 @@ #--depends-on commands from src import ModuleManager, utils +import re +import json URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php" @@ -9,10 +11,10 @@ def listify(self, items): if type(items) != list: items = list(items) return len(items) > 1 and ', '.join(items[:-1]) + ', or ' + items[-1] or items and items[0] or '' - + def disambig(self, title): api = utils.http.request(URL_WIKIPEDIA, get_params={ - "action": "parse", "format": "json", "page": page, "prop": "wikitext"}).json() + "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() if api: text = api['parse']['wikitext']['*'] links = re.findall('\* \[\[(.*)\]\]', text) @@ -38,6 +40,7 @@ def disambig(self, title): @utils.kwarg("help", "Get information from wikipedia") @utils.spec("!lstring") def wikipedia(self, event): + print(event["spec"][0]) page = utils.http.request(URL_WIKIPEDIA, get_params={ "action": "query", "prop": "extracts|info", "inprop": "url", "titles": event["spec"][0], "exintro": "", "explaintext": "", @@ -52,10 +55,10 @@ def wikipedia(self, event): info = utils.parse.line_normalise(article["extract"]) url = article["fullurl"] if 'may refer to' in info: - event["stdout.write("%s %s" % (self.disambig(title), url) - event["stdout"].write("%s: %s - %s" % (title, info, url)) + event["stdout"].write("%s %s" % (self.disambig(title), url)) + else: + event["stdout"].write("%s: %s - %s" % (title, info, url)) else: event["stderr"].write("No results found") else: raise utils.EventResultsError() - From c56d2da0ffc496e4d21731dc7c13e652bff769d9 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 15:49:52 -0500 Subject: [PATCH 03/12] rm unneeded print line --- modules/wikipedia.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 743af578..eec7f74f 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -40,7 +40,6 @@ def disambig(self, title): @utils.kwarg("help", "Get information from wikipedia") @utils.spec("!lstring") def wikipedia(self, event): - print(event["spec"][0]) page = utils.http.request(URL_WIKIPEDIA, get_params={ "action": "query", "prop": "extracts|info", "inprop": "url", "titles": event["spec"][0], "exintro": "", "explaintext": "", From 3562c648a60cf0bd5a122009be6e4afa447b86a4 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 15:56:26 -0500 Subject: [PATCH 04/12] some regex fixes --- modules/wikipedia.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index eec7f74f..9311eaa5 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -17,7 +17,9 @@ def disambig(self, title): "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() if api: text = api['parse']['wikitext']['*'] - links = re.findall('\* \[\[(.*)\]\]', text) + links = [] + links.extend(re.findall('\* \[\[(.*)\]\]', text)) + links.extend(re.findall('\*\[\[(.*)\]\]', text)) disambigs = [] if links: for link in links: From 90ec07c18acd35c1cf68ca3dae0f8b2a4e48dd83 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 16:53:55 -0500 Subject: [PATCH 05/12] make listify even smarter --- modules/wikipedia.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 9311eaa5..dc0cdd2a 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -10,7 +10,8 @@ class Module(ModuleManager.BaseModule): def listify(self, items): if type(items) != list: items = list(items) - return len(items) > 1 and ', '.join(items[:-1]) + ', or ' + items[-1] or items and items[0] or '' + + 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): api = utils.http.request(URL_WIKIPEDIA, get_params={ From e6d7d4446d03fd44f5fa304d64145a5603143aca Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 17:08:37 -0500 Subject: [PATCH 06/12] add a max disambig config option --- modules/wikipedia.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index dc0cdd2a..cc16b60b 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -6,6 +6,9 @@ URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php" +@utils.export("channelset", utils.IntSetting("wikipedia-disambig-max", + "Set the number disambiguation pages to show in a message")) + class Module(ModuleManager.BaseModule): def listify(self, items): if type(items) != list: @@ -13,7 +16,7 @@ def listify(self, 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): + def disambig(self, title, event): api = utils.http.request(URL_WIKIPEDIA, get_params={ "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() if api: @@ -33,7 +36,7 @@ def disambig(self, title): disambigs.append(d) else: return 'Unable to parse disambiguation page. You may view the page at' - if len(disambigs) > 15: + if len(disambigs) > event["channel"].get_setting("wikipedia-disambig-max", 10): return 'Sorry, but this page is too ambiguous. You may view the page at' else: return '%s could mean %s -' % (title, self.listify(disambigs)) @@ -57,7 +60,7 @@ def wikipedia(self, event): info = utils.parse.line_normalise(article["extract"]) url = article["fullurl"] if 'may refer to' in info: - event["stdout"].write("%s %s" % (self.disambig(title), url)) + event["stdout"].write("%s %s" % (self.disambig(title, event), url)) else: event["stdout"].write("%s: %s - %s" % (title, info, url)) else: From fdcc9d78c69b9d9d8cecb260f61629d1747cb927 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 17:16:54 -0500 Subject: [PATCH 07/12] add wikipedia-lang chan config --- modules/wikipedia.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index cc16b60b..4feceab8 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -4,11 +4,15 @@ 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")) + class Module(ModuleManager.BaseModule): def listify(self, items): if type(items) != list: @@ -17,7 +21,7 @@ def listify(self, 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, get_params={ + api = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].get_setting("wikipedia-lang", "en")), get_params={ "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() if api: text = api['parse']['wikitext']['*'] @@ -46,7 +50,7 @@ def disambig(self, title, event): @utils.kwarg("help", "Get information from wikipedia") @utils.spec("!lstring") def wikipedia(self, event): - page = utils.http.request(URL_WIKIPEDIA, get_params={ + page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].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() From 9ee569a960071729d716eb336f1f48effccd12bd Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 18:05:55 -0500 Subject: [PATCH 08/12] +autolink feature --- modules/wikipedia.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 4feceab8..79084fce 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -13,6 +13,9 @@ "Choose which language to use for Wikipedia", example="en")) +@utils.export("channelset", utils.BoolSetting("wikipedia-autolink", + "Auto-translate to wiki-links")) + class Module(ModuleManager.BaseModule): def listify(self, items): if type(items) != list: @@ -44,6 +47,38 @@ def disambig(self, title, event): return 'Sorry, but this page is too ambiguous. You may view the page at' else: return '%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) + page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].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 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"] + 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") From 065ce5490b81f45e241ec96aec78a7a6640cd4cd Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 18:23:47 -0500 Subject: [PATCH 09/12] we almost broke pm! --- modules/wikipedia.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 79084fce..f65fb7e5 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -16,6 +16,11 @@ @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: @@ -24,8 +29,12 @@ def listify(self, 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["channel"].get_setting("wikipedia-lang", "en")), get_params={ - "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() + if not str(event["target"]).startswith('#'): + 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() + else: + api = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].get_setting("wikipedia-lang", "en")), get_params={ + "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() if api: text = api['parse']['wikitext']['*'] links = [] @@ -85,11 +94,16 @@ def handle_chanmsg(self, event): @utils.kwarg("help", "Get information from wikipedia") @utils.spec("!lstring") def wikipedia(self, event): - page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].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 not str(event["target"]).startswith('#'): + 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() + else: + page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].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] From 6a151c202d8738f0a813d10b6748793b61d4bda4 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 18:31:51 -0500 Subject: [PATCH 10/12] turns out I can do it this way instead --- modules/wikipedia.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index f65fb7e5..f3b75107 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -29,12 +29,8 @@ def listify(self, 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): - if not str(event["target"]).startswith('#'): - 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() - else: - api = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].get_setting("wikipedia-lang", "en")), get_params={ - "action": "parse", "format": "json", "page": title, "prop": "wikitext"}).json() + 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 = [] @@ -52,7 +48,7 @@ def disambig(self, title, event): disambigs.append(d) else: return 'Unable to parse disambiguation page. You may view the page at' - if len(disambigs) > event["channel"].get_setting("wikipedia-disambig-max", 10): + if len(disambigs) > event["target"].get_setting("wikipedia-disambig-max", 10): return 'Sorry, but this page is too ambiguous. You may view the page at' else: return '%s could mean %s -' % (title, self.listify(disambigs)) @@ -65,7 +61,7 @@ def handle_chanmsg(self, event): wikilink = re.search("\[\[(.*)\]\]", event["message"]) if wikilink: page = wikilink.group(1) - page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].get_setting("wikipedia-lang", "en")), 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": page, "exintro": "", "explaintext": "", "exchars": "500", "redirects": "", "format": "json"}).json() @@ -94,16 +90,11 @@ def handle_chanmsg(self, event): @utils.kwarg("help", "Get information from wikipedia") @utils.spec("!lstring") def wikipedia(self, event): - if not str(event["target"]).startswith('#'): - 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() - else: - page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["channel"].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 not str(event["target"]).startswith('#'): + 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] From 4be98786c43e4483561ddc21f7901ebcca918b92 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 8 Oct 2020 19:17:30 -0500 Subject: [PATCH 11/12] apply various bug fixes --- modules/wikipedia.py | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index f3b75107..b2e8998d 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -48,10 +48,7 @@ def disambig(self, title, event): disambigs.append(d) else: return 'Unable to parse disambiguation page. You may view the page at' - if len(disambigs) > event["target"].get_setting("wikipedia-disambig-max", 10): - return 'Sorry, but this page is too ambiguous. You may view the page at' - else: - return '%s could mean %s -' % (title, self.listify(disambigs)) + 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") @@ -61,27 +58,27 @@ def handle_chanmsg(self, event): wikilink = re.search("\[\[(.*)\]\]", event["message"]) if wikilink: page = wikilink.group(1) - 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": page, "exintro": "", "explaintext": "", - "exchars": "500", "redirects": "", "format": "json"}).json() + 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 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"] - if 'may refer to' in info: - event["channel"].send_message("%s %s" % (self.disambig(title, event), url)) + 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("%s: %s - %s" % (title, info, url)) + event["channel"].send_message("No results found") else: - event["channel"].send_message("No results found") - else: - raise utils.EventResultsError() + raise utils.EventResultsError() @utils.hook("received.command.wi", alias_of="wiki") @@ -90,7 +87,6 @@ def handle_chanmsg(self, event): @utils.kwarg("help", "Get information from wikipedia") @utils.spec("!lstring") def wikipedia(self, event): -# if not str(event["target"]).startswith('#'): 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": "", @@ -102,7 +98,7 @@ def wikipedia(self, event): title, info = article["title"], article["extract"] title = article["title"] info = utils.parse.line_normalise(article["extract"]) - url = article["fullurl"] + url = article["fullurl"].replace(' ', '_') if 'may refer to' in info: event["stdout"].write("%s %s" % (self.disambig(title, event), url)) else: From 2b223b537fdb4a9e5023a4c3ca5600a1635df72e Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 22 Oct 2020 09:18:44 -0500 Subject: [PATCH 12/12] make disambigs detected by the api --- modules/wikipedia.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index b2e8998d..9857b165 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -88,9 +88,9 @@ def handle_chanmsg(self, event): @utils.spec("!lstring") def wikipedia(self, event): page = utils.http.request(URL_WIKIPEDIA.replace('$lang', event["target"].get_setting("wikipedia-lang", "en")), get_params={ - "action": "query", "prop": "extracts|info", "inprop": "url", + "action": "query", "prop": "extracts|info|pageprops", "inprop": "url", "titles": event["spec"][0], "exintro": "", "explaintext": "", - "exchars": "500", "redirects": "", "format": "json"}).json() + "exchars": "500", "redirects": "", "format": "json", "pageprops": "disambiguation"}).json() if page: pages = page["query"]["pages"] article = list(pages.items())[0][1] @@ -99,7 +99,7 @@ def wikipedia(self, event): title = article["title"] info = utils.parse.line_normalise(article["extract"]) url = article["fullurl"].replace(' ', '_') - if 'may refer to' in info: + if "pageprops" in article and "disambiguation" in article["pageprops"]: event["stdout"].write("%s %s" % (self.disambig(title, event), url)) else: event["stdout"].write("%s: %s - %s" % (title, info, url))