Created
August 17, 2010 04:21
-
-
Save wvega/528444 to your computer and use it in GitHub Desktop.
IMDB Movie Sorter
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
#!/usr/bin/python | |
import imdb | |
import os | |
import re | |
# (?:\[.+) everything after an opening bracket | |
# (?:(?:\.|\()(?:\d{4}.*)) everything after a period followed by a year | |
# (?:-?(?:aXXo|FXG|DvDrip.*)) torrent uploader signature | |
regex = {'noise': re.compile(r'(?:\[.+)|(?:(?:\.|\()(?:\d{4}.*))|(?:-?(?:aXXo|FXG|DvDrip.*))', flags=re.IGNORECASE), | |
'year': re.compile(r'(\d{4})')} | |
ia = imdb.IMDb() | |
missing = [] | |
movies = [] | |
for name in os.listdir(os.getcwd()): | |
if name == 'ranking.txt' or name == 'imdb-sort.py': | |
continue | |
# get the name of the movie | |
year = regex['year'].search(name) | |
name = regex['noise'].sub('', name).replace('.', ' ') | |
movie = (name, year and int(year.group(0)) or 0) | |
# search | |
result = ia.search_movie('%s' % movie[0]) | |
matches = [] | |
if movie[1] > 0: | |
for item in result: | |
if item['year'] == movie[1]: | |
matches.append(item) | |
else: | |
matches = result | |
print '%d movies found for "%s (%s)"' % (len(matches), movie[0], movie[1]) | |
# get detailed info for selected movie (if any) | |
# if there is more than one possible movie, the first one is selected. | |
if len(matches) > 0: | |
selected = matches[0] | |
ia.update(selected) | |
movies.append(selected) | |
if len(matches) > 1: | |
print '"%s" was selected. The rating is %0.2f\n' % (selected['title'], selected['rating']) | |
else: | |
print '"%s" has a rating of is %0.2f\n' % (selected['title'], selected['rating']) | |
else: | |
missing.append(movie) | |
print 'No results found for "%s". This movie won\'t be included in the results. \n' % movie[0] | |
# sort movies list by ranking and save it to a file | |
ranking = open('ranking.txt', 'w') | |
ranking.write('Movies list ordered by IMDB ranking\n') | |
ranking.write('-----------------------------------\n\n') | |
for m in sorted(movies, cmp = lambda x,y : int(10 * (y['rating'] - x['rating']))): | |
ranking.write('%s (%0.2f)\n' % (m['title'], m['rating'])) | |
ranking.write('\n\n\nIMDB didn\'t return enough data to rank the following movies') | |
ranking.write('\n------------------------------------------------------------\n\n'') | |
for m in sorted(missing, key = lambda x: x[0]): | |
ranking.write('%s\n' % m[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment