{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exploring facets\n", "\n", "
New to Jupyter notebooks? Try Using Jupyter notebooks for a quick introduction.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Facets aggregate collection data in interesting and useful ways, allowing us to build pictures of the collection. This notebook shows you how to get facet data from Trove." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import altair as alt\n", "import pandas as pd\n", "import requests\n", "\n", "# Make sure data directory exists\n", "os.makedirs(\"data\", exist_ok=True)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "# Load variables from the .env file if it exists\n", "# Use %%capture to suppress messages\n", "%load_ext dotenv\n", "%dotenv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Insert your API key between the quotes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Your API key is: gq29l1g1h75pimh4\n" ] } ], "source": [ "# This creates a variable called 'api_key', paste your key between the quotes\n", "API_KEY = \"\"\n", "\n", "# Use an api key value from environment variables if it is available (useful for testing)\n", "if os.getenv(\"TROVE_API_KEY\"):\n", " API_KEY = os.getenv(\"TROVE_API_KEY\")\n", "\n", "# This displays a message with your key\n", "print(\"Your API key is: {}\".format(API_KEY))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "api_search_url = \"https://api.trove.nla.gov.au/v3/result\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set up our query parameters. We want everything, so we set the `q` parameter to be a single space." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "params = {\n", " \"q\": \" \", # A space to search for everything\n", " \"facet\": \"format\",\n", " \"category\": \"book\",\n", " \"encoding\": \"json\",\n", " \"n\": 1,\n", "}\n", "\n", "headers = {\"X-API-KEY\": API_KEY}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "response = requests.get(api_search_url, params=params)\n", "data = response.json()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | facet | \n", "total | \n", "
---|---|---|
21 | \n", "Archived website | \n", "33660 | \n", "
4 | \n", "Article | \n", "7377170 | \n", "
5 | \n", "Article/Abstract | \n", "99 | \n", "
6 | \n", "Article/Book chapter | \n", "67276 | \n", "
7 | \n", "Article/Conference paper | \n", "112605 | \n", "
8 | \n", "Article/Journal or magazine article | \n", "1971332 | \n", "
9 | \n", "Article/Other article | \n", "4770227 | \n", "
10 | \n", "Article/Report | \n", "466581 | \n", "
11 | \n", "Article/Review | \n", "285937 | \n", "
12 | \n", "Article/Working paper | \n", "73468 | \n", "
18 | \n", "Audio book | \n", "321559 | \n", "
0 | \n", "Book | \n", "17061706 | \n", "
1 | \n", "Book/Braille | \n", "36613 | \n", "
2 | \n", "Book/Illustrated | \n", "7922202 | \n", "
3 | \n", "Book/Large print | \n", "119801 | \n", "
17 | \n", "Conference Proceedings | \n", "483440 | \n", "
22 | \n", "Data set | \n", "27 | \n", "
19 | \n", "Government publication | \n", "226184 | \n", "
16 | \n", "Microform | \n", "946703 | \n", "
13 | \n", "Periodical | \n", "2113846 | \n", "
14 | \n", "Periodical/Journal, magazine, other | \n", "2028483 | \n", "
15 | \n", "Periodical/Newspaper | \n", "87122 | \n", "
20 | \n", "Thesis | \n", "38121 | \n", "