2013-09-09 13:27:18 +08:00
import json
2013-09-13 17:27:18 +08:00
import urllib2
2013-09-09 13:27:18 +08:00
2014-02-14 16:36:57 +13:00
from util import hook , http , web
2013-09-09 13:27:18 +08:00
2013-09-10 14:59:45 +12:00
def get_sound_info ( game , search ) :
2013-09-09 13:27:18 +08:00
search = search . replace ( " " , " + " )
try :
2013-09-13 17:45:13 +08:00
data = http . get_json ( " http://p2sounds.blha303.com.au/search/ %s / %s ?format=json " % ( game , search ) )
2013-09-13 17:27:18 +08:00
except urllib2 . HTTPError as e :
2013-09-09 13:27:18 +08:00
return " Error: " + json . loads ( e . read ( ) ) [ " error " ]
items = [ ]
2013-09-13 17:17:15 +08:00
for item in data [ " items " ] :
2013-09-09 13:50:06 +08:00
if " music " in game :
textsplit = item [ " text " ] . split ( ' " ' )
text = " "
for i in xrange ( len ( textsplit ) ) :
if i % 2 != 0 and i < 6 :
if text :
text + = " / " + textsplit [ i ]
else :
text = textsplit [ i ]
else :
text = item [ " text " ]
items . append ( " {} - {} {} " . format ( item [ " who " ] ,
text if len ( text ) < 325 else text [ : 325 ] + " ... " ,
2013-11-12 07:06:06 +01:00
item [ " listen " ] ) )
2013-09-09 13:27:18 +08:00
if len ( items ) == 1 :
return items [ 0 ]
else :
2013-11-12 07:06:06 +01:00
return " {} (and {} others: {} ) " . format ( items [ 0 ] , len ( items ) - 1 , web . haste ( " \n " . join ( items ) ) )
2013-09-09 13:27:18 +08:00
@hook.command
def portal2 ( inp ) :
""" portal2 <quote> - Look up Portal 2 quote.
Example : . portal2 demand to see life ' s manager " " "
2013-09-10 14:59:45 +12:00
return get_sound_info ( " portal2 " , inp )
2013-09-09 13:27:18 +08:00
@hook.command
def portal2dlc ( inp ) :
""" portal2dlc <quote> - Look up Portal 2 DLC quote.
Example : . portal2dlc1 these exhibits are interactive """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " portal2dlc1 " , inp )
2013-09-09 13:27:18 +08:00
@hook.command ( " portal2pti " )
@hook.command
def portal2dlc2 ( inp ) :
""" portal2dlc2 <quote> - Look up Portal 2 Perpetual Testing Inititive quote.
Example : . portal2 Cave here . """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " portal2dlc2 " , inp )
2013-09-09 13:27:18 +08:00
@hook.command
def portal2music ( inp ) :
""" portal2music <title> - Look up Portal 2 music.
Example : . portal2music turret opera """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " portal2music " , inp )
2013-09-09 13:27:18 +08:00
@hook.command ( ' portal1 ' )
@hook.command
def portal ( inp ) :
""" portal <quote> - Look up Portal quote.
Example : . portal The last thing you want to do is hurt me """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " portal1 " , i np )
2013-09-09 13:27:18 +08:00
@hook.command ( ' portal1music ' )
@hook.command
def portalmusic ( inp ) :
""" portalmusic <title> - Look up Portal music.
Example : . portalmusic still alive """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " portal1music " , inp )
2013-09-09 13:27:18 +08:00
@hook.command ( ' tf2sound ' )
@hook.command
def tf2 ( inp ) :
""" tf2 [who - ]<quote> - Look up TF2 quote.
Example : . tf2 may i borrow your earpiece """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " tf2 " , inp )
2013-09-09 13:27:18 +08:00
@hook.command
def tf2music ( inp ) :
""" tf2music title - Look up TF2 music lyrics.
Example : . tf2music rocket jump waltz """
2013-09-10 14:59:45 +12:00
return get_sound_info ( " tf2music " , inp )