old_contents: import string
  import re
  import utility
  from commands import Command
  
  def imdb_info(url):
  	response = utility.read_url(url)
  	data = response["data"]
  	
  	m = re.search('<title>(.*?) \((\d+\/?I?)\)<\/title>', data)
  	if m:
  		title = m.group(1)
  		year = m.group(2)
  	else:
  		title = ""
  		year = ""
  
  	m = re.search('<b>([0-9\.]+)\/10<\/b>', data)
  	if m:
  		rating = m.group(1)
  	else:
  		rating = 0
  
  	m = re.search('<h5>Plot Summary:</h5> \n(.*?)\n<a', data)
  	if m:
  		tagline = "\"%s\" -" % m.group(1)
  	else:
  		m = re.search('<h5>Plot Outline:</h5> \n(.*?) <a', data)
  		if m:
  			tagline = "\"%s\" -" % m.group(1)
  		else:
  			tagline = ""
  
  	m = re.findall('"\/Sections\/Genres\/(.*?)\/', data)
  	if m:
  		genres = ", ".join(set(m))
  	else:
  		genres = ""
  
  	return "%s (%s) - Rating: %s/10 - Genre: %s - %s %s" % (title, year, rating, genres, tagline, url)
  
  def imdb_search(name):
  #	url = "http://akas.imdb.com/find?s=tt&q=%s" % name.replace(" ","+")
  	url = "http://www.imdb.com/find?s=tt&q=%s" % utility.escape(name)
  
  	response = utility.read_url(url)
  	data = response["data"]
  
  #	print url
  	
  	m = re.search('<title>(.*?) \((\d+)\)<\/title>', data)
  	if m:
  		return imdb_info(url)
  	
  	m = re.search('<a href="(\/(title)\/.*?)"', data)
  	if m:
  		url = "http://www.imdb.com%s" % m.group(1)
  		return imdb_info(url)
  
  class ImdbCommand(Command):
  	def trig_imdb(self, bot, source, target, trigger, argument):
  		return imdb_search(argument)
  		
  
  #imdb_search("Rob Roy")
  #imdb_search("Layer Cake")
  #print imdb_search("The Last King of Scotland")
  #print imdb_search("Die Hard")
  
new_contents: # -*- coding: utf-8 -*-
  
  import utility
  from commands import Command
  
  from json import JSONDecoder
  
  class IMDbCommand(Command):
  	usage = "Usage: .imdb <search term>"
  	
  	def __init__(self):
  		pass
  
  	def query(self, argument):
  		decoder = JSONDecoder()
  		argument = utility.escape(argument)
  		api_url = u"http://www.imdbapi.com/?t=%(search_term)s&r=json&plot=short" % \
  				{"search_term": argument}
  		site_search_url = u"http://akas.imdb.com/find?s=all&q=" + argument
  		response = utility.read_url(api_url)
  
  		if not response:
  			return u"Couldn't connect to the API :( | Manual search: " + site_search_url
  
  		try:
  			data = decoder.decode(response['data'])
  		except Error:
  			return u"Couldn't parse the API output :( | Manual search: " + site_search_url
  		
  		if data.get(u"Response") != u"True":
  			return u"No results found! Maybe you should try searching manually: " + \
  					site_search_url
  
  		return \
  				(u"%(title)s (%(year)s) - Rating: %(rating)s out of 10 - Genre: %(genre)s - " + \
  				u"http://akas.imdb.com/title/%(id)s/ | More results: %(site_search_url)s") % \
  				{u"title": data.get(u"Title", u"Missing title :S"),
  					u"year": data.get(u"Year", u"Unknown year"),
  					u"rating": data.get(u"Rating", u"N/A"),
  					u"genre": data.get(u"Genre", u"Unknown"),
  					u"id": data.get(u"ID", u"tt0107838"),
  					u"site_search_url": site_search_url}
  		
  	def trig_imdb(self, bot, source, target, trigger, argument):
  		"""Command that queries IMDb - The Internet Movie Database."""
  		
  		argument = argument.strip()
  		
  		# Show usage
  		if not argument:
  			return "IMDb - The Internet Movie Database. " + self.usage
  				
  		# Query API
  		else:
  			return self.query(argument).encode("utf-8")
  
  
subject: Complete rewrite of IMDb command, using http://imdbapi.com/