From 339d853258e364c4fbca777e3d07dd2907e6e39e Mon Sep 17 00:00:00 2001 From: melonhead Date: Tue, 26 Apr 2011 02:17:55 +0800 Subject: [PATCH] Added Rotten Tomatoes command --- core/config.py | 1 + plugins/rottentomatoes.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 plugins/rottentomatoes.py diff --git a/core/config.py b/core/config.py index 0649b50..1b2cacb 100644 --- a/core/config.py +++ b/core/config.py @@ -22,6 +22,7 @@ if not os.path.exists('config'): "disabled_plugins": [], "disabled_commands": [], "acls": {}, + "api_keys": {}, "censored_strings": [ "DCC SEND", diff --git a/plugins/rottentomatoes.py b/plugins/rottentomatoes.py new file mode 100644 index 0000000..5e20c43 --- /dev/null +++ b/plugins/rottentomatoes.py @@ -0,0 +1,36 @@ +from util import http, hook + +api_root = 'http://api.rottentomatoes.com/api/public/v1.0/' +movie_search_url = api_root+'movies.json?q=%s&apikey=%s' +movie_info_url = api_root+'movies/%s.json?apikey=%s' +movie_reviews_url = api_root+'movies/%s/reviews.json?apikey=%s&review_type=all' + +response = u"%s - critics: \x02%d%%\x02 (%d\u2191%d\u2193) audience: \x02%d%%\x02 - %s" + +@hook.command('rt') +def rottentomatoes(inp, bot=None): + '.rt -- gets ratings for <title> from Rotten Tomatoes' + + api_key = bot.config.get("api_keys", {}).get("rottentomatoes", None) + if not api_key: + return None + + title = inp.strip() + + results = http.get_json(movie_search_url % (http.quote_plus(title), api_key)) + if results['total'] > 0: + movie = results['movies'][0] + title = movie['title'] + id = movie['id'] + critics_score = movie['ratings']['critics_score'] + audience_score = movie['ratings']['audience_score'] + url = movie['links']['alternate'] + + if critics_score != -1: + reviews = http.get_json(movie_reviews_url%(id, api_key)) + review_count = reviews['total'] + + fresh = critics_score * review_count / 100 + rotten = review_count - fresh + + return response % (title, critics_score, fresh, rotten, audience_score, url) \ No newline at end of file