This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c49d52
commit 231a02b
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
|