re-added .randomplugin @blha303
This commit is contained in:
parent
91c827a03b
commit
3df53f95a8
1 changed files with 63 additions and 2 deletions
|
@ -1,13 +1,17 @@
|
||||||
from util import hook, http, web
|
from util import hook, http, web
|
||||||
import time
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
base_url = "http://api.bukget.org/3/"
|
base_url = "http://api.bukget.org/3/"
|
||||||
|
|
||||||
search_url = base_url + "search/plugin_name/like/{}"
|
search_url = base_url + "search/plugin_name/like/{}"
|
||||||
|
random_url = base_url + "plugins/bukkit/?start={}&size=1"
|
||||||
details_url = base_url + "plugins/bukkit/{}"
|
details_url = base_url + "plugins/bukkit/{}"
|
||||||
|
|
||||||
categories = http.get_json("http://api.bukget.org/3/categories")
|
categories = http.get_json("http://api.bukget.org/3/categories")
|
||||||
total_plugins = sum([cat["count"] for cat in categories])
|
|
||||||
|
count_total = sum([cat["count"] for cat in categories])
|
||||||
|
count_categores = {cat["name"].lower() : int(cat["count"]) for cat in categories} # dict conps!
|
||||||
|
|
||||||
|
|
||||||
class BukgetError(Exception):
|
class BukgetError(Exception):
|
||||||
|
@ -40,6 +44,22 @@ def plugin_search(term):
|
||||||
return results[0]["slug"]
|
return results[0]["slug"]
|
||||||
|
|
||||||
|
|
||||||
|
def plugin_random():
|
||||||
|
""" returns the slug of a random plugin from the bukget API """
|
||||||
|
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"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def plugin_details(slug):
|
def plugin_details(slug):
|
||||||
""" takes a plugin slug and returns details from the bukget API """
|
""" takes a plugin slug and returns details from the bukget API """
|
||||||
slug = slug.lower().strip()
|
slug = slug.lower().strip()
|
||||||
|
@ -57,7 +77,7 @@ def plugin_details(slug):
|
||||||
def bukkitplugin(inp, reply=None, message=None):
|
def bukkitplugin(inp, reply=None, message=None):
|
||||||
"""plugin <slug/name> - Look up a plugin on dev.bukkit.org"""
|
"""plugin <slug/name> - Look up a plugin on dev.bukkit.org"""
|
||||||
# get the plugin slug using search
|
# get the plugin slug using search
|
||||||
print total_plugins
|
print count_categores
|
||||||
try:
|
try:
|
||||||
slug = plugin_search(inp)
|
slug = plugin_search(inp)
|
||||||
except BukgetError as e:
|
except BukgetError as e:
|
||||||
|
@ -92,3 +112,44 @@ def bukkitplugin(inp, reply=None, message=None):
|
||||||
reply(u"\x02{}\x02, by \x02{}\x02 ({}) \x02{}".format(name, authors, stage, url))
|
reply(u"\x02{}\x02, by \x02{}\x02 ({}) \x02{}".format(name, authors, stage, url))
|
||||||
|
|
||||||
message(u"Last release: \x02v{}\x02 for \x02{}\x02 at {} \x02{}\x02".format(version_number, bukkit_versions, last_update, link))
|
message(u"Last release: \x02v{}\x02 for \x02{}\x02 at {} \x02{}\x02".format(version_number, bukkit_versions, last_update, link))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
# get the plugin info using the slug
|
||||||
|
try:
|
||||||
|
data = plugin_details(slug)
|
||||||
|
except BukgetError as e:
|
||||||
|
return e
|
||||||
|
|
||||||
|
|
||||||
|
name = data["plugin_name"]
|
||||||
|
description = data['description']
|
||||||
|
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',
|
||||||
|
time.gmtime(current_version['date']))
|
||||||
|
version_number = data['versions'][0]['version']
|
||||||
|
|
||||||
|
bukkit_versions = ", ".join(current_version['game_versions'])
|
||||||
|
link = web.try_isgd(current_version['link'])
|
||||||
|
|
||||||
|
if description:
|
||||||
|
reply(u"\x02{}\x02, by \x02{}\x02 - {} - ({}) \x02{}".format(name, authors, description, stage, url))
|
||||||
|
else:
|
||||||
|
reply(u"\x02{}\x02, by \x02{}\x02 ({}) \x02{}".format(name, authors, stage, url))
|
||||||
|
|
||||||
|
message(u"Last release: \x02v{}\x02 for \x02{}\x02 at {} \x02{}\x02".format(version_number, bukkit_versions, last_update, link))
|
||||||
|
|
Reference in a new issue