import requests
import pandas as pd
import altair as alt
from IPython.display import display, HTML
api_key = 'YOUR API KEY'
print('Your API key is: {}'.format(api_key))
# Base url for queries
api_search_url = 'http://api.digitalnz.org/v3/records.json'
# Set up the query params (we'll change these later)
# Let's start with an empty text query to look at everything
params = {
'api_key': api_key,
'text': ''
}
params['and[display_collection][]'] = 'Papers Past'
params['text'] = 'possum OR opossum'
params['facets'] = 'year,collection'
params['facets_per_page'] = 100
response = requests.get(api_search_url, params=params)
data = response.json()
titles = data['search']['facets']['collection']
titles_df = pd.Series(titles).to_frame().reset_index()
titles_df.columns = ['title', 'count']
titles_df.head()
years = data['search']['facets']['year']
years_df = pd.Series(years).to_frame().reset_index()
years_df.columns = ['year', 'count']
years_df['url'] = 'https://paperspast.natlib.govt.nz/newspapers?query={0}&start_date=01-01-{1}&end_date=31-12-{1}'.format(params['text'], years_df['year'][0])
years_df.head()
c1 = alt.Chart(years_df, width=600).mark_line(point=True).encode(
x = 'year(year):T',
y = 'count:Q',
tooltip = [alt.Tooltip('year(year):T', title='year'), alt.Tooltip('count', format=',')],
href='url:N'
).properties(
height=300,
width=500
)
c2 = alt.Chart(titles_df[1:11]).mark_bar().encode(
x = 'count:Q',
y = 'title:O',
tooltip = alt.Tooltip('count', format=',')
).properties(
height=300,
width=200
)
c1 | c2