2013-07-30 16:48:42 +08:00
import time
2013-12-02 22:55:58 +13:00
import random
2013-12-01 19:11:39 +13:00
2014-02-14 16:36:57 +13:00
from util import hook , http , web , text
2013-12-02 23:04:36 +13:00
## CONSTANTS
2013-12-01 19:11:39 +13:00
base_url = " http://api.bukget.org/3/ "
search_url = base_url + " search/plugin_name/like/ {} "
2013-12-02 22:55:58 +13:00
random_url = base_url + " plugins/bukkit/?start= {} &size=1 "
2013-12-01 19:11:39 +13:00
details_url = base_url + " plugins/bukkit/ {} "
2013-12-01 22:53:40 +13:00
categories = http . get_json ( " http://api.bukget.org/3/categories " )
2013-12-02 22:55:58 +13:00
count_total = sum ( [ cat [ " count " ] for cat in categories ] )
2014-02-14 16:30:38 +13:00
count_categories = { cat [ " name " ] . lower ( ) : int ( cat [ " count " ] ) for cat in categories } # dict comps!
2013-12-01 22:53:40 +13:00
2013-12-01 19:11:39 +13:00
class BukgetError ( Exception ) :
def __init__ ( self , code , text ) :
self . code = code
self . text = text
def __str__ ( self ) :
return self . text
2013-12-02 23:04:36 +13:00
## DATA FUNCTIONS
2013-12-02 23:03:29 +13:00
2013-12-01 19:11:39 +13:00
def plugin_search ( term ) :
""" searches for a plugin with the bukget API and returns the slug """
term = term . lower ( ) . strip ( )
search_term = http . quote_plus ( term )
try :
results = http . get_json ( search_url . format ( search_term ) )
except ( http . HTTPError , http . URLError ) as e :
raise BukgetError ( 500 , " Error Fetching Search Page: {} " . format ( e ) )
if not results :
raise BukgetError ( 404 , " No Results Found " )
for result in results :
if result [ " slug " ] == term :
return result [ " slug " ]
return results [ 0 ] [ " slug " ]
2013-12-02 22:55:58 +13:00
def plugin_random ( ) :
2013-12-02 23:06:26 +13:00
""" gets a random plugin from the bukget API and returns the slug """
2013-12-02 22:55:58 +13:00
results = None
while not results :
plugin_number = random . randint ( 1 , count_total )
print " trying {} " . format ( plugin_number )
try :
results = http . get_json ( random_url . format ( plugin_number ) )
except ( http . HTTPError , http . URLError ) as e :
raise BukgetError ( 500 , " Error Fetching Search Page: {} " . format ( e ) )
return results [ 0 ] [ " slug " ]
2013-12-01 19:11:39 +13:00
def plugin_details ( slug ) :
""" takes a plugin slug and returns details from the bukget API """
slug = slug . lower ( ) . strip ( )
try :
details = http . get_json ( details_url . format ( slug ) )
except ( http . HTTPError , http . URLError ) as e :
raise BukgetError ( 500 , " Error Fetching Details: {} " . format ( e ) )
return details
2013-07-30 16:48:42 +08:00
2013-12-02 23:03:29 +13:00
## OTHER FUNCTIONS
2013-07-30 16:48:42 +08:00
2013-12-02 23:03:29 +13:00
def format_output ( data ) :
""" takes plugin data and returns two strings representing information about that plugin """
2013-12-02 22:55:58 +13:00
name = data [ " plugin_name " ]
2013-12-02 23:10:08 +13:00
description = text . truncate_str ( data [ ' description ' ] , 30 )
2013-12-02 22:55:58 +13:00
url = data [ ' website ' ]
authors = data [ ' authors ' ] [ 0 ]
authors = authors [ 0 ] + u " \u200b " + authors [ 1 : ]
stage = data [ ' stage ' ]
current_version = data [ ' versions ' ] [ 0 ]
last_update = time . strftime ( ' %d % B % Y % H: % M ' ,
2014-02-14 16:30:38 +13:00
time . gmtime ( current_version [ ' date ' ] ) )
2013-12-02 22:55:58 +13:00
version_number = data [ ' versions ' ] [ 0 ] [ ' version ' ]
bukkit_versions = " , " . join ( current_version [ ' game_versions ' ] )
link = web . try_isgd ( current_version [ ' link ' ] )
if description :
2013-12-02 23:03:29 +13:00
line_a = u " \x02 {} \x02 , by \x02 {} \x02 - {} - ( {} ) \x02 {} " . format ( name , authors , description , stage , url )
2013-12-02 22:55:58 +13:00
else :
2013-12-02 23:03:29 +13:00
line_a = u " \x02 {} \x02 , by \x02 {} \x02 ( {} ) \x02 {} " . format ( name , authors , stage , url )
2013-12-02 22:55:58 +13:00
2014-02-13 15:09:22 +13:00
line_b = u " Last release: \x02 v {} \x02 for \x02 {} \x02 at {} \x02 {} \x02 " . format ( version_number , bukkit_versions ,
last_update , link )
2013-12-02 22:55:58 +13:00
2013-12-02 23:03:29 +13:00
return line_a , line_b
2013-12-02 22:55:58 +13:00
2013-12-02 23:04:36 +13:00
## HOOK FUNCTIONS
2013-12-02 23:03:29 +13:00
@hook.command ( ' plugin ' )
2013-12-12 15:06:29 +13:00
@hook.command
2013-12-11 17:31:57 +13:00
def bukget ( inp , reply = None , message = None ) :
""" bukget <slug/name> - Look up a plugin on dev.bukkit.org """
2013-12-02 23:03:29 +13:00
# get the plugin slug using search
2013-12-02 22:55:58 +13:00
try :
2013-12-02 23:03:29 +13:00
slug = plugin_search ( inp )
2013-12-02 22:55:58 +13:00
except BukgetError as e :
return e
# get the plugin info using the slug
2013-12-01 19:11:39 +13:00
try :
data = plugin_details ( slug )
except BukgetError as e :
return e
2013-12-02 23:03:29 +13:00
# format the final message and send it to IRC
line_a , line_b = format_output ( data )
2013-12-01 22:53:40 +13:00
2013-12-02 23:03:29 +13:00
reply ( line_a )
message ( line_b )
2013-07-30 16:48:42 +08:00
2013-12-01 19:11:39 +13:00
2013-12-02 23:03:29 +13:00
@hook.command ( autohelp = None )
def randomplugin ( inp , reply = None , message = None ) :
""" randomplugin - Gets a random plugin from dev.bukkit.org """
# get a random plugin slug
try :
slug = plugin_random ( )
except BukgetError as e :
return e
2013-12-01 19:11:39 +13:00
2013-12-02 23:03:29 +13:00
# get the plugin info using the slug
try :
data = plugin_details ( slug )
except BukgetError as e :
return e
# format the final message and send it to IRC
line_a , line_b = format_output ( data )
2013-12-01 19:11:39 +13:00
2013-12-02 23:03:29 +13:00
reply ( line_a )
message ( line_b )