You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
7.4 KiB
243 lines
7.4 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### \n",
|
|
"\n",
|
|
"# Unit 5 Lab: Finalizing the Movie App\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"Welcome to the Unit 5 lab!\n",
|
|
"\n",
|
|
"This is the lab we've been working toward. Here, you'll add functionality to read an API key from a local file, as well as structure the GET request to the remote server.\n",
|
|
"\n",
|
|
"You may not have internet access to this remote server - that's okay! We'll mock up the foundation based on our class lessons and you can check your work against the solution for correctness.\n",
|
|
"\n",
|
|
"Buckle up — here we go!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Read API Key From File\n",
|
|
"\n",
|
|
"Write a function, `load_api_key` that, when called:\n",
|
|
"\n",
|
|
"- Reads in an api key from a json file, `key.json`, in the notebook's current directory\n",
|
|
" - The key of the json element will be `api_key` and may look something like this:\n",
|
|
" ```javascript\n",
|
|
" {\n",
|
|
" 'api_key': '12345ABCDE'\n",
|
|
" }\n",
|
|
" ```\n",
|
|
"- Returns the api key as a string"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def load_api_key():\n",
|
|
" import json\n",
|
|
" import sys\n",
|
|
" try:\n",
|
|
" with open('./key.json') as f:\n",
|
|
" d = json.load(f)\n",
|
|
" try:\n",
|
|
" return d['api_key']\n",
|
|
" except KeyError as e:\n",
|
|
" print(f'Expected api_key, found {e}')\n",
|
|
" sys.exit(1)\n",
|
|
" except FileNotFoundError as e:\n",
|
|
" print(e)\n",
|
|
" sys.exit(1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'12345ABCDE'"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"load_api_key()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Media Class\n",
|
|
"\n",
|
|
"For this exercise, this class will remain untouched."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class Media(object):\n",
|
|
" \"\"\"Base class for Movie and Book objects\"\"\"\n",
|
|
"\n",
|
|
" def __init__(self, publisher, market):\n",
|
|
" self.publisher = publisher\n",
|
|
" self.market = market\n",
|
|
"\n",
|
|
" def get_media_info(self):\n",
|
|
" \"\"\"Prints media info to stdout\"\"\"\n",
|
|
" print(f'The Media object\\'s publisher is {self.publisher} \\\n",
|
|
" and the market is {market}.')\n",
|
|
" return None"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Movie Class\n",
|
|
"\n",
|
|
"Augment the functionality of the `Movie` class as follows:\n",
|
|
"\n",
|
|
"- Make a GET Request\n",
|
|
" - Add a method, `call_api` which calls a ficticious endpoint URL, `https://www.rottentomatoes.com/api/v1`\n",
|
|
" - Include the [param payload](http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls) of 'apiKey' with the key you loaded in with `load_api_key()`\n",
|
|
" - Check the return response and handle errors accordingly\n",
|
|
" - In the event of a successful call, return the parsed json object (assume a mimetype of UTF-8 application/json)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"import requests\n",
|
|
"\n",
|
|
"class Movie(Media):\n",
|
|
" \"\"\"\n",
|
|
" Movie objects contain all information about a particular movie,\n",
|
|
" including the title and rating.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" def __init__(self, movie_data, publisher, market):\n",
|
|
" \"\"\"\n",
|
|
" Initialize Movie class, note that the init of the superclass,\n",
|
|
" Media, is called with the super().__init__() call\n",
|
|
" \"\"\"\n",
|
|
" # Call the constructor (init) method of Media\n",
|
|
" super().__init__(publisher, market)\n",
|
|
" # Store the raw data in this object so that we can use the\n",
|
|
" # data in the getter functions.\n",
|
|
" self.movie_data = movie_data\n",
|
|
"\n",
|
|
" def get_movie_title(self):\n",
|
|
" \"\"\"\n",
|
|
" get_movie_title is a getter function that returns the movie title.\n",
|
|
" \"\"\"\n",
|
|
" # Return the title from the movie data.\n",
|
|
" try:\n",
|
|
" return self.movie_data[\"title\"]\n",
|
|
" except KeyError as e:\n",
|
|
" print(f'Key {e.args[0]} was not found in dictionary.')\n",
|
|
" sys.exit(1)\n",
|
|
"\n",
|
|
" def get_movie_rating(self):\n",
|
|
" \"\"\"\n",
|
|
" get_movie_rating is a getter function that returns the rating.\n",
|
|
" \"\"\"\n",
|
|
" # Return the rating from the movie data.\n",
|
|
" try:\n",
|
|
" return self.movie_data[\"rating\"]\n",
|
|
" except KeyError as e:\n",
|
|
" print(f'Key {e.args[0]} was not found in dictionary.')\n",
|
|
" sys.exit(1)\n",
|
|
" \n",
|
|
" def call_api(self):\n",
|
|
" \"\"\"\n",
|
|
" Calls ficticious API with api key as parameter payload\n",
|
|
" \"\"\"\n",
|
|
" res = requests.get(\n",
|
|
" 'https://www.rottentomatoes.com/api/v1',\n",
|
|
" params={'apiKey': load_api_key()}\n",
|
|
" )\n",
|
|
" if res.status_code != requests.codes.ok:\n",
|
|
" print(f'Error with status code {res.status_code}')\n",
|
|
" sys.exit(1)\n",
|
|
" else:\n",
|
|
" return res.json()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Functions\n",
|
|
"\n",
|
|
"Functions below will remain untouched for this exercise"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def return_single_movie_object(movie_title, movie_rating, publisher, market):\n",
|
|
" \"\"\"\n",
|
|
" Take in the movie title and rating, and return the movie object.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" return Movie({'title': movie_title, 'rating': movie_rating}, publisher, market)\n",
|
|
"\n",
|
|
"def print_all_ratings(movie_list):\n",
|
|
" \"\"\"\n",
|
|
" Take in a list of movies, create a movie object for each, and print the rating.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" for movie in movie_list:\n",
|
|
" movie_object = return_single_movie_object(movie, 4, \"Universal Studios\", \"USA\")\n",
|
|
" print(\"The movie\", movie_object.get_movie_title(), \"has a rating of\", movie_object.get_movie_rating())"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.7.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|