def omdb_get_movie(self, id): movie_id = id r = requests.get( f"http://www.omdbapi.com/?i={movie_id}&apikey={omdb_api_key}") data = r.json() imdb_id = data['imdbID'] original_title = data['Title'] title = data['Title'] release_date = datetime.strptime(data['Released'], '%d %b %Y').date() duration = None score = data['imdbRating'] rating = data['Rated'] if rating == "PG-13": rating = "-12" elif rating in ["PG", "G", "R"]: rating = "TP" synopsis = data['Plot'] #print(imdb_id) #print(title) #print(original_title) #print(release_date) #print(duration) #print(rating) #print(score) movie = Movie(title, original_title, duration, release_date, rating) movie.imdb_id = imdb_id movie.score = score movie.synopsis = synopsis return movie
def findAll(table): cnx = connectToDatabase() cursor = createCursor(cnx) cursor.execute(findAllQuery(table)) results = cursor.fetchall( ) # liste de dictionnaires contenant des valeurs scalaires closeCursor(cursor) disconnectDatabase(cnx) if (table == "movies"): movies = [] for result in results: # result: dictionnaire avec id, title, ... movie = Movie(title=result['title'], original_title=result['original_title'], release_date=result['release_date'], duration=result['duration'], rating=result['rating']) movie.imdb_id = result['imdb_id'] movie.imdb_score = result['imdb_score'] movie.boxoffice = result['box_office'] movie.id = result['id'] movies.append(movie) return movies if table == "people": people = [] for result in results: # result: dictionnaire avec id, firstname, ... person = Person( firstname=result['firstname'], lastname=result['lastname'], ) person.id = result['id'] people.append(person) return people
def omdb_get_by_id(self, id, api_key): r = requests.get(f'http://www.omdbapi.com/?i={id}&apikey={api_key}') r = r.json() if r['Response'] == "False": movie = f"Aucun film avec l'id {id} n'existe pas dans la base" return movie else: imdb_id_str = r['imdbID'] imdb_id = imdb_id_str.replace("tt", "") title = r['Title'] original_title = r['Title'] release_date_class = r['Released'] if release_date_class == 'N/A': release_date_class = None release_date = None else: release_date_strip = release_date_class.strip() release_date_object = datetime.strptime(release_date_strip, '%d %b %Y') release_date = release_date_object.strftime('%Y-%m-%d') duration = r['Runtime'] if duration == 'N/A': duration = None else: duration = duration.split() duration = duration[0] if r['Rated'] == 'R': rating = '-12' elif r['Rated'] == 'NC-17': rating = '-16' else: rating = 'TP' if r['Type']=="movie": box_office = r['BoxOffice'] if r['BoxOffice'] == 'N/A': box_office = None else: box_office = None imdb_score = r['imdbRating'] movie = Movie(title, original_title, release_date, duration, rating) movie.imdb_id = imdb_id movie.imdb_score = imdb_score movie.box_office = box_office return movie
def tmdb_get_movie(self, id): movie_id = id url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}&language=fr" #print(url) r = requests.get(url) data = r.json() #print(data) imdb_id = data.get('imdb_id', None) title = data.get('title', None) original_title = data.get('original_title', None) release_date = data['release_date'] duration = data['runtime'] synopsis = data['overview'].replace("'", "''") production_budget = data['budget'] url = f"http://www.omdbapi.com/?i={imdb_id}&apikey={omdb_api_key}" #print(url) r = requests.get(url) data_omdb = r.json() #print(data_omdb) score = data_omdb.get('imdbRating', None) rating = data_omdb.get('Rated', None) if rating == "PG-13": rating = "-12" if rating == "R": rating = "-18" elif rating in ["PG", "G"]: rating = "TP" print(imdb_id) print(title) print(original_title) print(release_date) print(duration) print(score) print(rating) print(production_budget) movie = Movie(title, original_title, duration, release_date) movie.imdb_id = imdb_id movie.score = score movie.synopsis = synopsis movie.production_budget = production_budget movie.rating = rating return movie
def get_movie(self, id): args = {'region': self.region} result, status = self.get(f'/movie/{id}', args) title = result['title'] release_date = result['release_date'] duration = result['runtime'] original_title = result['original_title'] # origin_country = result_json['production_countries'][0]['name'] movie = Movie(title, original_title, duration, release_date=release_date) # popularity = result_json['popularity'] # vote = result_json['vote_average'] # revenue = result_json['revenue'] movie.synopsis = result['overview'] movie.production_budget = result['budget'] movie.tmdb_id = result['id'] movie.imdb_id = result['imdb_id'] movie.score = result['vote_average'] # print(movie) return movie
def tmdb_get_by_id(self, id, api_key): r = requests.get( f'https://api.themoviedb.org/3/movie/{id}?api_key={api_key}') r = r.json() if 'status_code' not in r: title = r['title'] original_title = r['original_title'] release_date = r['release_date'] if r['adult'] == 'False': rating = 'TP' else: rating = '-18' duration = r['runtime'] box_office = r['revenue'] imdb_id = r['imdb_id'] imdb_id = imdb_id.replace("tt", "") imdb_score = r['vote_average'] synopsis = r['overview'] production_budget = r['budget'] movie = Movie(title, original_title, release_date, duration, rating) movie.imdb_id = imdb_id movie.imdb_score = imdb_score movie.box_office = box_office actors = r['Actors'] for actor in actors: firstname = actor[0] lastname = actor[1] app.insert_people(firstname, lastname) return movie if r['status_code'] == 34: movie = f"Aucun film avec l'id {id} n'existe dans la base" return movie
# break if row['isAdult']=="1" or row['titleType'] !="movie": #continue à la ligne suivante continue print(row) #duration = row.get('runtimeMinutes', None #valeur si runtimeMinutes n'est pas présente) duration = row['runtimeMinutes'] if duration == "\\N": duration = None movie = Movie( original_title = row['originalTitle'], duration=duration, release_date=None, rating=None ) movie.imdb_id = row['tconst'] print(f"Ajout du film #{movie.imdb_id}") movies.append(movie) insert_movies(movie) with gzip.open("name.basics.tsv.gz","rt",encoding='utf-8') as tsvfile: rows = csv.DictReader(tsvfile,delimiter="\t", quoting=csv.QUOTE_NONE) people=[] for row in rows: print(row) person = Person( imdb_id = row['nconst'], name=row['name'] ) people.append(person) insert_people(people) with gzip.open("title.crew.tsv.gz","rt",encoding='utf-8') as tsvfile:
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)