1.Principle
The main idea of this python program is to build a movie trailer website which can show posters of several movies and once clicked, show the trailer automatically.
fresh tomatoes
2. Details in realizing
We have 3 python files: entertainment_center.py, media.py, fresh_tomatoes.py.
media.py
This file defines a class Movie which provides a way to store movie related information
import webbrowser
class Movie():
def __init__(self, movie_title, poster_image, trailer_youtube):
# initialize instance of class Movie
self.title = movie_title
self.poster_image_url = poster_image
self.trailer_youtube_url = trailer_youtube
def show_trailer(self):
# open a webbrowser and access in the page linked by url
webbrowser.open(self.trailer_youtube_url)
entertainment_center.py
This file is the central program we need to run.** media.py** and fresh_tomatoes.py are imported.
import media
import fresh_tomatoes
In this file at first we build 6 instances of class Movie(), which contain what we need to post on the webpage. one example making an instance:
deadpool = media.Movie('Deadpool',
'http://www.sideshowtoy.com/wp-content/uploads/2016/01/marvel-deadpool-sixth-scale-hot-toys-feature-902628.jpg', # noqa
'https://www.youtube.com/watch?v=tLmStxxzhkI') # noqa
Then build a list to store the instances and execute the open_moives_page() function from fresh_tomatoes.py
movies = [deadpool, suicide_squad, doctor_strange,
star_trek, assassin_creed, final_fantasy]
fresh_tomatoes.open_movies_page(movies)
fresh_tomatoes.py
This file contains the html codes. in the html codes {movie_tiles} {poster_image_url} {movie_title} remain to be replaced according to the instances established in entertainment_center.py. 2 functions are defined as well.
def create_movie_tiles_content(movies):
# The HTML content for this section of the page
content = ''
for movie in movies:
# Extract the youtube ID from the url
youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer_youtube_url)
youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer_youtube_url)
trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None
# Append the tile for the movie with its content filled in
content += movie_tile_content.format(
movie_title=movie.title,
poster_image_url=movie.poster_image_url,
trailer_youtube_id=trailer_youtube_id
)
return content
This function will return reformed html codes which contain the movie contents.
def open_movies_page(movies):
# Create or overwrite the output file
output_file = open('fresh_tomatoes.html', 'w')
# Replace the placeholder for the movie tiles with the actual dynamically generated content
rendered_content = main_page_content.format(movie_tiles=create_movie_tiles_content(movies))
# Output the file
output_file.write(main_page_head + rendered_content)
output_file.close()
# open the output file in the browser
url = os.path.abspath(output_file.name)
webbrowser.open('file://' + url, new=2) # open in a new tab, if possible
This file will build a html file, write in the well formated html codes and open the webbrowser, show the page.