redesigned bukkit plugin search (still needs work and readding random plugin)

This commit is contained in:
Luke Rogers 2013-12-01 19:11:39 +13:00
parent a37fc245d0
commit b3a8703d15

View file

@ -1,73 +1,89 @@
from util import hook, http, web from util import hook, http, web
import time import time
import json
from urllib2 import HTTPError base_url = "http://api.bukget.org/3/"
import random
from os import path search_url = base_url + "search/plugin_name/like/{}"
details_url = base_url + "plugins/bukkit/{}"
@hook.command('randomplugin') class BukgetError(Exception):
@hook.command(autohelp=False) def __init__(self, code, text):
def randombukkitplugin(inp, reply=None): self.code = code
if not path.exists("plugins/data/bukgetplugins"): self.text = text
with open("plugins/data/bukgetplugins", "w") as f:
f.write(http.get("http://api.bukget.org/3/plugins/bukkit")) def __str__(self):
jsahn = json.loads(open("plugins/data/bukgetplugins", "r").read()) return self.text
pickslug = random.choice(jsahn)['slug']
data = getplugininfo(pickslug)
name = data['plugin_name'] def plugin_search(term):
description = data['description'] """ searches for a plugin with the bukget API and returns the slug """
url = data['website'] term = term.lower().strip()
authors = data['authors'][0]
authors = authors[0] + u"\u200b" + authors[1:] search_term = http.quote_plus(term)
stage = data['stage']
lastUpdate = time.strftime('%d %B %Y %H:%M', try:
time.gmtime(data['versions'][0]['date'])) results = http.get_json(search_url.format(search_term))
lastVersion = data['versions'][0]['version'] except (http.HTTPError, http.URLError) as e:
bukkitver = ", ".join(data['versions'][0]['game_versions']) raise BukgetError(500, "Error Fetching Search Page: {}".format(e))
link = web.isgd(data['versions'][0]['link'])
if description != "": if not results:
reply("\x02{}\x02, by \x02{}\x02 - {} - ({}) \x02{}".format(name, authors, description, stage, url)) raise BukgetError(404, "No Results Found")
else:
reply("\x02{}\x02, by \x02{}\x02 ({}) \x02{}".format(name, authors, stage, url)) for result in results:
reply("Last release: \x02v{}\x02 for \x02{}\x02 at {} \x02{}\x02".format(lastVersion, bukkitver, lastUpdate, link)) if result["slug"] == term:
return result["slug"]
return results[0]["slug"]
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
@hook.command('bplugin')
@hook.command('plugin') @hook.command('plugin')
@hook.command @hook.command
def bukkitplugin(inp, reply=None): def bukkitplugin(inp, reply=None):
"""plugin <bukkit plugin slug> - Look up a plugin on dev.bukkit.org""" """plugin <slug/name> - Look up a plugin on dev.bukkit.org"""
data = getplugininfo(inp.lower()) # get the plugin slug using search
try: try:
name = data['plugin_name'] slug = plugin_search(inp)
except ValueError: except BukgetError as e:
return data 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'] description = data['description']
url = data['website'] url = data['website']
authors = data['authors'][0] authors = data['authors'][0]
authors = authors[0] + u"\u200b" + authors[1:] authors = authors[0] + u"\u200b" + authors[1:]
stage = data['stage'] stage = data['stage']
lastUpdate = time.strftime('%d %B %Y %H:%M',
time.gmtime(data['versions'][0]['date']))
lastVersion = data['versions'][0]['version']
bukkitver = ", ".join(data['versions'][0]['game_versions'])
link = web.isgd(data['versions'][0]['link'])
if description != "":
reply("\x02{}\x02, by \x02{}\x02 - {} - ({}) \x02{}".format(name, authors, description, stage, url))
else:
reply("\x02{}\x02, by \x02{}\x02 ({}) \x02{}".format(name, authors, stage, url))
reply("Last release: \x02v{}\x02 for \x02{}\x02 at {} \x02{}\x02".format(lastVersion, bukkitver, lastUpdate, link))
current_version = data['versions'][0]
def getplugininfo(inp): last_update = time.strftime('%d %B %Y %H:%M',
if len(inp.split(" ")) > 1: time.gmtime(current_version['date']))
slug = inp.split(" ")[0] 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: else:
slug = inp reply(u"\x02{}\x02, by \x02{}\x02 ({}) \x02{}".format(name, authors, stage, url))
try:
data = http.get_json("http://api.bukget.org/3/plugins/bukkit/%s/" reply(u"Last release: \x02v{}\x02 for \x02{}\x02 at {} \x02{}\x02".format(version_number, bukkit_versions, last_update, link))
% slug)
except HTTPError as e:
return "Got error: {}".format(e)
return data