add !crate in rust.py
This commit is contained in:
parent
d6960865d6
commit
f88b054f38
1 changed files with 20 additions and 0 deletions
|
@ -3,6 +3,9 @@
|
|||
import json, socket
|
||||
from src import ModuleManager, utils
|
||||
|
||||
API_CRATE = "https://crates.io/api/v1/crates/%s"
|
||||
URL_CRATE = "https://crates.io/crates/%s"
|
||||
|
||||
EVAL_URL = "https://play.rust-lang.org/execute"
|
||||
FN_TEMPLATE = """
|
||||
fn main() {
|
||||
|
@ -40,3 +43,20 @@ class Module(ModuleManager.BaseModule):
|
|||
err_or_out = "stdout" if page.data["success"] else "stderr"
|
||||
event[err_or_out].write("%s: %s" % (event["user"].nickname,
|
||||
page.data[err_or_out].strip("\n")))
|
||||
|
||||
@utils.hook("received.command.crate")
|
||||
@utils.kwarg("min_args", 1)
|
||||
@utils.kwarg("help", "Look up a given Rust crate on crates.io")
|
||||
@utils.kwarg("usage", "<crate-name>")
|
||||
def crate(self, event):
|
||||
query = event["args_split"][0]
|
||||
request = utils.http.Request(API_CRATE % query, json=True)
|
||||
response = utils.http.request(request)
|
||||
if response.code == 200:
|
||||
crate = response.data["crate"]
|
||||
name = crate["id"]
|
||||
url = URL_CRATE % name
|
||||
event["stdout"].write("%s %s: %s - %s" % (
|
||||
name, crate["max_version"], crate["description"], url))
|
||||
else:
|
||||
event["stderr"].write("Crate '%s' not found" % query)
|
||||
|
|
Loading…
Reference in a new issue