Example #1
0
def main():
    args = process_arguments()
    movies_list = []
    os.path.dirname(os.path.realpath(__file__))

    # Single file should be checked
    if args.path[0] == "single":
        movies_list = get_list_of_files(args.path[1], "single")
    # Folder should be scanned recusrivly
    elif args.path[0] == "folder" and args.recursive:
        movies_list = get_list_of_files(args.path[1], "recursive")
    # Folder
    else:
        movies_list = get_list_of_files(args.path[1], "folder")

    print "Working..."

    # Obtain imdb id for each movie file
    movie_dict = {}
    movie_dict = OpenSubtitles.get_movie_data(movies_list)
    del movies_list

    # Create movies database which contains movie objects
    movies_database = []
    for element in movie_dict.items():
        movie_obj = Movie()
        movie_obj.imdb_id = element[1]
        movie_obj.path = element[0]
        movies_database.append(movie_obj)
    del movie_dict

    # For movies which has not imdb id detected try to obtain title
    # from the file name
    # File name must be in such format: mobie_title_year(4 digits)_extension

    for movie in movies_database:
        if movie.imdb_id == None:
            obtain_title_and_year(movie)
            print "obtained! %s" % (movie.title)

    # Obtain movie details from imdb.
    unique_movies_dict = {}
    for movie in movies_database:
        # imdb id detected - take movie data from imdb.com
        if movie.imdb_id != None:
            movie.imdb_object = ImdbCom.get_movie_data_from_imdbcom(movie.imdb_id)
            movie.prepare_data_from_imdb()
            if movie.imdb_id not in unique_movies_dict.keys():
                print '"%s" processed.' % movie.title
            unique_movies_dict[movie.imdb_id] = movie
        # imdb id is not known but title has been obtained from file name
        elif movie.title != None:
            movie.imdb_object, movie.imdb_id = ImdbCom.search_movie_by_title(movie)
            if movie.imdb_object:
                movie.prepare_data_from_imdb()
                if movie.imdb_id not in unique_movies_dict.keys():
                    print '"%s" processed.' % movie.title
                unique_movies_dict[movie.imdb_id] = movie
        else:
            print '"%s" not processed.' % movie.path

    # Preapre list of not duplicated movies
    # Each movie objact on this list contains data from imdb.com
    unique_movies_list = unique_movies_dict.values()

    # Finally render index.html file

    # Prepare environment for jinja2
    execution_path = os.path.dirname(os.path.realpath(__file__))
    templates_path = os.path.join(execution_path, "templates")
    static_path = os.path.join(execution_path, "static")
    # Prepare path for copying static files to cwd
    templates_cwd_path = os.path.join(os.getcwd(), "moview/templates")
    static_cwd_path = os.path.join(os.getcwd(), "moview/static")
    # Remove moview tmp files if already exists in cwd
    try:
        shutil.rmtree(os.path.join(os.getcwd(), "moview"))
    except:
        pass
    # Copy static files to cwd under moview direcotry
    shutil.copytree(templates_path, templates_cwd_path)
    shutil.copytree(static_path, static_cwd_path)
    # Prepare environment for jinja2
    env = Environment(loader=FileSystemLoader(templates_path))
    # Select template
    template = env.get_template("index.html")
    # Render results to index.html file
    rendered_file = open("index.html", "w")
    rendered_file.write(template.render(movielist=unique_movies_list).encode("utf-8"))
    rendered_file.close()
    # That's it!

    print "index.html with movie list has been " + "generated in the current directory."
    print "Thanks for using MoView!"

    return sys.exit(0)