From 99106d26a9e7bb1d1e0f440f0597b39e8096f440 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Tue, 16 Dec 2014 07:09:15 +0100 Subject: [PATCH] Fix #70: new module tpb using an API with a TPB dump --- modules/tpb.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 modules/tpb.py diff --git a/modules/tpb.py b/modules/tpb.py new file mode 100644 index 0000000..ab4d23c --- /dev/null +++ b/modules/tpb.py @@ -0,0 +1,39 @@ +import urllib + +from tools.web import getJSON + +nemubotversion = 3.4 + +from hooks import hook +from more import Response + +URL_TPBAPI = None + +def load(context): + global URL_TPBAPI + + if not CONF or not CONF.hasNode("tpbapi") or not CONF.getNode("tpbapi").hasAttribute("url"): + print ("You need a TPB API in order to use the !tpb feature. Add it to " + "the module configuration file:\n" + "\nSample API: " + "https://gist.github.com/colona/07a925f183cfb47d5f20") + else: + URL_TPBAPI = CONF.getNode("tpbapi")["url"] + + from hooks.messagehook import MessageHook + add_hook("cmd_hook", MessageHook(cmd_tpb, "tpb")) + + +def cmd_tpb(msg): + if len(msg.cmds) < 1: + raise IRCException("indicate an item to search!") + + torrents = getJSON(URL_TPBAPI + urllib.parse.quote(" ".join(msg.cmds[1:]))) + + res = Response(channel=msg.channel, nomore="No more torrents", count=" (%d more torrents)") + + if torrents: + for t in torrents: + res.append_message("\x03\x02{title}\x03\x02 in {category}, {size}B; id: {id}; magnet: {magnet}".format(**t)) + + return res