Skip to content

Instantly share code, notes, and snippets.

@wragge
Created November 25, 2014 07:19
Show Gist options
  • Save wragge/449559365f9aafb4cb04 to your computer and use it in GitHub Desktop.
Save wragge/449559365f9aafb4cb04 to your computer and use it in GitHub Desktop.
Code to produce the graph of daily newspapers in Trove at https://plot.ly/~wragge/20
import requests
import time
import plotly.plotly as py
from plotly.graph_objs import *
from credentials import TROVE_API_KEY
py.sign_in("[your plotly id]", "[your plotly password]")
titles_url = 'http://api.trove.nla.gov.au/newspaper/titles?encoding=json&key={}'
title_url = 'http://api.trove.nla.gov.au/newspaper/title/{}?encoding=json&key={}&include=years'
response = requests.get(titles_url.format(TROVE_API_KEY))
titles = response.json()
years = {}
x = []
y = []
for title in titles['response']['records']['newspaper']:
title_id = title['id']
response = requests.get(title_url.format(title_id, TROVE_API_KEY))
details = response.json()
for year in details['newspaper']['year']:
date = int(year['date'])
count = int(year['issuecount'])
if count > 300:
print 'Daily!'
try:
years[date] += 1
except KeyError:
years[date] = 1
time.sleep(.5)
for year in sorted(years):
if year < 1955:
x.append(year)
y.append(years[year])
trace1 = Scatter(
x=x,
y=y
)
layout = Layout(
xaxis=XAxis(
title='Year'
),
yaxis=YAxis(
title='Number of daily newspapers in Trove'
)
)
data = Data([trace1])
fig = Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='daily-papers')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment