[mediawiki] Permit control of ssl and absolute path through keywords
This commit is contained in:
parent
e9cea5d010
commit
3f2b18cae8
|
@ -16,10 +16,10 @@ from more import Response
|
||||||
|
|
||||||
# MEDIAWIKI REQUESTS ##################################################
|
# MEDIAWIKI REQUESTS ##################################################
|
||||||
|
|
||||||
def get_namespaces(site, ssl=False):
|
def get_namespaces(site, ssl=False, path="/w/api.php"):
|
||||||
# Built URL
|
# Built URL
|
||||||
url = "http%s://%s/w/api.php?format=json&action=query&meta=siteinfo&siprop=namespaces" % (
|
url = "http%s://%s%s?format=json&action=query&meta=siteinfo&siprop=namespaces" % (
|
||||||
"s" if ssl else "", site)
|
"s" if ssl else "", site, path)
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
data = web.getJSON(url)
|
data = web.getJSON(url)
|
||||||
|
@ -30,10 +30,10 @@ def get_namespaces(site, ssl=False):
|
||||||
return namespaces
|
return namespaces
|
||||||
|
|
||||||
|
|
||||||
def get_raw_page(site, term, ssl=False):
|
def get_raw_page(site, term, ssl=False, path="/w/api.php"):
|
||||||
# Built URL
|
# Built URL
|
||||||
url = "http%s://%s/w/api.php?format=json&redirects&action=query&prop=revisions&rvprop=content&titles=%s" % (
|
url = "http%s://%s%s?format=json&redirects&action=query&prop=revisions&rvprop=content&titles=%s" % (
|
||||||
"s" if ssl else "", site, urllib.parse.quote(term))
|
"s" if ssl else "", site, path, urllib.parse.quote(term))
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
data = web.getJSON(url)
|
data = web.getJSON(url)
|
||||||
|
@ -45,10 +45,10 @@ def get_raw_page(site, term, ssl=False):
|
||||||
raise IMException("article not found")
|
raise IMException("article not found")
|
||||||
|
|
||||||
|
|
||||||
def get_unwikitextified(site, wikitext, ssl=False):
|
def get_unwikitextified(site, wikitext, ssl=False, path="/w/api.php"):
|
||||||
# Built URL
|
# Built URL
|
||||||
url = "http%s://%s/w/api.php?format=json&action=expandtemplates&text=%s" % (
|
url = "http%s://%s%s?format=json&action=expandtemplates&text=%s" % (
|
||||||
"s" if ssl else "", site, urllib.parse.quote(wikitext))
|
"s" if ssl else "", site, path, urllib.parse.quote(wikitext))
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
data = web.getJSON(url)
|
data = web.getJSON(url)
|
||||||
|
@ -58,10 +58,10 @@ def get_unwikitextified(site, wikitext, ssl=False):
|
||||||
|
|
||||||
## Search
|
## Search
|
||||||
|
|
||||||
def opensearch(site, term, ssl=False):
|
def opensearch(site, term, ssl=False, path="/w/api.php"):
|
||||||
# Built URL
|
# Built URL
|
||||||
url = "http%s://%s/w/api.php?format=json&action=opensearch&search=%s" % (
|
url = "http%s://%s%s?format=json&action=opensearch&search=%s" % (
|
||||||
"s" if ssl else "", site, urllib.parse.quote(term))
|
"s" if ssl else "", site, path, urllib.parse.quote(term))
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
response = web.getJSON(url)
|
response = web.getJSON(url)
|
||||||
|
@ -73,10 +73,10 @@ def opensearch(site, term, ssl=False):
|
||||||
response[3][k])
|
response[3][k])
|
||||||
|
|
||||||
|
|
||||||
def search(site, term, ssl=False):
|
def search(site, term, ssl=False, path="/w/api.php"):
|
||||||
# Built URL
|
# Built URL
|
||||||
url = "http%s://%s/w/api.php?format=json&action=query&list=search&srsearch=%s&srprop=titlesnippet|snippet" % (
|
url = "http%s://%s%s?format=json&action=query&list=search&srsearch=%s&srprop=titlesnippet|snippet" % (
|
||||||
"s" if ssl else "", site, urllib.parse.quote(term))
|
"s" if ssl else "", site, path, urllib.parse.quote(term))
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
data = web.getJSON(url)
|
data = web.getJSON(url)
|
||||||
|
@ -108,9 +108,9 @@ def strip_model(cnt):
|
||||||
return cnt
|
return cnt
|
||||||
|
|
||||||
|
|
||||||
def parse_wikitext(site, cnt, namespaces=dict(), ssl=False):
|
def parse_wikitext(site, cnt, namespaces=dict(), **kwargs):
|
||||||
for i, _, _, _ in re.findall(r"({{([^{]|\s|({{(.|\s|{{.*?}})*?}})*?)*?}})", cnt):
|
for i, _, _, _ in re.findall(r"({{([^{]|\s|({{(.|\s|{{.*?}})*?}})*?)*?}})", cnt):
|
||||||
cnt = cnt.replace(i, get_unwikitextified(site, i, ssl), 1)
|
cnt = cnt.replace(i, get_unwikitextified(site, i, **kwargs), 1)
|
||||||
|
|
||||||
# Strip [[...]]
|
# Strip [[...]]
|
||||||
for full, args, lnk in re.findall(r"(\[\[(.*?|)?([^|]*?)\]\])", cnt):
|
for full, args, lnk in re.findall(r"(\[\[(.*?|)?([^|]*?)\]\])", cnt):
|
||||||
|
@ -139,8 +139,8 @@ def irc_format(cnt):
|
||||||
return cnt.replace("'''", "\x03\x02").replace("''", "\x03\x1f")
|
return cnt.replace("'''", "\x03\x02").replace("''", "\x03\x1f")
|
||||||
|
|
||||||
|
|
||||||
def get_page(site, term, ssl=False, subpart=None):
|
def get_page(site, term, subpart=None, **kwargs):
|
||||||
raw = get_raw_page(site, term, ssl)
|
raw = get_raw_page(site, term, **kwargs)
|
||||||
|
|
||||||
if subpart is not None:
|
if subpart is not None:
|
||||||
subpart = subpart.replace("_", " ")
|
subpart = subpart.replace("_", " ")
|
||||||
|
@ -151,50 +151,62 @@ def get_page(site, term, ssl=False, subpart=None):
|
||||||
|
|
||||||
# NEMUBOT #############################################################
|
# NEMUBOT #############################################################
|
||||||
|
|
||||||
def mediawiki_response(site, term, to):
|
def mediawiki_response(site, term, to, **kwargs):
|
||||||
ns = get_namespaces(site)
|
ns = get_namespaces(site, **kwargs)
|
||||||
|
|
||||||
terms = term.split("#", 1)
|
terms = term.split("#", 1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Print the article if it exists
|
# Print the article if it exists
|
||||||
return Response(get_page(site, terms[0], subpart=terms[1] if len(terms) > 1 else None),
|
return Response(get_page(site, terms[0], subpart=terms[1] if len(terms) > 1 else None, **kwargs),
|
||||||
line_treat=lambda line: irc_format(parse_wikitext(site, line, ns)),
|
line_treat=lambda line: irc_format(parse_wikitext(site, line, ns)),
|
||||||
channel=to)
|
channel=to)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Try looking at opensearch
|
# Try looking at opensearch
|
||||||
os = [x for x, _, _ in opensearch(site, terms[0])]
|
os = [x for x, _, _ in opensearch(site, terms[0], **kwargs)]
|
||||||
print(os)
|
print(os)
|
||||||
# Fallback to global search
|
# Fallback to global search
|
||||||
if not len(os):
|
if not len(os):
|
||||||
os = [x for x, _ in search(site, terms[0]) if x is not None and x != ""]
|
os = [x for x, _ in search(site, terms[0], **kwargs) if x is not None and x != ""]
|
||||||
return Response(os,
|
return Response(os,
|
||||||
channel=to,
|
channel=to,
|
||||||
title="Article not found, would you mean")
|
title="Article not found, would you mean")
|
||||||
|
|
||||||
|
|
||||||
@hook.command("mediawiki")
|
@hook.command("mediawiki",
|
||||||
|
help="Read an article on a MediaWiki",
|
||||||
|
keywords={
|
||||||
|
"ssl": "query over https instead of http",
|
||||||
|
"path=PATH": "absolute path to the API",
|
||||||
|
})
|
||||||
def cmd_mediawiki(msg):
|
def cmd_mediawiki(msg):
|
||||||
"""Read an article on a MediaWiki"""
|
|
||||||
if len(msg.args) < 2:
|
if len(msg.args) < 2:
|
||||||
raise IMException("indicate a domain and a term to search")
|
raise IMException("indicate a domain and a term to search")
|
||||||
|
|
||||||
return mediawiki_response(msg.args[0],
|
return mediawiki_response(msg.args[0],
|
||||||
" ".join(msg.args[1:]),
|
" ".join(msg.args[1:]),
|
||||||
msg.to_response)
|
msg.to_response,
|
||||||
|
ssl="ssl" in msg.kwargs,
|
||||||
|
path=msg.kwargs["path"] if "path" in msg.kwargs else "/w/api.php")
|
||||||
|
|
||||||
|
|
||||||
@hook.command("search_mediawiki")
|
@hook.command("search_mediawiki",
|
||||||
|
help="Search an article on a MediaWiki",
|
||||||
|
keywords={
|
||||||
|
"ssl": "query over https instead of http",
|
||||||
|
"path=PATH": "absolute path to the API",
|
||||||
|
})
|
||||||
def cmd_srchmediawiki(msg):
|
def cmd_srchmediawiki(msg):
|
||||||
"""Search an article on a MediaWiki"""
|
|
||||||
if len(msg.args) < 2:
|
if len(msg.args) < 2:
|
||||||
raise IMException("indicate a domain and a term to search")
|
raise IMException("indicate a domain and a term to search")
|
||||||
|
|
||||||
res = Response(channel=msg.to_response, nomore="No more results", count=" (%d more results)")
|
res = Response(channel=msg.to_response, nomore="No more results", count=" (%d more results)")
|
||||||
|
|
||||||
for r in search(msg.args[0], " ".join(msg.args[1:])):
|
for r in search(msg.args[0], " ".join(msg.args[1:]),
|
||||||
|
ssl="ssl" in msg.kwargs,
|
||||||
|
path=msg.kwargs["path"] if "path" in msg.kwargs else "/w/api.php"):
|
||||||
res.append_message("%s: %s" % r)
|
res.append_message("%s: %s" % r)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
Loading…
Reference in New Issue
Block a user