-
Notifications
You must be signed in to change notification settings - Fork 1
/
movie_addicts.py
78 lines (65 loc) · 2.7 KB
/
movie_addicts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
__author__ = 'Luiz Arantes Sa'
import webbrowser
import os
import re
def create_movie_tiles_content(movies):
"""
Generates movie tiles from the movie list using
"tile-container.html" as a template.
:param movies:
:type movies: list lib.media.movie
:return: the generated movie tiles.
:rtype: str
"""
with open('templates/tile-container.html', 'r') as template:
tile_template = template.read()
content = ''
# Keep track of the current movie tile
# so we know when to insert a clearfix div
count = 1
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 contents filled in
content += tile_template.format(
title=movie.title,
cover=movie.poster_image_url,
youtube_id=trailer_youtube_id,
score=movie.rating,
genre=movie.genre,
duration=movie.duration,
movie_rating=movie.movie_rating,
description=movie.description,
year=movie.year_released
)
clearfix = ''
if count % 2 == 0: # Add small grid clearfix every 2 tiles
clearfix += ' visible-sm-block '
if count % 3 == 0: # Add medium/large grid clearfix every 3 tiles
clearfix += ' visible-md-block visible-lg-block '
if len(clearfix) > 0: # append clearfix if needed
content += '<div class="clearfix ' + clearfix + '"></div>'
count += 1
return content
def open_movies_page(movies):
"""
Creates a web page from the list of movies and opens it using
the default web browser.
Note: Opens in a new tab if possible.
:param movies: list of movies
:type movies: list lib.media.movie
:return: absolutely nothing ;)
"""
# Replace the placeholder for the movie tiles with the actual dynamically generated content
movie_tiles = create_movie_tiles_content(movies)
# Wrap the header and footer content around the movie tiles
with open('templates/header.html', 'r') as header, open('templates/footer.html', 'r') as footer:
content = header.read() + movie_tiles + footer.read()
# Create or overwrite the output file
with open('index.html', 'w') as output_file:
output_file.write(content)
# 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