# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # ML: Movie Learning
#
# We're going to do some form of movie text wrangling using Python. At this point, we have acquired a list of movie titles, along with some other data scraped from Wikipedia. Unfortunately, these other data are inconsistently formatted and will be a bit difficult to work with. Since we want to get plot synopses from the OMDb API anyway, we can get these other data of interest from here as well.
#
#
#
# ## 1. Data Collection
#
# ### 1.1. The OMDb API
#
# We're going to use the OMDb API, which is free once again (http://www.omdbapi.com/). To use it, you need to get an API key. You can get a free key, which limits to 1000 requests per day. More requests (and a poster API) are available if you patronize the OMDb Patreon. The OMDb website explains how the API works pretty well. We'll use the `requests` package to make calls to the OMDb API. 
#
# There are two ways we can get movie data using this API, either by movie title or IMDb ID. We'll want to be able to handle either, as I have a feeling that it will be easier to get a random list of IMDb ID's than a random list of movie titles? It is also more exact to use ID's since movie titles aren't unique (e.g.,"The Mummy" can either refer to the Brendan Fraser masterpiece, or the Tom Cruise dumpster fire).
#
# Note, I'm using format strings (`f'some {text}'`) which is a Python 3.6 feature (equivalent to `'some {}.format(text)'`).

# +
import os
import json

import requests

from dotenv import load_dotenv, find_dotenv
#find .env automagically by walking up directories until it's found
dotenv_path = find_dotenv()
# load up the entries as environment variables
load_dotenv(dotenv_path)

# +
API_KEY = os.environ.get('OMDB_API_KEY')

def get_movie_data(name, year=None, api_key=API_KEY, full_plot=False):
    """Returns json from OMDb API for movie."""
    api_url = f'http://private.omdbapi.com/?apikey={api_key}'
    # There are actually utilities that can automatically escape invalid characters
    # but here we do the manual dumb solution
    name = name.lower().replace(' ', '+')
    
    # Can either manually extend the url with parameters or...
    #api_url += f'&t={name}'
    # if year is not None:
    #     api_url += f'&y={year}'
    # if full_plot:
    #     api_url += '&plot=full'
    # response = requests.get(api_url)
    
    # ... have `requests` do it for you!
    body = {'t': name}
    if year is not None:
        body['y'] = year
    if full_plot:
        body['plot'] = 'full'
    response = requests.get(api_url, params=body)
    
    # Throw error if API call has an error
    if response.status_code != 200:
        raise requests.HTTPError(
            f'Couldn\'t call API. Error {response.status_code}.'
        )
     
    # Throw error if movie not found
    # if response.json()['Response'] == 'False':
    #     raise ValueError(response.json()['Error'])
    
    return response.json()


# -

# Let's test it out, and see what kind of information we get from our request.

response_json = get_movie_data('Snakes on a Plane')
print(json.dumps(response_json, indent=4))

# With year provided
response_json = get_movie_data('Snakes on a Plane', year=2006)
print(json.dumps(response_json, indent=4))

# With incorrect year provided
response_json = get_movie_data('Snakes on a Plane', year=2005)
print(json.dumps(response_json, indent=4))

# With full plot:
response_json = get_movie_data('Snakes on a Plane', full_plot=True)
print(json.dumps(response_json, indent=4))

# Film without a lot of data
# Note you can leave out the apostrophe and OMDb will still find the film
response_json = get_movie_data('Boarding School Girls\' Pajama Parade', full_plot=False)
print(json.dumps(response_json, indent=4))

# Nonsense film that doesn't exist
response_json = get_movie_data('Phantom of the cheese cake')
print(json.dumps(response_json, indent=4))

# Nonsense film that doesn't exist
response_json = get_movie_data('ar')
print(json.dumps(response_json, indent=4))

StarcoderdataJupyterScriptsDedupFiltered