Esempio n. 1
0
def run(MOVIE_DIR, HTML_OUTPUT_FLAG, LIMIT):

    movielookup = MovieLookup()                         #A class to help lookup movie titles
    movieDataUtil = MovieDataUtil()                     #A helper class for movie json data
    matcher = Matcher(movieMatchRegex, allowedFiletypes)#Match files in a given directory
    normaliser = Normaliser()                           #
    idFinder = IdFinder()                               #Used to find an imdb id from movie filename

    #First, let's match files which match the regex and have the required file extensions in the given directory
    matcher.findInDirectory(MOVIE_DIR)
    movieMatches = matcher.getMatches()
    unMatched = matcher.getIgnored()

    #normalise the matches (the filenames will be used as movie titles)
    normalisedMovieMatches = []
    for item in movieMatches:
        normalisedItem = item
        normalisedItem = normaliser.removeTrailingNumber(normalisedItem)
        normalisedItem = normaliser.normalise(normalisedItem)
        normalisedMovieMatches.append(normalisedItem)

    #Now we lookup successful matches
    movieData = {}      #successful lookup data will go here
    failedLookups = []  #we will do something with failed lookups later...

    count = 0   #used to limit the number of lookups we will do
    for title in normalisedMovieMatches:
        count += 1
        if count >= LIMIT:#check that we don't go over the arbitrary limit
            break

        #look up each movie in the list
        lookupData = movielookup.lookupByTitle(title)

        #check if we found a movie
        if movieDataUtil.isValidLookupResult(lookupData):
            movieData[title] = lookupData
        else:
            failedLookups.append(title)

    #now we will try to correct the failed lookups by using google to find each imdb id
    idLookupDict = idFinder.findIdByTitleList(failedLookups)

    #reset the failed lookups
    failedLookups = []      #there should be a lot less now...
    titleCorrections = 0    #count how many corrections we actually found

    #Now lookup using the new ids which we found
    for title, foundId in idLookupDict.items():
        if foundId != None:
            #we found an id, now let's look the movie up by its id
            lookupData = movielookup.lookupById(foundId)

            #theoretically this should always be true unless we got an invalid id somehow...
            if movieDataUtil.isValidLookupResult(lookupData):
                movieData[title] = lookupData
                titleCorrections += 1
            else:
                failedLookups.append(title)
        else:
            failedLookups.append(title)

    #sort the data by imdb id
    movieData = movieDataUtil.sortMovieData(movieData)

    #Output the data
    if HTML_OUTPUT_FLAG:
        templateEnvironment = Environment(loader=FileSystemLoader(templateDirectory),trim_blocks=True)
        print templateEnvironment.get_template('main.html').render(
            movieLookupData=movieData,
            failedLookups=failedLookups,
            unMatched=unMatched,
            titleCorrections=titleCorrections,
            dateTime = time.strftime("%c"),
            version = __version__,
        )
    else:
        simpleOutput(movieData, failedLookups, unMatched)