Skip to content

Instantly share code, notes, and snippets.

@BaseCase
Last active August 29, 2015 14:07
Show Gist options
  • Save BaseCase/38133b5d42af612c7e02 to your computer and use it in GitHub Desktop.
Save BaseCase/38133b5d42af612c7e02 to your computer and use it in GitHub Desktop.
uses OMDB to grab running times for a list of movie titles
# Requires the 'requests' lib to be installed in your python path
# usage: `python movie_time.py "title 1" "title 2" "title 3" (etc.)`
# OR: `python movie_time.py -f filename` # file format should be newline separated list of titles
#
# output is a list of run times for each movie plus the total
# it will return 0 for the runtime of a movie it can't find
import sys
import requests
def main():
titles = get_titles_from_args()
runtimes = [get_runtime(movie) for movie in titles]
for title, runtime in zip(titles, runtimes):
sys.stdout.write("{0}: {1} minutes\n".format(title, runtime))
total = sum(runtimes)
sys.stdout.write("\nTotal: {} minutes ({:.1f} hours)\n".format(total, total / 60.0))
def get_titles_from_args():
if '-f' in sys.argv: # read titles from a file
if len(sys.argv) > 3:
sys.stdout.write('pls only 1 filename with -f\n')
sys.exit(1)
args = sys.argv[1:]
args.remove('-f')
filename = args[0]
with open(filename) as f:
titles = [t.strip() for t in f.readlines()]
else: # read titles from args
titles = sys.argv[1:]
return titles
def get_runtime(title):
res = requests.get('http://omdbapi.com/?t={}'.format(title))
minutes = res.json().get('Runtime', '0 min')
if minutes == 'N/A': minutes = '0 min'
return int(minutes.split()[0])
if __name__ == '__main__':
main()
sys.exit(0)
@BaseCase
Copy link
Author

EXAMPLE:

$ python movie_time.py -f david_lynch.txt 
Eraserhead: 89 minutes
The Elephant Man: 124 minutes
Dune: 137 minutes
Blue Velvet: 120 minutes
Wild at Heart: 125 minutes
Lost Highway: 134 minutes
The Straight Story: 112 minutes
Mulholland Drive: 147 minutes
Inland Empire: 180 minutes

Total: 1168 minutes (19.5 hours)

@drewsday
Copy link

There's a bug in david_lynch.txt. You have listed Dune which is a movie no human should have to sit through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment