2014-12-17 06:07:54 +00:00
from datetime import datetime
2014-12-16 06:09:15 +00:00
import urllib
2015-01-04 22:57:09 +00:00
from nemubot . exception import IRCException
2015-01-03 19:34:44 +00:00
from nemubot . hooks import hook
from nemubot . tools import human
from nemubot . tools . web import getJSON
2014-12-16 06:09:15 +00:00
2015-06-03 20:07:06 +00:00
nemubotversion = 4.0
2014-12-16 06:09:15 +00:00
from more import Response
URL_TPBAPI = None
def load ( context ) :
2015-06-03 20:07:06 +00:00
if not context . config or not context . config . hasAttribute ( " url " ) :
raise ImportError ( " You need a TPB API in order to use the !tpb feature "
" . Add it to the module configuration file: \n <module "
" name= \" tpb \" url= \" http://tpbapi.org/ \" /> \n Sample "
" API: "
" https://gist.github.com/colona/07a925f183cfb47d5f20 " )
2014-12-16 06:09:15 +00:00
global URL_TPBAPI
2015-06-03 20:07:06 +00:00
URL_TPBAPI = context . config [ " url " ]
2014-12-16 06:09:15 +00:00
2015-06-03 20:07:06 +00:00
@hook ( " cmd_hook " , " tpb " )
2014-12-16 06:09:15 +00:00
def cmd_tpb ( msg ) :
2015-06-03 20:07:06 +00:00
if not len ( msg . args ) :
2014-12-16 06:09:15 +00:00
raise IRCException ( " indicate an item to search! " )
2015-06-03 20:07:06 +00:00
torrents = getJSON ( URL_TPBAPI + urllib . parse . quote ( " " . join ( msg . args ) ) )
2014-12-16 06:09:15 +00:00
res = Response ( channel = msg . channel , nomore = " No more torrents " , count = " ( %d more torrents) " )
if torrents :
for t in torrents :
2014-12-17 06:07:54 +00:00
t [ " sizeH " ] = human . size ( t [ " size " ] )
t [ " dateH " ] = datetime . fromtimestamp ( t [ " date " ] ) . strftime ( ' % Y- % m- %d % H: % M: % S ' )
2014-12-19 06:27:27 +00:00
res . append_message ( " \x03 \x02 {title} \x03 \x02 in {category} , {sizeH} ; added at {dateH} ; id: {id} ; magnet:?xt=urn:btih: {magnet} &tr=udp % 3A %2F %2F tracker.openbittorrent.com % 3A80&tr=udp % 3A %2F %2F tracker.publicbt.com % 3A80&tr=udp % 3A %2F %2F tracker.istole.it % 3A6969&tr=udp % 3A %2F %2F open.demonii.com % 3A1337 " . format ( * * t ) )
2014-12-16 06:09:15 +00:00
return res