import os
import tmdbsimple as tmdb
import media
import fresh_tomatoes as ft

movies = []

if os.environ.get('TMDB_API', False):
    # Retrieve API KEY
    tmdb.API_KEY = os.environ['TMDB_API']

    # TMDB Movie Ids
    movie_ids = [271110, 297761, 246655, 278154, 135397, 188927]

    # Get Configuration
    configuration = tmdb.Configuration().info()
    image_base_url = configuration['images']['secure_base_url']
    image_width = "w500"

    for movie_id in movie_ids:
        m = tmdb.Movies(movie_id)

        # Retrieve Image URL
        minfo = m.info()
        poster_image_url = image_base_url + image_width + minfo['poster_path']

        # Retrieve Youtube Video URL
        videos = m.videos()
        video = videos['results'][0]
        youtube_url = 'https://youtube.com/watch?v=' + video['key']

        # Append Movie object
        movie = media.Movie(m.title)
        movie.storyline = m.overview
        movie.poster_url = poster_image_url
        movie.trailer_url = youtube_url
        movies.append(movie)

else:

    # Avatar
    avatar = media.Movie("Avatar")
    avatar.storyline = ("A paraplegic marine dispatched to the moon Pandora "
                        "on a unique mission becomes torn between following "
                        "his orders and protecting the world he feels is "
                        "his home.")
    avatar.poster_url = ("https://upload.wikimedia.org/wikipedia/"
                         "en/b/b0/Avatar-Teaser-Poster.jpg")
    avatar.trailer_url = "https://www.youtube.com/watch?v=-9ceBgWV8io"

    # Deadpool
    deadpool = media.Movie("Deadpool")
    deadpool.storyline = ("A fast-talking mercenary with a morbid sense of "
                          "humor is subjected to a rogue experiment that "
                          "leaves him with accelerated healing powers and a "
                          "quest for revenge.")
    deadpool.poster_url = ("https://upload.wikimedia.org/wikipedia/en/4/46/"
                           "Deadpool_poster.jpg")
    deadpool.trailer_url = "https://www.youtube.com/watch?v=gtTfd6tISfw"

    # Ghostbusters
    ghostbusters = media.Movie("Ghostbusters")
    ghostbusters.storyline = ("Following a ghost invasion of Manhattan, "
                              "paranormal enthusiasts Erin Gilbert and Abby "
                              "Yates, nuclear engineer Jillian Holtzmann, "
                              "and subway worker Patty Tolan band together "
                              "to stop the otherworldly threat.")
    ghostbusters.poster_url = ("https://upload.wikimedia.org/wikipedia/"
                               "en/3/32/Ghostbusters_2016_film_poster.png")
    ghostbusters.trailer_url = "https://www.youtube.com/watch?v=w3ugHP-yZXw"

    # Olympus
    olympus = media.Movie("Olympus Has Fallen")
    olympus.storyline = ("Disgraced Secret Service agent (and former "
                         "presidential guard) Mike Banning finds himself "
                         "trapped inside the White House in the wake of a "
                         "terrorist attack; using his inside knowledge, "
                         "Banning works with national security to rescue "
                         "the President from his kidnappers.")
    olympus.poster_url = ("https://upload.wikimedia.org/wikipedia/en/b/bf/"
                          "Olympus_Has_Fallen_poster.jpg")
    olympus.trailer_url = "https://www.youtube.com/watch?v=vwx1f0kyNwI"

    # Angry Birds
    angry_birds = media.Movie("The Angry Birds Movie")
    angry_birds.storyline = ("Find out why the birds are so angry. When an "
                             "island populated by happy, flightless birds "
                             "is visited by mysterious green piggies, it's "
                             "up to three unlikely outcasts - Red, Chuck "
                             "and Bomb - to figure out what the pigs are up "
                             "to.")
    angry_birds.poster_url = ("https://upload.wikimedia.org/wikipedia/en/f/"
                              "f9/The_Angry_Birds_Movie_poster.png")
    angry_birds.trailer_url = "https://www.youtube.com/watch?v=1U2DKKqxHgE"

    # Ironman
    ironman = media.Movie("Iron Man")
    ironman.storyline = ("After being held captive in an Afghan cave, "
                         "billionaire engineer Tony Stark creates a unique "
                         "weaponized suit of armor to fight evil.")
    ironman.poster_url = ("https://upload.wikimedia.org/wikipedia/en/7/70/"
                          "Ironmanposter.JPG")
    ironman.trailer_url = "https://www.youtube.com/watch?v=8hYlB38asDY"

    movies = [avatar, deadpool, ghostbusters, olympus, angry_birds, ironman]

ft.open_movies_page(movies)

[((8, 3, 8, 36), 'os.environ.get', 'os.environ.get', ({(8, 18, 8, 28): '"""TMDB_API"""', (8, 30, 8, 35): '(False)'}, {}), "('TMDB_API', False)", False, 'import os\n'), ((107, 0, 107, 27), 'fresh_tomatoes.open_movies_page', 'ft.open_movies_page', ({(107, 20, 107, 26): 'movies'}, {}), '(movies)', True, 'import fresh_tomatoes as ft\n'), ((42, 13, 42, 34), 'media.Movie', 'media.Movie', ({(42, 25, 42, 33): '"""Avatar"""'}, {}), "('Avatar')", False, 'import media\n'), ((52, 15, 52, 38), 'media.Movie', 'media.Movie', ({(52, 27, 52, 37): '"""Deadpool"""'}, {}), "('Deadpool')", False, 'import media\n'), ((62, 19, 62, 46), 'media.Movie', 'media.Movie', ({(62, 31, 62, 45): '"""Ghostbusters"""'}, {}), "('Ghostbusters')", False, 'import media\n'), ((73, 14, 73, 47), 'media.Movie', 'media.Movie', ({(73, 26, 73, 46): '"""Olympus Has Fallen"""'}, {}), "('Olympus Has Fallen')", False, 'import media\n'), ((85, 18, 85, 54), 'media.Movie', 'media.Movie', ({(85, 30, 85, 53): '"""The Angry Birds Movie"""'}, {}), "('The Angry Birds Movie')", False, 'import media\n'), ((97, 14, 97, 37), 'media.Movie', 'media.Movie', ({(97, 26, 97, 36): '"""Iron Man"""'}, {}), "('Iron Man')", False, 'import media\n'), ((21, 12, 21, 33), 'tmdbsimple.Movies', 'tmdb.Movies', ({(21, 24, 21, 32): 'movie_id'}, {}), '(movie_id)', True, 'import tmdbsimple as tmdb\n'), ((33, 16, 33, 36), 'media.Movie', 'media.Movie', ({(33, 28, 33, 35): 'm.title'}, {}), '(m.title)', False, 'import media\n'), ((16, 20, 16, 40), 'tmdbsimple.Configuration', 'tmdb.Configuration', ({}, {}), '()', True, 'import tmdbsimple as tmdb\n')]