Programming the Web with APIs – Tim Ribaric and John Fink – Why are APIs important?



Programming the Web with APIs – Tim Ribaric and John Fink – Why are APIs important?

0 0


OLA2014-API

Slides for OLA 2014 API talk

On Github jbfink / OLA2014-API

Programming the Web with APIs

Tim Ribaric and John Fink

GO AWAY

Who are we?

Who are you?

Why are APIs important?

Why APIs matter to the web

  • They are the lingua franca of the Internet
  • Ensure that communication can happen between different sites and different versions
  • Provide a consistent reliable way for information to be delivered
  • They abstract some of the complications of the underlying technologies

Why in the Library?

  • Provides an unencumbered interchange of data
  • Perfect example, a union catalogue based on MARC
  • ILL just wouldn't be possible without an underlying API
  • Copy Cataloguing would be difficult as well

Where it could have helped

  • Imagine if every database vendor provided an API?
  • You could make your own Google Scholar
  • Google Scholar exists because the web is more or less recognizable as an API
  • Without ubiquity of web we'd just have: AOL Online, Prodigy, Compuserve, etc
  • Your Gmail message wouldn't get to your Yahoo Grandma

Demonstration of API

Application Programming Interface

Person as program, computer as "API". Let's take a search, for example. This is just a fancy way of saying "how to make one thing talk to another thing.

You as a client, browser as API

  • "Hey, I wonder who directed Jason X?"
  • "Let's go to IMDB and find out."
  • "here's my browser..." tappity tappity type...
This aside has a swear word in it. "Fuck", I say.

This is good and this is bad

  • This is great for *us*...
  • ...but not so great for *machines*
  • Machines are dumb, so we make it simple. They want...
{
  "Title": "Jason X",
  "Year": "2001",
  "Rated": "R",
  "Released": "26 Apr 2002",
  "Runtime": "91 min",
  "Genre": "Horror, Sci-Fi, Thriller",
  "Director": "James Isaac",
  "Writer": "Victor Miller (characters), Todd Farmer",
  "Actors": "Kane Hodder, Jeff Geddis, Lexa Doig, David Cronenberg",
  "Plot": "Jason Voorhees returns with a new look, a new machete, and his same murderous attitude as he is awakened on a spaceship in the 25th century.",
  "Language": "English",
  "Country": "USA",
  "Awards": "1 nomination.",
  "Poster": "http://ia.media-imdb.com/images/M/MV5BMTQ3NjA5NjcyNl5BMl5BanBnXkFtZTYwMzg5NjQ5._V1_SX300.jpg",
  "Metascore": "25",
  "imdbRating": "4.3",
  "imdbVotes": "31,145",
  "imdbID": "tt0211443",
  "Type": "movie",
  "Response": "True"
}

If we get data like this we can pull out information in a *programmatic* way...

curl --silent "http://www.omdbapi.com/?i=&t=Jason+X" | jq '.Director'
"James Isaac"

It's alive!

An API from Library Land.

Digital Public Library of America

Request an API key, then you are good to go

curl -v -XPOST http://api.dp.la/v2/api_key/YOUR_EMAIL@example.com

Oodles of ways to interact

http://api.dp.la/v2/items?q=Frankenstein&api_key=4c39detc

keyword search for Frankenstein

Oodles of ways to interact

http://api.dp.la/v2/items?sourceResource.temporal.end=1923-01-01&api_key=4c39detc

items from before 1923

{"media":
{"#text":"http://npgportraits.si.edu/eMuseumNPG/media/CSC/9100132c.jpg",
 "@idsId":"http://npgportraits.si.edu/eMuseumNPG/media/CSC/9100132c.jpg",
 "@thumbnail":"http://npgportraits.si.edu/eMuseumNPG/media/CSC/9100132c.jpg",
 "@type":"Images"},
 "@mediaCount":"1"}

URL of the actual image file

{
"subject":
[{"name":"Motion pictures"},
{"name":"Boris Karloff"},
{"name":"Performing Arts"},
{"name":"Performer"},
{"name":"Actor"},
{"name":"Theater"},
{"name":"Actors and actresses"},
{"name":"Entertainers"},
{"name":"Portraits"},
{"name":"Freulich, Roman"},
{"name":"Karloff, Boris"},
{"name":"Portrait"},
{"name":"Movie"}]

Subjects tagged in the record

Time to make Apps!

API to the rescue

One API, three uses

  • When are you open until?
  • ...On Halloween
  • (I'm trying my best here people)
  • Needed some easy way to display what hours we were open
  • Wanted to make it easy to re-use this information
  • Made an in-house API that spits out hours in JSON

Can open this in any browser

http://library.brocku.ca/hours/?format=json&date=2013-10-31&location=rhd
{
	"location_name":"Help Desk",
	"location_key":"rhd",
	"schedule_name":"Fall Term",
	"start_date":"2013-09-03",
	"end_date":"2014-01-05",
	"sun_open":"12:00:00",
	"sun_close":"17:00:00",
	"mon_open":"08:30:00",
	"mon_close":"19:00:00",
	"tue_open":"08:30:00",
	"tue_close":"19:00:00",
	"wed_open":"08:30:00",
	"wed_close":"19:00:00",
	"thu_open":"08:30:00",
	"thu_close":"19:00:00",
	"fri_open":"08:30:00",
	"fri_close":"17:00:00",
	"sat_open":"12:00:00",
	"sat_close":"17:00:00",
	"id":"28"
}

What happens when there is no API?

Screen scraping happens

Screen Scraping

  • True nightmare on Elm Street
  • Systematically extracting text from a webpage
  • A very fickle business at best
  • Tools exist that help with the process

Beautiful Soup

  • Software package that downloads a webpage
  • It then organizes the HTML by tag
  • You can then traverse the data, sorta

Legality of Scraping?

Example of scraping

Finding the length of a movie using IMDB

Ah there it is!

Ctrl + U to view source, then try to search for it

Not fun

Followup for the curious

Questions/Discussion

Tim Ribaric

@elibtronic

http://elibtronic.ca

tribaric@brocku.ca

John Fink

@adr

http://github.com/jbfink

john.fink@gmail.com