From c4360a81aaf49ef9aa492194c395fd4054a3610f Mon Sep 17 00:00:00 2001 From: Abhay Rana Date: Sun, 30 Dec 2012 13:24:21 +0530 Subject: [PATCH] terminal IMDB --- imdb | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 imdb diff --git a/imdb b/imdb new file mode 100755 index 0000000..248d398 --- /dev/null +++ b/imdb @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import urllib +import argparse +from xml.etree import ElementTree +import sys + +def retrieveMovie (title): + title=urllib.quote(title.encode("utf8")) + URL="http://www.omdbapi.com/?r=xml&plot=full&t=%s" % title + xml = ElementTree.parse(urllib.urlopen(URL)) + #fall back to movie search if no movie is found + for A in xml.iter('root'): + if (A.get('response')=='False'): + print "Movie not found!" + sys.exit() + xml=xml.getroot() + printInfo(xml) + return xml + +#Search for movie and return plot for all the results +def movieSearch (title): + title=urllib.quote(title.encode("utf8")) + URL="http://www.omdbapi.com/?r=xml&s=%s" % title + xml = ElementTree.parse(urllib.urlopen(URL)) + xml=xml.getroot() + for B in xml.findall('Movie'): + apicall=retrieveMovie(B.get('Title')) + printInfo(apicall) + return xml + +def printInfo(xml): + for B in xml.findall('movie'): + print "\n%s (%s) || %s || %s\n" %(B.get('title'),B.get('year'), + B.get('runtime'),B.get('imdbRating')) + print "Director: %s\nActors: %s\n" %(B.get('director'),B.get('actors')) + print "%s\n" %(B.get('plot')) + +if __name__=='__main__': + parser=argparse.ArgumentParser(description='Command-Line Interface for the IMdB') + + parser.add_argument("-t", help="Search by title. Return first result") + parser.add_argument("-s", help="Search and return results") + + args=parser.parse_args() + choices=["None"] + try: + choices[0]=sys.argv[1] + title=sys.argv[2] + except: + parser.print_usage() + sys.exit() + + if choices[0]=="-t": + retrieveMovie(title) + else: + movieSearch(title) + +