{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NAME = \"\"\n", "COLLABORATORS = \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# TMDb API Exercises" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "d83a92dd7af044227cb96171749eeecc", "grade": false, "grade_id": "cell-b9abcf27cf7faf8f", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "import requests\n", "\n", "import os\n", "import os.path\n", "import sys\n", "import importlib\n", "\n", "if os.path.isdir(os.path.join(\"../../..\", \"modules\")):\n", " module_dir = os.path.join(\"../../..\", \"modules\")\n", "else:\n", " module_dir = os.path.join(\"../..\", \"modules\")\n", "\n", "module_path = os.path.abspath(module_dir)\n", "if not module_path in sys.path:\n", " sys.path.append(module_path)\n", "\n", "import util\n", "importlib.reload(util)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Helpful Links\n", "\n", "- TMDB Login Page: https://themoviedb.org/login\n", "- API Settings: https://www.themoviedb.org/settings/api\n", "- API Documentation Top Level: https://www.themoviedb.org/documentation/api\n", "- API Endpoints: https://developers.themoviedb.org/3/getting-started/introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set Up\n", "\n", "**Edit `creds.json` Before Starting this Homework**\n", "\n", "#### Record Credentials Information\n", "\n", "1. In the same folder as this notebook, right-click on `creds.json` and `Open With` and select `Editor`.\n", "2. In the `tmdb` dictionary entry, put your API key, obtained for your application from TMDb, as the value of the `apikey` key.\n", "3. **Save the File**\n", "4. Run the cell below, which calls a function in the `util` module that simply reads in a paricular sub-dictionary within the creds file, and then assigns a global variable `apikey`. The signature for this function is:\n", "\n", " `read_creds(key, folder=\".\", file=\"creds.json\")`\n", " \n", " where the second and third parameter give the folder and name for the credentials file. Since we are using a `creds.json` in the current directory, we only need to specify the key for the sub-dictionary we wish to read." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tmdb_creds = util.read_creds(\"tmdb\", \".\", \"creds.json\")\n", "print(tmdb_creds)\n", "\n", "#apikey = \"\"\n", "apikey = tmdb_creds['apikey']\n", "print(apikey)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "24a5702d711e05a2f09c99568ec4a071", "grade": false, "grade_id": "cell-aa43a565afed02aa", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "**Q1** Write a function \n", "\n", " getGenreDict(my_api_key)\n", " \n", "that returns a Python dictionary mapping **Movie** genre names to id numbers, e.g. `{'Action':28, 'Adventure':12,...}`. Your function should only make a single call to `requests.get()`, with the appropriate endpoint path for TMDB, and with the given API key. As always, return `None` if something goes wrong. Note that this is not asking for the entire JSON result, but a dictionary mapping genre names to id numbers.\n", "\n", "You may want to write this as a global cell to get it working and to examine the results, and then convert it into a function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "3a302faa221f8eac690b790710653f55", "grade": false, "grade_id": "cell-f13033eadf64207e", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a30f9c97be76f1213b3bb2627c224b3f", "grade": true, "grade_id": "cell-c6c9372623d6829a", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "d = getGenreDict(apikey)\n", "print(d)\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "3e925e641c5c01c662984737a0104852", "grade": false, "grade_id": "cell-b3aec791f18dc972", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "**Q2** Write a function \n", "\n", " search_person_id(my_api_key, name)\n", " \n", "that uses the `/search/person` endpoint to conduct a search for the given `name`, and returns the `id` of the first entry in the `results` list. This should use only one call to `requests.get()`. As always, return `None` if something goes wrong, and return `-1` if there were no results." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "61f40b96d3d5aa50b6f1e0a400831fbe", "grade": false, "grade_id": "cell-d53a29800554dd8c", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "6be059b808f13203b42043ad6c9d65ec", "grade": true, "grade_id": "cell-5cc129c05c292f70", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "assert search_person_id(apikey,'tom cruise') == 500\n", "assert search_person_id(apikey+'z','Bill Murray') == None\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "7f7aa002089ef743555a0cda5b8edc26", "grade": false, "grade_id": "cell-04d00a6955eee627", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "**Q3** The reading mentioned the TMDB endpoint `/discover/movie`. Many examples of what can be done with this endpoint are provided at the following link. \n", "\n", "- https://www.themoviedb.org/documentation/api/discover\n", "\n", "However, as always with APIs, it is unwise to rely solely on examples. Rather, you must read and understand the API documentation. Navigate the TMDB API documentation and find the url that explains how to use the endpoint `/discover/movie` (the place with the \"Try it out\" tab). Store this url as a string called `documentation` below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "de03d79dcba05ffdde8d0dc4232b95d4", "grade": false, "grade_id": "cell-dad96c8c9a596585", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "documentation = \"\" # modify this\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "8e6294edbbec78df13331e890dde460e", "grade": true, "grade_id": "cell-08eb4b5e826e0678", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Discovering Movies and/or TV Shows\n", "\n", "The Discover Movie endpoint has many possibilities, as seen by the long list of various examples pointed to in the last question, and there is also a comparable discover endpoint for TV shows. The remainder of the questions in this notebook will be primarily self-determined and focus on versions of the discover endpoints that are interesting to the individual student." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4** Write Python code to issue at least four distinct requests to either the movie or the TV discover endpoints, and then examine the results. As you look at the results, think about how you would process results to build a tabular/`pandas` dataframe and the columns you would use in such a representation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "619afc9a7204afadfcf3278f59152078", "grade": true, "grade_id": "cell-f619b8473dc14015", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Discover Example 1\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "bf58192de467362b1e80e7efaaf9fe05", "grade": true, "grade_id": "cell-4b1c05b8a2bc5620", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Discover Example 2\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "fca40ed58700f64718f681e10368c6f9", "grade": true, "grade_id": "cell-cce240c1aaabcb41", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Discover Example 3\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1eeb6edc675b6193e38ce2c3365fb2b9", "grade": true, "grade_id": "cell-6c12aef42c132f17", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Discover Example 4\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "2194e94eec32ad267dc357c4dc1858b1", "grade": false, "grade_id": "cell-6d7dae5eb94cfe81", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "The following suggests a culminating exercise that focuses on movies and the use of **two** attributes to use for filtering in a call to the discover endpoint. If you wish, you can change to focus on TV shows, and can use **different** attributes, depending on what you might find personally interesting. The point is the use of multiple attributes and the building of a tabular (LoD) representation. \n", "\n", "**Q5** Write a function \n", "\n", " makeBigMovieLoD(my_api_key, actorList, genreList)\n", " \n", "that returns a LoD with one dictionary per movie, with columns `title` (that's 'original title'), `id`, `popularity`, `language`, and `overview`. You will have to use your earlier functions since the `actorList` will be a list of actual actor names, and `genreList` will also be symbolic, and not numeric.\n", "\n", "Your function should then use a single call to `requests.get()` and should include movies with **all** of the actors on `actorList` and with **any** of the genres on `genreList`. If you come across a non-existant actor or genre (e.g., \"abventure\"), don't include it in the search. Please sort your results by popularity, from most popular to least popular." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "0366835afbb3d1a453cd55aafb877e63", "grade": true, "grade_id": "cell-a8c294e2a1de8260", "locked": false, "points": 3, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }