Created
March 28, 2014 20:42
-
-
Save EntityReborn/9842567 to your computer and use it in GitHub Desktop.
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
[general] | |
name = CHAPI# human friendly name | |
modulefile = chapi.py# Actual module to import to load plugins | |
enabled = True |
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
from socbot.pluginbase import Base, BadParams | |
from twisted.internet import reactor | |
from twisted.internet.defer import Deferred | |
from twisted.internet.protocol import Protocol | |
from twisted.web.client import Agent | |
from bs4 import BeautifulSoup | |
import json, re | |
class NoSuchFunction(Exception): pass | |
apiurl = "http://wiki.sk89q.com/w/api.php?format=json&action=query&titles=%s&prop=revisions&rvprop=content" | |
regex = r"""\|- id="(?P<id>%s)" | |
! scope="row" \| \[\[[^]]+\]\](\(\))? | |
\| (?P<returns>.*?) | |
\| (?P<arguments>.*?) | |
\| (?P<raises>((\[\[[^]]+\]\])(<br />\r?\n)?)*) | |
\| (?P<definition>.*?) | |
\| (?P<since>.*?) | |
\| <div[^>]*>(?P<protected>[^<]+)</div>""" | |
class DeferredPrinter(Protocol): | |
def __init__(self, finished, url, page, word): | |
self.finished = finished | |
self.url = url | |
self.page = page | |
self.word = word | |
self.data = "" | |
def dataReceived(self, b): | |
self.data += b | |
def stripTags(self, value): | |
if "<hr />" in value: | |
value = value.replace("<hr />", '-or-') | |
soup = BeautifulSoup(value) | |
return soup.get_text() | |
def connectionLost(self, reason): | |
data = json.loads(self.data) | |
content = data['query']['pages'].values()[0]['revisions'][0]['*'] | |
if self.word.endswith("()"): | |
self.word = self.word.replace("()", "") | |
m = re.search(regex % self.word.lower(), content) | |
if m: | |
defn = self.stripTags(m.group('definition')) | |
args = m.group('arguments') | |
accepts = self.stripTags(args) | |
rtns = m.group('returns') | |
returns = self.stripTags(rtns) | |
url = "http://wiki.sk89q.com/wiki/%s#%s" % (self.page, self.word) | |
response = "{func} // Returns {retn} // Expects {exp} // {defn} {url}".format( | |
func=self.word.lower(), url=url, exp=accepts, retn=returns, defn=defn) | |
self.finished.callback(response) | |
else: | |
self.finished.errback(NoSuchFunction) | |
def getUrl(apipage, word): | |
url = apiurl % apipage | |
agent = Agent(reactor) | |
d = agent.request('GET', url) | |
def cbRequest(response): | |
finished = Deferred() | |
response.deliverBody(DeferredPrinter(finished, url, apipage, word)) | |
return finished | |
def cbErr(failure): | |
return "'%s' is not a valid function in the api." % word | |
d.addCallback(cbRequest) | |
d.addErrback(cbErr) | |
return d | |
class Plugin(Base): # Must subclass Base | |
@Base.trigger("API") | |
def on_api(self, bot, user, details): | |
"""API <id> - request link to api function""" | |
if len(details['splitmsg']) < 1: | |
return "http://wiki.sk89q.com/wiki/CommandHelper/API" | |
url = getUrl("CommandHelper/API", details['splitmsg'][0]) | |
return url | |
@Base.trigger("STAGED") | |
def on_staged(self, bot, user, details): | |
"""STAGED <id> - request link to staged api function""" | |
if len(details['splitmsg']) < 1: | |
return "http://wiki.sk89q.com/wiki/CommandHelper/Staged/API" | |
url = getUrl("CommandHelper/Staged/API", details['splitmsg'][0]) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment