from willie import module
import urllib
import json

@module.commands('anagram')
@module.priority('low')
def anagram(bot, trigger):
    try:
        url = "http://www.anagramica.com/best/:" + urllib.quote(trigger.group(2))
        data = urllib.urlopen(url).read().decode("utf-8")
        result = json.loads(data)
        bot.say(result.get("best")[0])
    except Exception as e:
        bot.say('Sorry, I couldn\'t make an anagram out of that.')
