{"captions":[{"content":"[Seminar] [A Programmer's Introduction to APIs]","startTime":0,"duration":2480,"startOfParagraph":false},{"content":"[Billy Janitsch] [Harvard University] [Tommy MacWilliam]","startTime":2480,"duration":1579,"startOfParagraph":false},{"content":"[This is CS50.] [CS50.TV]","startTime":4059,"duration":4161,"startOfParagraph":false},{"content":"Hi, everyone, I'm Billy, and today I'm going to be talking about APIs,","startTime":8220,"duration":3880,"startOfParagraph":true},{"content":"or application programming interfaces,","startTime":12100,"duration":3120,"startOfParagraph":false},{"content":"specifically in the context of CS50 final projects and that sort of thing.","startTime":15220,"duration":4820,"startOfParagraph":false},{"content":"In general what is an API?","startTime":20040,"duration":2450,"startOfParagraph":false},{"content":"In very broad terms, it's sort of a middle man that allows 2 pieces ","startTime":22490,"duration":3040,"startOfParagraph":false},{"content":"of software to communicate with each other.","startTime":25530,"duration":3080,"startOfParagraph":false},{"content":"That's a kind of very broad definition and isn't that relevant for what we're looking at.","startTime":28610,"duration":3920,"startOfParagraph":false},{"content":"What we really want is some sort of useful middle ground","startTime":32530,"duration":2920,"startOfParagraph":false},{"content":"to communicate with some sort of database somewhere.","startTime":35450,"duration":5120,"startOfParagraph":false},{"content":"Here's a chart, and basically the idea is that","startTime":40570,"duration":2740,"startOfParagraph":true},{"content":"we are an application, and we want to get data from a database,","startTime":43310,"duration":3480,"startOfParagraph":false},{"content":"but we don't want to query the database directly.","startTime":46790,"duration":2780,"startOfParagraph":false},{"content":"Instead we want to go through this sort of middle man, the API.","startTime":49570,"duration":3140,"startOfParagraph":false},{"content":"The idea behind that is numbers 2 and 3 on the chart","startTime":52710,"duration":2730,"startOfParagraph":false},{"content":"are both going to be very complicated and messy.","startTime":55440,"duration":2310,"startOfParagraph":false},{"content":"In other words, when the API is querying the database,","startTime":57750,"duration":2210,"startOfParagraph":false},{"content":"it's probably going to be using SQL tables and all of that sort of stuff,","startTime":59960,"duration":3340,"startOfParagraph":false},{"content":"and we've learned a bit about it in CS50, but overall,","startTime":63300,"duration":2189,"startOfParagraph":false},{"content":"you've noticed that it's a bit of a pain.","startTime":65489,"duration":2101,"startOfParagraph":false},{"content":"It gets very, very complicated and messy, especially when you're making","startTime":67590,"duration":3000,"startOfParagraph":false},{"content":"complex queries and that sort of thing.","startTime":70590,"duration":1940,"startOfParagraph":false},{"content":"What we really want is some sort of useful and simple way","startTime":72530,"duration":3430,"startOfParagraph":true},{"content":"to get that data, and that's the idea behind numbers 1 and 4 on the chart.","startTime":75960,"duration":3820,"startOfParagraph":false},{"content":"In other words, we want a really simple way to tell the API what to get for us","startTime":79780,"duration":3820,"startOfParagraph":false},{"content":"and a really simple way to get that data back.","startTime":83600,"duration":4160,"startOfParagraph":false},{"content":"There is one main way that that data is usually sent and received,","startTime":87760,"duration":5260,"startOfParagraph":false},{"content":"which is JSON, or JavaScript Object Notation.","startTime":93020,"duration":3470,"startOfParagraph":false},{"content":"That can vary a little bit as far as how you send the request to the API.","startTime":96490,"duration":3880,"startOfParagraph":false},{"content":"In other words, if you want some certain amount of data,","startTime":100370,"duration":2840,"startOfParagraph":false},{"content":"how you tell the API to get that data can vary a little bit.","startTime":103210,"duration":3460,"startOfParagraph":false},{"content":"Usually it involves making some sort of network request.","startTime":106670,"duration":2540,"startOfParagraph":false},{"content":"In other words, accessing some sort of URL that's going to tell the API","startTime":109210,"duration":3920,"startOfParagraph":false},{"content":"exactly what you want, but the data is almost always sent back,","startTime":113130,"duration":3060,"startOfParagraph":false},{"content":"in other words, number 4 in JSON.","startTime":116190,"duration":3340,"startOfParagraph":false},{"content":"What is JSON exactly?","startTime":119530,"duration":1500,"startOfParagraph":true},{"content":"As I said, JavaScript Object Notation.","startTime":121030,"duration":2000,"startOfParagraph":false},{"content":"It's basically the universal standard for transmitting and receiving data.","startTime":123030,"duration":4060,"startOfParagraph":false},{"content":"The idea is that you have these 3 categories of things.","startTime":127090,"duration":4320,"startOfParagraph":false},{"content":"You have arrays, hashmaps, and primitives.","startTime":131410,"duration":2130,"startOfParagraph":false},{"content":"Arrays and hashmaps you've looked at a little bit in CS50,","startTime":133540,"duration":3040,"startOfParagraph":false},{"content":"but you've sort of gotten a very strict sense of what they are.","startTime":136580,"duration":3290,"startOfParagraph":false},{"content":"In other words, with arrays you know that they're type bound,","startTime":139870,"duration":2910,"startOfParagraph":false},{"content":"so you only have one sort of type that goes throughout the entire array.","startTime":142780,"duration":3780,"startOfParagraph":false},{"content":"JSON is a lot more lenient with that sort of thing.","startTime":146560,"duration":2750,"startOfParagraph":false},{"content":"Basically the idea is you construct this object,","startTime":149310,"duration":4280,"startOfParagraph":false},{"content":"which can be composed of any of these 3 things","startTime":153590,"duration":2680,"startOfParagraph":false},{"content":"and can be composed of multiple ones of them, and they can be nested.","startTime":156270,"duration":3200,"startOfParagraph":false},{"content":"Here's sort of an example of JSON,","startTime":159470,"duration":2640,"startOfParagraph":true},{"content":"which is these curly brackets here represent your hashmap,","startTime":162110,"duration":5800,"startOfParagraph":false},{"content":"and a hashmap is basically a mapping from some sort of key","startTime":167910,"duration":3490,"startOfParagraph":false},{"content":"to some sort of value.","startTime":171400,"duration":1940,"startOfParagraph":false},{"content":"You'll see here that we have the properties key,","startTime":173340,"duration":3100,"startOfParagraph":false},{"content":"and that's mapping onto an array, which is this whole thing.","startTime":176440,"duration":3160,"startOfParagraph":false},{"content":"We see another element of the hashmap, which is this key isAwesome,","startTime":179600,"duration":4520,"startOfParagraph":false},{"content":"which maps to a primitive value of true, in other words, a boolean.","startTime":184120,"duration":3250,"startOfParagraph":false},{"content":"Primitives can be strings. They can be integers.","startTime":187370,"duration":2050,"startOfParagraph":false},{"content":"They can be bools, anything like that.","startTime":189420,"duration":2540,"startOfParagraph":false},{"content":"And you see the contents of this array that properties points to has 2 strings in it,","startTime":191960,"duration":6450,"startOfParagraph":false},{"content":"self-similar and wonderful.","startTime":198410,"duration":1640,"startOfParagraph":false},{"content":"Those are 2 properties of JSON, and we see that JSON is awesome.","startTime":200050,"duration":7360,"startOfParagraph":false},{"content":"To look at that a little more closely I'm going to construct","startTime":207410,"duration":2650,"startOfParagraph":false},{"content":"a more complex example of JSON here.","startTime":210060,"duration":2810,"startOfParagraph":false},{"content":"Let's start with an array, for example, just an empty array.","startTime":212870,"duration":4130,"startOfParagraph":true},{"content":"But that's sort of boring, so we're going to fill it up a bit,","startTime":217000,"duration":2180,"startOfParagraph":false},{"content":"and as I said, arrays in JSON are type bound,","startTime":219180,"duration":4240,"startOfParagraph":false},{"content":"so we could also have a string here, which is hi, ","startTime":223420,"duration":2980,"startOfParagraph":false},{"content":"and that's another element of that array.","startTime":226400,"duration":2930,"startOfParagraph":false},{"content":"And likewise, we could add a hashmapping here, which is going to have a few mappings.","startTime":229330,"duration":4120,"startOfParagraph":false},{"content":"It's going to have a mapping from name to the string Billy.","startTime":233450,"duration":7020,"startOfParagraph":false},{"content":"We have a mapping from name to Billy, and we have a mapping of ","startTime":240470,"duration":4120,"startOfParagraph":false},{"content":"favorite color to blue.","startTime":244590,"duration":6270,"startOfParagraph":false},{"content":"That's basically a good example of JSON.","startTime":250860,"duration":1840,"startOfParagraph":false},{"content":"It kind of gets into—whoops, need a comma there—all of the different parts of it.","startTime":252700,"duration":5460,"startOfParagraph":false},{"content":"Again, it's not type bound at all, so you can have any kind of types","startTime":258160,"duration":2980,"startOfParagraph":false},{"content":"inside anything you want, and the idea is it's self-similar.","startTime":261140,"duration":3570,"startOfParagraph":false},{"content":"In other words, this right here is a JSON object, as is this whole thing,","startTime":264710,"duration":4120,"startOfParagraph":false},{"content":"as is just this, so you can have a primitive be an object, ","startTime":268830,"duration":4370,"startOfParagraph":false},{"content":"an array be an object or a hashmap be an object.","startTime":273200,"duration":2480,"startOfParagraph":false},{"content":"As you can kind of see, JSON is really, really useful in that it's so versatile.","startTime":275680,"duration":4590,"startOfParagraph":true},{"content":"You can have any possible data that you can conceive stored in JSON.","startTime":280270,"duration":5590,"startOfParagraph":false},{"content":"That makes it a really nice language to use with APIs","startTime":285860,"duration":2040,"startOfParagraph":false},{"content":"because it pretty much means that no matter what data that you want","startTime":287900,"duration":2870,"startOfParagraph":false},{"content":"there's going to be some way to get it back in JSON.","startTime":290770,"duration":3500,"startOfParagraph":false},{"content":"A few properties that make JSON particularly good for this sort of thing.","startTime":294270,"duration":4330,"startOfParagraph":false},{"content":"As you can see, compared to a lot of things that you've been working with in CS50","startTime":298600,"duration":3670,"startOfParagraph":false},{"content":"it's comparatively very easy to read and also very easy to write.","startTime":302270,"duration":3770,"startOfParagraph":false},{"content":"You can indent it out if you want, like I was doing in that example,","startTime":306040,"duration":3660,"startOfParagraph":false},{"content":"which gives you a nice, pretty version that you can see really well.","startTime":309700,"duration":3290,"startOfParagraph":false},{"content":"But moreover, it's also easy to read and write for a computer. ","startTime":312990,"duration":4160,"startOfParagraph":false},{"content":"In other words, it's easy to parse and easy to encode,","startTime":317150,"duration":2720,"startOfParagraph":false},{"content":"which means that it's pretty fast as far as reading the data is concerned,","startTime":319870,"duration":3950,"startOfParagraph":false},{"content":"and JSON can be generated really quickly.","startTime":323820,"duration":2640,"startOfParagraph":false},{"content":"It's also very easy to access different parts of JSON and that sort of thing.","startTime":326460,"duration":3840,"startOfParagraph":true},{"content":"That's nice, and furthermore, the fact that it's self-similar,","startTime":330300,"duration":3020,"startOfParagraph":false},{"content":"in other words, the fact that you can have JSON within JSON within JSON","startTime":333320,"duration":2770,"startOfParagraph":false},{"content":"is really nice for storing data.","startTime":336090,"duration":3950,"startOfParagraph":false},{"content":"Another part that is generally really useful in working with APIs is jQuery.","startTime":340040,"duration":5450,"startOfParagraph":false},{"content":"You've learned a little bit of JavaScript, which is a nice way","startTime":345490,"duration":3800,"startOfParagraph":false},{"content":"to manipulate HTML and CSS within a website.","startTime":349290,"duration":4420,"startOfParagraph":false},{"content":"But it can kind of be a pain to code in plain JavaScript,","startTime":353710,"duration":3480,"startOfParagraph":false},{"content":"largely because JavaScript is a really verbose language.","startTime":357190,"duration":2620,"startOfParagraph":false},{"content":"You have to learn a lot of syntax, and just to do very simple things","startTime":359810,"duration":3210,"startOfParagraph":false},{"content":"it takes a lot of code, so jQuery is a library for JavaScript.","startTime":363020,"duration":4570,"startOfParagraph":false},{"content":"In other words, it's a JavaScript file that you can load ","startTime":367590,"duration":2210,"startOfParagraph":false},{"content":"and then use jQuery functions to do certain things.","startTime":369800,"duration":2930,"startOfParagraph":false},{"content":"And jQuery basically makes your life a whole lot easier.","startTime":372730,"duration":2940,"startOfParagraph":false},{"content":"It simplifies what would take hundreds of lines in JavaScript down to a few lines in jQuery.","startTime":375670,"duration":4720,"startOfParagraph":false},{"content":"It's particularly useful if you're using APIs because generally","startTime":380390,"duration":4040,"startOfParagraph":true},{"content":"how you'll be accessing APIs is by making AJAX requests,","startTime":384430,"duration":3170,"startOfParagraph":false},{"content":"and I believe David has mentioned in lecture that AJAX requests","startTime":387600,"duration":2530,"startOfParagraph":false},{"content":"are generally when you're making a network request to some sort of server","startTime":390130,"duration":2990,"startOfParagraph":false},{"content":"and getting back some sort of data and updating a page instantaneously.","startTime":393120,"duration":4640,"startOfParagraph":false},{"content":"Whereas in plain JavaScript that would take crazy numbers of lines","startTime":397760,"duration":4080,"startOfParagraph":false},{"content":"to validate all of the headers and do all of that sort of stuff,","startTime":401840,"duration":2780,"startOfParagraph":false},{"content":"jQuery has a really simple function called AJAX,","startTime":404620,"duration":2190,"startOfParagraph":false},{"content":"and all you have to do in AJAX is give the parameters that you want to give the API,","startTime":406810,"duration":4950,"startOfParagraph":false},{"content":"the location of the API and any additional sort of options that you want to configure.","startTime":411760,"duration":5070,"startOfParagraph":false},{"content":"It's really, really nice and very useful for this kind of thing.","startTime":416830,"duration":5650,"startOfParagraph":false},{"content":"That's all we need to start getting our hands dirty in APIs.","startTime":422480,"duration":4490,"startOfParagraph":false},{"content":"I'm going to bring up a few examples and explore their different properties","startTime":426970,"duration":3250,"startOfParagraph":true},{"content":"and why they're useful for different kinds of things.","startTime":430220,"duration":2930,"startOfParagraph":false},{"content":"The first thing I'll actually show you is something that I'm working on ","startTime":433150,"duration":2420,"startOfParagraph":false},{"content":"at my research lab, which is an Ngram Viewer,","startTime":435570,"duration":2740,"startOfParagraph":false},{"content":"and basically the idea of an Ngram Viewer is you can search for some kind of word","startTime":438310,"duration":4960,"startOfParagraph":false},{"content":"or phrase and see how often it's appeared in a certain set of text over time.","startTime":443270,"duration":5570,"startOfParagraph":false},{"content":"This example here is this data set of babies","startTime":448840,"duration":4320,"startOfParagraph":false},{"content":"that were born in New York between 1920 and 2000.","startTime":453160,"duration":3320,"startOfParagraph":false},{"content":"We can search, for example, for the name Jennifer,","startTime":456480,"duration":3610,"startOfParagraph":false},{"content":"and we see that pre-1960s it really wasn't used all that much,","startTime":460090,"duration":4310,"startOfParagraph":false},{"content":"and then as we get into later years it's becoming used more and more.","startTime":464400,"duration":4500,"startOfParagraph":false},{"content":"We can also do comparisons, so if we compare Jennifer to, for example, Thomas,","startTime":468900,"duration":4780,"startOfParagraph":false},{"content":"we can see Thomas has been pretty prevalent throughout history,","startTime":473680,"duration":2840,"startOfParagraph":false},{"content":"whereas Jennifer is a more recent name.","startTime":476520,"duration":2260,"startOfParagraph":false},{"content":"We can do that kind of thing.","startTime":478780,"duration":1810,"startOfParagraph":false},{"content":"How does this application work?","startTime":480590,"duration":1870,"startOfParagraph":true},{"content":"Basically, it works via an API.","startTime":482460,"duration":3570,"startOfParagraph":false},{"content":"In other words, we have certain parameters here.","startTime":486030,"duration":2630,"startOfParagraph":false},{"content":"We have the parameters of what we're actually searching for,","startTime":488660,"duration":2700,"startOfParagraph":false},{"content":"which are these names, and then we have a few other properties,","startTime":491360,"duration":2360,"startOfParagraph":false},{"content":"like the Y axis and the X axis.","startTime":493720,"duration":2850,"startOfParagraph":false},{"content":"You can see we have a few different options as far as the ","startTime":496570,"duration":1870,"startOfParagraph":false},{"content":"time resolution to use and that sort of thing.","startTime":498440,"duration":2420,"startOfParagraph":false},{"content":"We have these options as far as what data we actually want from the database,","startTime":500860,"duration":5840,"startOfParagraph":false},{"content":"and we want to get that data back in some useful way.","startTime":506700,"duration":2700,"startOfParagraph":false},{"content":"Ordinarily, if we were querying the database directly it would sort of be a pain to do","startTime":509400,"duration":4620,"startOfParagraph":false},{"content":"because presumably this data about baby names lives in some database somewhere,","startTime":514020,"duration":4950,"startOfParagraph":false},{"content":"and it would be really complicated to have to query it manually","startTime":518970,"duration":3819,"startOfParagraph":false},{"content":"and decide exactly what data to return.","startTime":522789,"duration":3041,"startOfParagraph":false},{"content":"In other words, we only care about Jennifer and Thomas in this case,","startTime":525830,"duration":3470,"startOfParagraph":false},{"content":"and we only care about on a certain axis and all of that sort of stuff.","startTime":529300,"duration":4110,"startOfParagraph":false},{"content":"How do we get around this?","startTime":533410,"duration":2310,"startOfParagraph":true},{"content":"To dig into this API a little more I'll show you another example of this platform","startTime":535720,"duration":5480,"startOfParagraph":false},{"content":"which uses a slightly different data set.","startTime":541200,"duration":3290,"startOfParagraph":false},{"content":"This data set, instead of being baby names, is actually just the entire","startTime":544490,"duration":5460,"startOfParagraph":false},{"content":"print publication database of Open Library,","startTime":549950,"duration":2510,"startOfParagraph":false},{"content":"which is a giant source of texts published throughout the last 100 or so years.","startTime":552460,"duration":5950,"startOfParagraph":false},{"content":"The idea is we have this compository of millions and millions of text,","startTime":558410,"duration":5130,"startOfParagraph":false},{"content":"which we can now search for different words and phrases in.","startTime":563540,"duration":3880,"startOfParagraph":false},{"content":"Here's an example that varies a little differently from the previous example ","startTime":567420,"duration":3420,"startOfParagraph":false},{"content":"I showed you, which is we have these 3 search queries,","startTime":570840,"duration":2510,"startOfParagraph":false},{"content":"war, war, and the French word for war, which is guerre.","startTime":573350,"duration":2940,"startOfParagraph":false},{"content":"And we're searching within 3 different sections of the total database.","startTime":576290,"duration":4090,"startOfParagraph":false},{"content":"In other words, in this first query we're only searching in the USA,","startTime":580380,"duration":4700,"startOfParagraph":false},{"content":"in the second one only in the UK, and the third only from works published in France.","startTime":585080,"duration":6070,"startOfParagraph":false},{"content":"We see some interesting patterns emerge.","startTime":591150,"duration":1970,"startOfParagraph":false},{"content":"For example, we see right around here which—","startTime":593120,"duration":5060,"startOfParagraph":false},{"content":"oops, I messed up the axis a little bit, but you can see right in this range here","startTime":598180,"duration":4230,"startOfParagraph":false},{"content":"around the Civil War there's a big spike in the American edition","startTime":602410,"duration":3320,"startOfParagraph":false},{"content":"but not such a big spike in the other two, and that's obviously because ","startTime":605730,"duration":2610,"startOfParagraph":false},{"content":"the American Civil War was happening at that point.","startTime":608340,"duration":2540,"startOfParagraph":false},{"content":"We can see some cool stuff there,","startTime":610880,"duration":3010,"startOfParagraph":true},{"content":"but what we really care about is how we got this data.","startTime":613890,"duration":3180,"startOfParagraph":false},{"content":"I'll take you behind the scenes in this app in a little bit.","startTime":617070,"duration":4250,"startOfParagraph":false},{"content":"A neat trick is if you're working with the site and kind of want to know","startTime":621320,"duration":3220,"startOfParagraph":false},{"content":"what's going on behind the scenes, you can open up the developer tools.","startTime":624540,"duration":2890,"startOfParagraph":false},{"content":"I'm going to be using Chrome's developer tools, and to get to those","startTime":627430,"duration":2770,"startOfParagraph":false},{"content":"you can do control, shift, J, and that takes you to the JavaScript console.","startTime":630200,"duration":4960,"startOfParagraph":false},{"content":"There are a few tabs here. ","startTime":635160,"duration":2260,"startOfParagraph":false},{"content":"They can all be pretty useful under different circumstances, but I care about the network","startTime":637420,"duration":2260,"startOfParagraph":false},{"content":"tab right now, and I actually have to refresh to get that working.","startTime":639680,"duration":4470,"startOfParagraph":false},{"content":"Oh, sorry.","startTime":644150,"duration":6030,"startOfParagraph":false},{"content":"It likes to give a random example.","startTime":650180,"duration":2140,"startOfParagraph":false},{"content":"Okay, we'll use this example instead then.","startTime":652320,"duration":2380,"startOfParagraph":false},{"content":"The idea is there's this API here,","startTime":654700,"duration":6630,"startOfParagraph":true},{"content":"and you can see exactly what the API is returning.","startTime":661330,"duration":4000,"startOfParagraph":false},{"content":"This is what the application is getting back from the API having sent that request.","startTime":665330,"duration":4890,"startOfParagraph":false},{"content":"Let me zoom in a little bit, ","startTime":670220,"duration":3460,"startOfParagraph":false},{"content":"and we can basically see it's just a series of key value pairs in JSON.","startTime":673680,"duration":4660,"startOfParagraph":false},{"content":"In other words, we have this hashmap here that's mapping values.","startTime":678340,"duration":4880,"startOfParagraph":false},{"content":"In other words, it's mapping years to values.","startTime":683220,"duration":3220,"startOfParagraph":false},{"content":"In 1765 whatever word we initially searched for is used 90 times ","startTime":686440,"duration":6160,"startOfParagraph":false},{"content":"out of 1 million, so we're getting back this result.","startTime":692600,"duration":3210,"startOfParagraph":false},{"content":"It's not exactly JSON since we have this little result header here,","startTime":695810,"duration":4470,"startOfParagraph":false},{"content":"but notice that this whole object here is just a great big JSON blob.","startTime":700280,"duration":5350,"startOfParagraph":false},{"content":"We have an array here which contains this whole element,","startTime":705630,"duration":5440,"startOfParagraph":false},{"content":"and you can see that whole element ends there, and then we have another big element","startTime":711070,"duration":4520,"startOfParagraph":false},{"content":"that goes all the way down to the end, and that ends here.","startTime":715590,"duration":3840,"startOfParagraph":false},{"content":"We have a really big array with 2 objects in it, ","startTime":719430,"duration":2770,"startOfParagraph":false},{"content":"and each of those objects is a hashmap.","startTime":722200,"duration":2430,"startOfParagraph":false},{"content":"You can see within each of those hashmaps we have a mapping ","startTime":724630,"duration":2710,"startOfParagraph":false},{"content":"of this index value to 0 and this value's value to another hashmap,","startTime":727340,"duration":5360,"startOfParagraph":false},{"content":"which again is mapping X axis values to Y axis values.","startTime":732700,"duration":5660,"startOfParagraph":false},{"content":"You can see JSON gets a little bit complicated, but overall, ","startTime":738360,"duration":2610,"startOfParagraph":true},{"content":"it's actually very useful, and it's very easy to access compared to","startTime":740970,"duration":3220,"startOfParagraph":false},{"content":"other different forms of notation.","startTime":744190,"duration":3200,"startOfParagraph":false},{"content":"As far as what we're actually sending data to the API to get,","startTime":747390,"duration":3160,"startOfParagraph":false},{"content":"I'm going to go into the back end a little bit here.","startTime":750550,"duration":4140,"startOfParagraph":false},{"content":"This is the big JavaScript file that's handling all of the interactions of the web app,","startTime":754690,"duration":5160,"startOfParagraph":false},{"content":"and so we don't care about most of this, but we do care about some of it.","startTime":759850,"duration":4960,"startOfParagraph":false},{"content":"For example, we care about this buildQuery function,","startTime":764810,"duration":2600,"startOfParagraph":false},{"content":"and the idea of this function is basically it's looking around the page,","startTime":767410,"duration":3260,"startOfParagraph":false},{"content":"figuring out what the user wants to query, in other words,","startTime":770670,"duration":3080,"startOfParagraph":false},{"content":"checking those boxes where they've input their search terms,","startTime":773750,"duration":3340,"startOfParagraph":false},{"content":"checking the different Y and X axis values that they've chosen and all of that sort of thing,","startTime":777090,"duration":4290,"startOfParagraph":false},{"content":"and it's going to spit out this query value, which I can then send off to the API.","startTime":781380,"duration":5270,"startOfParagraph":false},{"content":"This looks complicated, and it is pretty complicated","startTime":786650,"duration":2530,"startOfParagraph":true},{"content":"but what I'm going to do—in fact, I'm already doing this, which is great—","startTime":789180,"duration":8910,"startOfParagraph":false},{"content":"is that I'm going to get the console to print out exactly that query value ","startTime":798090,"duration":3550,"startOfParagraph":false},{"content":"that it's sending off to the API.","startTime":801640,"duration":6470,"startOfParagraph":false},{"content":"That's actually right here. Sorry, it outputs a lot of things.","startTime":808110,"duration":2760,"startOfParagraph":false},{"content":"But this is what we care about, this object right here.","startTime":810870,"duration":2820,"startOfParagraph":false},{"content":"This is the query object.","startTime":813690,"duration":1610,"startOfParagraph":false},{"content":"In other words, this is exactly what the web application is sending to the API,","startTime":815300,"duration":5370,"startOfParagraph":false},{"content":"and so let's look inside a little bit, and we see we have a few values here.","startTime":820670,"duration":5060,"startOfParagraph":false},{"content":"We see we have this count type, which is occurrences per million words,","startTime":825730,"duration":2980,"startOfParagraph":false},{"content":"which is exactly what we've chosen in the Y axis over here.","startTime":828710,"duration":2750,"startOfParagraph":false},{"content":"That's where that's coming from.","startTime":831460,"duration":2280,"startOfParagraph":false},{"content":"We have a database value, which means that there's some certain database","startTime":833740,"duration":4270,"startOfParagraph":false},{"content":"that this data is living in, and we want to access that data specifically","startTime":838010,"duration":3600,"startOfParagraph":false},{"content":"as opposed to the baby names data, for example.","startTime":841610,"duration":3340,"startOfParagraph":false},{"content":"Then we have this groups value,","startTime":844950,"duration":3370,"startOfParagraph":false},{"content":"which is saying that we want to search by year as opposed to ","startTime":848320,"duration":3770,"startOfParagraph":false},{"content":"any other X axis value.","startTime":852090,"duration":3940,"startOfParagraph":false},{"content":"Then we have a method, which some APIs will do multiple things.","startTime":856030,"duration":3010,"startOfParagraph":false},{"content":"In other words, this API can also return other kinds of data, ","startTime":859040,"duration":3320,"startOfParagraph":false},{"content":"but in this case, we want that mapping of X axis values to Y axis values.","startTime":862360,"duration":5380,"startOfParagraph":false},{"content":"That's what that is telling it to do there,","startTime":867740,"duration":2990,"startOfParagraph":false},{"content":"and we have this search limits array, which contains 2 values.","startTime":870730,"duration":4290,"startOfParagraph":false},{"content":"The first one is what we see here, which is all of the values","startTime":875020,"duration":5700,"startOfParagraph":false},{"content":"contained within that first little box at the top.","startTime":880720,"duration":2300,"startOfParagraph":false},{"content":"In other words, we want to look for the word battle, and we want to filter it","startTime":883020,"duration":4550,"startOfParagraph":true},{"content":"by English texts within American literature.","startTime":887570,"duration":4350,"startOfParagraph":false},{"content":"We have this country, which is USA.","startTime":891920,"duration":2670,"startOfParagraph":false},{"content":"We have a language, which is English, so we have all of these different parts","startTime":894590,"duration":4540,"startOfParagraph":false},{"content":"that are all telling the API exactly what we want.","startTime":899130,"duration":3560,"startOfParagraph":false},{"content":"We don't know what the data that we get back is yet,","startTime":902690,"duration":2250,"startOfParagraph":false},{"content":"but we know that the data is going to take a certain form.","startTime":904940,"duration":6030,"startOfParagraph":false},{"content":"This example is sort of on the complicated side,","startTime":910970,"duration":2680,"startOfParagraph":false},{"content":"and you wouldn't necessarily be using an API this complex,","startTime":913650,"duration":2530,"startOfParagraph":false},{"content":"but this is to show you the range and power of what APIs can do.","startTime":916180,"duration":4420,"startOfParagraph":false},{"content":"In other words, using a relatively simple query system we basically have an input box","startTime":920600,"duration":4380,"startOfParagraph":false},{"content":"with a few other selectors in different places.","startTime":924980,"duration":4510,"startOfParagraph":false},{"content":"Let me zoom back out here.","startTime":929490,"duration":2520,"startOfParagraph":true},{"content":"We have an input box with a few different metadata selections,","startTime":932010,"duration":5710,"startOfParagraph":false},{"content":"and we have Y axis and X axis selections.","startTime":937720,"duration":2890,"startOfParagraph":false},{"content":"We don't actually have that many fields,","startTime":940610,"duration":2220,"startOfParagraph":false},{"content":"and we can see very easily we're able to query some sort of API","startTime":942830,"duration":3380,"startOfParagraph":false},{"content":"and get data back and then put it into this chart, ","startTime":946210,"duration":2300,"startOfParagraph":false},{"content":"which is then going to display it in a useful way.","startTime":948510,"duration":3570,"startOfParagraph":false},{"content":"To look at another example that might be a bit more familiar to you guys","startTime":952080,"duration":2890,"startOfParagraph":false},{"content":"we're going to turn to Facebook.","startTime":954970,"duration":1540,"startOfParagraph":false},{"content":"Facebook's API is called the Facebook Graph,","startTime":956510,"duration":2930,"startOfParagraph":false},{"content":"and basically what that means is Facebook sees itself as this massive database","startTime":959440,"duration":4950,"startOfParagraph":false},{"content":"of lots of different parts that all have certain relationships to each other.","startTime":964390,"duration":3610,"startOfParagraph":false},{"content":"In other words, I'm a user on Facebook, so I have a profile,","startTime":968000,"duration":3070,"startOfParagraph":false},{"content":"and I also have certain friends, and each of them has a profile,","startTime":971070,"duration":3240,"startOfParagraph":false},{"content":"and each of my friends has a wall, which has different comments on it,","startTime":974310,"duration":3270,"startOfParagraph":false},{"content":"and each of those comments has likes and all of that sort of thing.","startTime":977580,"duration":3220,"startOfParagraph":false},{"content":"There's lots of different parts to Facebook.","startTime":980800,"duration":2300,"startOfParagraph":true},{"content":"It's a hugely complex API, and there's tons you can do with it,","startTime":983100,"duration":3570,"startOfParagraph":false},{"content":"but it's actually pretty simple to use.","startTime":986670,"duration":1780,"startOfParagraph":false},{"content":"I'm going to start out by going to graph.facebook.com/billyjanitsch,","startTime":988450,"duration":5230,"startOfParagraph":false},{"content":"which is my unique account name, and your account name will either be","startTime":993680,"duration":4750,"startOfParagraph":false},{"content":"some kind of word if you've chosen it, or it might just be a string of numbers.","startTime":998430,"duration":5280,"startOfParagraph":false},{"content":"What we get back is pretty basic information.","startTime":1003710,"duration":2650,"startOfParagraph":false},{"content":"We see that I have a first name, which is Billy, a last name, which is Janitsch.","startTime":1006360,"duration":4100,"startOfParagraph":false},{"content":"There's a unique Facebook ID which I have.","startTime":1010460,"duration":2910,"startOfParagraph":false},{"content":"You can see that I'm male and that I have my language setting ","startTime":1013370,"duration":4550,"startOfParagraph":false},{"content":"to British English.","startTime":1017920,"duration":3370,"startOfParagraph":false},{"content":"In other words, we're seeing very basic information here.","startTime":1021290,"duration":2200,"startOfParagraph":false},{"content":"It's not too much, but it does give us an idea of what's there.","startTime":1023490,"duration":5180,"startOfParagraph":false},{"content":"We can do the same thing to David Malan, for example.","startTime":1028670,"duration":2179,"startOfParagraph":true},{"content":"I think his name is dmalan.","startTime":1030849,"duration":2750,"startOfParagraph":false},{"content":"We see David Malan has a unique ID.","startTime":1033599,"duration":2770,"startOfParagraph":false},{"content":"He has a name, first name, middle name, last name.","startTime":1036369,"duration":2931,"startOfParagraph":false},{"content":"We also see that he's male and has his language set to US English.","startTime":1039300,"duration":4910,"startOfParagraph":false},{"content":"In other words, we're seeing pretty basic information here.","startTime":1044210,"duration":2659,"startOfParagraph":false},{"content":"Now, what happens if we try to check out something else?","startTime":1046869,"duration":1991,"startOfParagraph":false},{"content":"Let's say I'm interested in what David Malan has liked on Facebook.","startTime":1048860,"duration":4200,"startOfParagraph":false},{"content":"I can do /likes. Now we've run into a problem.","startTime":1053060,"duration":3800,"startOfParagraph":false},{"content":"We've got some sort of error that says an access token ","startTime":1056860,"duration":2420,"startOfParagraph":false},{"content":"is required to request this resource.","startTime":1059280,"duration":2380,"startOfParagraph":false},{"content":"But if you think about it, that actually makes sense because it would be weird","startTime":1061660,"duration":3070,"startOfParagraph":false},{"content":"if you could access every single part of Facebook's database","startTime":1064730,"duration":3100,"startOfParagraph":false},{"content":"just from some sort of simple API, right?","startTime":1067830,"duration":2340,"startOfParagraph":false},{"content":"In other words, presumably your information can't be accessed by anyone who wants it.","startTime":1070170,"duration":5870,"startOfParagraph":false},{"content":"This error is precisely what that means.","startTime":1076040,"duration":2290,"startOfParagraph":true},{"content":"Some APIs require certain permissions in order to access their data.","startTime":1078330,"duration":5300,"startOfParagraph":false},{"content":"And even more advanced APIs, like the Facebook one,","startTime":1083630,"duration":3310,"startOfParagraph":false},{"content":"will require certain permissions to do certain things.","startTime":1086940,"duration":2900,"startOfParagraph":false},{"content":"I can see this basic information about David Malan.","startTime":1089840,"duration":2810,"startOfParagraph":false},{"content":"I can see that he's male and that he lives in the US,","startTime":1092650,"duration":3300,"startOfParagraph":false},{"content":"but I can't really see anything past that.","startTime":1095950,"duration":3320,"startOfParagraph":false},{"content":"To get around this for now, Facebook has this nice tool ","startTime":1099270,"duration":3780,"startOfParagraph":false},{"content":"which is the graph API explorer, and the idea of that is you can sort of ","startTime":1103050,"duration":4640,"startOfParagraph":false},{"content":"make up permissions for yourself based on your own account","startTime":1107690,"duration":4190,"startOfParagraph":false},{"content":"and then view things that specifically your account can view.","startTime":1111880,"duration":3800,"startOfParagraph":false},{"content":"For example, if I do graph.facebook.com/billyjanitsch/likes—","startTime":1115680,"duration":9440,"startOfParagraph":false},{"content":"whoops, I guess I have to revalidate my token here.","startTime":1125120,"duration":8390,"startOfParagraph":false},{"content":"Okay.","startTime":1133510,"duration":2440,"startOfParagraph":false},{"content":"If I do that again, great, now I see that I get this object back","startTime":1135950,"duration":5790,"startOfParagraph":false},{"content":"which says that I like pool noodles, which are in the category Games and Toys.","startTime":1141740,"duration":4560,"startOfParagraph":false},{"content":"I like walruses, which are in the category Animal.","startTime":1146300,"duration":2320,"startOfParagraph":false},{"content":"These are my actual Facebook likes. ","startTime":1148620,"duration":1560,"startOfParagraph":false},{"content":"They're kind of embarrassing.","startTime":1150180,"duration":3100,"startOfParagraph":false},{"content":"But we can see this data is all returned in JSON.","startTime":1153280,"duration":2810,"startOfParagraph":true},{"content":"It's pretty readable.","startTime":1156090,"duration":2070,"startOfParagraph":false},{"content":"In other words, we have this mapping of data to some sort of an array,","startTime":1158160,"duration":2810,"startOfParagraph":false},{"content":"and each element of this array is a hashmap which maps","startTime":1160970,"duration":4250,"startOfParagraph":false},{"content":"the name of a like and the category of a like.","startTime":1165220,"duration":3310,"startOfParagraph":false},{"content":"Each like has a unique ID.","startTime":1168530,"duration":2710,"startOfParagraph":false},{"content":"There are all sorts of different things of data that we can get,","startTime":1171240,"duration":3270,"startOfParagraph":false},{"content":"and if you're interested in using the Facebook API for a CS50 final project","startTime":1174510,"duration":3470,"startOfParagraph":false},{"content":"or for anything like that it's actually quite doable.","startTime":1177980,"duration":2740,"startOfParagraph":false},{"content":"Basically how you get around the authentication thing is Facebook ","startTime":1180720,"duration":3540,"startOfParagraph":false},{"content":"uses a system called OAuth, or Open Authentication,","startTime":1184260,"duration":3770,"startOfParagraph":false},{"content":"and I don't want to get into it now because OAuth or the different type","startTime":1188030,"duration":4840,"startOfParagraph":false},{"content":"of authentication tends to vary a lot between different APIs,","startTime":1192870,"duration":3190,"startOfParagraph":false},{"content":"so I could spend a long time going over each one, ","startTime":1196060,"duration":2260,"startOfParagraph":false},{"content":"but they're actually pretty self-explanatory.","startTime":1198320,"duration":2850,"startOfParagraph":false},{"content":"If you Google Facebook API it's very readable.","startTime":1201170,"duration":2880,"startOfParagraph":true},{"content":"There's a whole spec.","startTime":1204050,"duration":2620,"startOfParagraph":false},{"content":"For example, this is the documentation for the Facebook API,","startTime":1206670,"duration":3540,"startOfParagraph":false},{"content":"and you can see I'm on the User page, so I can learn all about the different kinds of things","startTime":1210210,"duration":3960,"startOfParagraph":false},{"content":"that are available to get as far as data","startTime":1214170,"duration":3000,"startOfParagraph":false},{"content":"and also the different permissions that I need in order to access them.","startTime":1217170,"duration":4380,"startOfParagraph":false},{"content":"As we saw, we don't need permissions to access the name or the gender,","startTime":1221550,"duration":3920,"startOfParagraph":false},{"content":"but beyond that we do need permissions for most things.","startTime":1225470,"duration":3910,"startOfParagraph":false},{"content":"This page, or rather, this website will also tell you how to get ","startTime":1229380,"duration":3660,"startOfParagraph":false},{"content":"a token to be able to authenticate yourself.","startTime":1233040,"duration":2600,"startOfParagraph":false},{"content":"Most authentication systems use some sort of token","startTime":1235640,"duration":3650,"startOfParagraph":false},{"content":"where you get this unique value, which is a really long and random string,","startTime":1239290,"duration":3590,"startOfParagraph":false},{"content":"and that way they can associate the request that you're making with you.","startTime":1242880,"duration":3360,"startOfParagraph":false},{"content":"In other words, they know that you're not doing anything suspicious with their data.","startTime":1246240,"duration":4320,"startOfParagraph":false},{"content":"They know exactly what you're getting.","startTime":1250560,"duration":2780,"startOfParagraph":false},{"content":"They also know that you have permission to view that information.","startTime":1253340,"duration":2840,"startOfParagraph":false},{"content":"If you've made a Facebook app and your app has certain users,","startTime":1256180,"duration":2930,"startOfParagraph":true},{"content":"and those users have allowed that app to access certain parts of their profile,","startTime":1259110,"duration":4270,"startOfParagraph":false},{"content":"then whatever API key or token that that app is using","startTime":1263380,"duration":4410,"startOfParagraph":false},{"content":"will be able to access the data for those users.","startTime":1267790,"duration":3300,"startOfParagraph":false},{"content":"This might sound complicated, but it's not too bad,","startTime":1271090,"duration":2690,"startOfParagraph":false},{"content":"and if you want to use Facebook I would highly recommend that you ","startTime":1273780,"duration":3030,"startOfParagraph":false},{"content":"consider playing around with their API.","startTime":1276810,"duration":2180,"startOfParagraph":false},{"content":"It's very cool, and you can do a lot of different things with it.","startTime":1278990,"duration":2620,"startOfParagraph":false},{"content":"If the user grants you these permissions you can even go back to the API","startTime":1281610,"duration":3270,"startOfParagraph":false},{"content":"and say I want to actually post to this user's wall, or I want to have them post a photo,","startTime":1284880,"duration":3940,"startOfParagraph":false},{"content":"and that's why on your news feed you'll sometimes get those annoying things","startTime":1288820,"duration":3570,"startOfParagraph":false},{"content":"saying your friend has watched this video on some sort of weird site or something like that.","startTime":1292390,"duration":5450,"startOfParagraph":false},{"content":"That's because that app has been granted access to post on that person's wall.","startTime":1297840,"duration":5280,"startOfParagraph":false},{"content":"The idea overall, the Facebook API is pretty complicated but also really useful.","startTime":1303120,"duration":5230,"startOfParagraph":false},{"content":"Definitely worth checking out if you're still looking for a final project.","startTime":1308350,"duration":4870,"startOfParagraph":false},{"content":"Another suite of APIs that I'm going to go over is CS50 APIs.","startTime":1313220,"duration":4710,"startOfParagraph":true},{"content":"Let me zoom in here.","startTime":1317930,"duration":2140,"startOfParagraph":false},{"content":"CS50 has actually put together a whole series of APIs","startTime":1320070,"duration":3320,"startOfParagraph":false},{"content":"that you can use for a final project or just for anything that you're making.","startTime":1323390,"duration":3690,"startOfParagraph":false},{"content":"And they're mostly Harvard related, and they vary from the HUDS menu,","startTime":1327080,"duration":5750,"startOfParagraph":false},{"content":"for example, to this Harvard Events API, which will let you access a list of","startTime":1332830,"duration":4950,"startOfParagraph":false},{"content":"different events that are going on at Harvard and that sort of thing.","startTime":1337780,"duration":3510,"startOfParagraph":false},{"content":"And so we can click on any one of these and get a spec for it,","startTime":1341290,"duration":3220,"startOfParagraph":false},{"content":"which you'll be able to find for any API, and the idea is ","startTime":1344510,"duration":3580,"startOfParagraph":false},{"content":"it lets you know, A, specifically what to request from the API and how to request it.","startTime":1348090,"duration":5830,"startOfParagraph":false},{"content":"In other words, if I want all events that are happening tomorrow","startTime":1353920,"duration":3450,"startOfParagraph":false},{"content":"then I've got to obviously give it that date that I want in a certain format,","startTime":1357370,"duration":5180,"startOfParagraph":false},{"content":"and B, it will tell me exactly what it's going to give back to me.","startTime":1362550,"duration":3480,"startOfParagraph":false},{"content":"It will say I'm going to return you this JSON object, ","startTime":1366030,"duration":2560,"startOfParagraph":false},{"content":"or like you can see, there are different formats.","startTime":1368590,"duration":2370,"startOfParagraph":false},{"content":"You can also return the data as a CSV, for example.","startTime":1370960,"duration":3090,"startOfParagraph":true},{"content":"But you know exactly how that data is going to look when you get it back","startTime":1374050,"duration":3570,"startOfParagraph":false},{"content":"so you can expect to do certain things with it.","startTime":1377620,"duration":2990,"startOfParagraph":false},{"content":"We can scroll down and see, for example, if we want to query the API","startTime":1380610,"duration":6630,"startOfParagraph":false},{"content":"to get a calendar, then we can use this particular URL","startTime":1387240,"duration":4260,"startOfParagraph":false},{"content":"and give it certain parameters which are going to be the data that we want exactly.","startTime":1391500,"duration":4980,"startOfParagraph":false},{"content":"And likewise, if we want the data back in a certain format,","startTime":1396480,"duration":3060,"startOfParagraph":false},{"content":"then we can ask it to output the data in a CSV,","startTime":1399540,"duration":4250,"startOfParagraph":false},{"content":"and that's just another parameter that we're passing to the API.","startTime":1403790,"duration":3910,"startOfParagraph":false},{"content":"Lots of cool things to do there. ","startTime":1407700,"duration":1510,"startOfParagraph":false},{"content":"I would definitely recommend checking out the CS50 APIs.","startTime":1409210,"duration":3340,"startOfParagraph":false},{"content":"I'm going to look at this Harvard Food API in particular for a little bit.","startTime":1412550,"duration":3450,"startOfParagraph":true},{"content":"One thing I've actually designed is this Harvard Noms website,","startTime":1416000,"duration":3870,"startOfParagraph":false},{"content":"which uses the CS50 Food API to retrieve the HUDS menu for the day.","startTime":1419870,"duration":5060,"startOfParagraph":false},{"content":"And for extension school people, HUDS is the dining service at Harvard.","startTime":1424930,"duration":5470,"startOfParagraph":false},{"content":"What you get is this page which contains all of the meals for the day, so we see lunch.","startTime":1430400,"duration":4730,"startOfParagraph":false},{"content":"We have a few different categories. We have the bean and whole grain station.","startTime":1435130,"duration":3000,"startOfParagraph":false},{"content":"We have the brown rice station.","startTime":1438130,"duration":2210,"startOfParagraph":false},{"content":"We can see for brunch we have these few food items.","startTime":1440340,"duration":3020,"startOfParagraph":false},{"content":"If we click on them, then we get the nutrition information.","startTime":1443360,"duration":3670,"startOfParagraph":false},{"content":"You see this is the nutrition information for grapefruit, in case you were wondering.","startTime":1447030,"duration":5210,"startOfParagraph":false},{"content":"And so again, we're going to peer into the back end here a little bit ","startTime":1452240,"duration":2630,"startOfParagraph":false},{"content":"and see what exactly this is doing to get this data.","startTime":1454870,"duration":3660,"startOfParagraph":false},{"content":"And it turns out to not actually be very complex at all.","startTime":1458530,"duration":3180,"startOfParagraph":false},{"content":"This file looks a little messy, but keep in mind that this is handling the entire website,","startTime":1461710,"duration":7010,"startOfParagraph":false},{"content":"and if I scroll down we see this change data function.","startTime":1468720,"duration":5410,"startOfParagraph":false},{"content":"Now, just to be clear, this is written in CoffeeScript,","startTime":1474130,"duration":2500,"startOfParagraph":true},{"content":"which is a language that you probably haven't seen before.","startTime":1476630,"duration":2940,"startOfParagraph":false},{"content":"But it's pretty readable, so I'll walk through it as though it were pseudocode.","startTime":1479570,"duration":5240,"startOfParagraph":false},{"content":"Change date is a function that's going to take in this date value,","startTime":1484810,"duration":4270,"startOfParagraph":false},{"content":"and it's also going to take in a first, which we don't care about as much.","startTime":1489080,"duration":2660,"startOfParagraph":false},{"content":"But the important thing is that it has this date,","startTime":1491740,"duration":2370,"startOfParagraph":false},{"content":"and that date is the day that we want to request all of the food items for.","startTime":1494110,"duration":5970,"startOfParagraph":false},{"content":"And then you see we have a little bit of syntax here,","startTime":1500080,"duration":3950,"startOfParagraph":false},{"content":"which is basically parsing that date into a readable format.","startTime":1504030,"duration":4970,"startOfParagraph":false},{"content":"In other words, the API requires the date in a certain format.","startTime":1509000,"duration":2920,"startOfParagraph":false},{"content":"You can't just say November 16th, 2012 AD.","startTime":1511920,"duration":5470,"startOfParagraph":false},{"content":"It won't know what to do with that. It wants the date in a specific format.","startTime":1517390,"duration":2930,"startOfParagraph":false},{"content":"All we're doing here is giving it exactly that format, ","startTime":1520320,"duration":2910,"startOfParagraph":false},{"content":"which is a year value and then a hyphen, a month value, ","startTime":1523230,"duration":3290,"startOfParagraph":false},{"content":"another hyphen and the date value.","startTime":1526520,"duration":2900,"startOfParagraph":false},{"content":"And we also say we want the data to be output in JSON.","startTime":1529420,"duration":5490,"startOfParagraph":false},{"content":"Now we're making this AJAX request, and as I mentioned earlier,","startTime":1534910,"duration":2650,"startOfParagraph":true},{"content":"jQuery has this super useful AJAX function which all you need to do is specify","startTime":1537560,"duration":4120,"startOfParagraph":false},{"content":"a few parameters down here, and it will give you back exactly what you want.","startTime":1541680,"duration":4100,"startOfParagraph":false},{"content":"We're telling it that the URL we want it to go to is this CS50 Food API,","startTime":1545780,"duration":4710,"startOfParagraph":false},{"content":"which we got from the spec.","startTime":1550490,"duration":1780,"startOfParagraph":false},{"content":"We say that we want the data in JSON and that ","startTime":1552270,"duration":4460,"startOfParagraph":false},{"content":"we're going to give it this data which we've defined up here.","startTime":1556730,"duration":2760,"startOfParagraph":false},{"content":"This is the day we want the food items for.","startTime":1559490,"duration":3180,"startOfParagraph":false},{"content":"And then all we have to do is define some sort of success function,","startTime":1562670,"duration":5120,"startOfParagraph":false},{"content":"which is basically what happens when the API returns that data.","startTime":1567790,"duration":4190,"startOfParagraph":false},{"content":"In other words, we've packaged up all of the parameters that we want,","startTime":1571980,"duration":3510,"startOfParagraph":false},{"content":"which in this case is the day that we want it and the fact that we want it in JSON,","startTime":1575490,"duration":5040,"startOfParagraph":false},{"content":"and we sent it off to the API, so now the API is saying, okay,","startTime":1580530,"duration":3310,"startOfParagraph":false},{"content":"here is your data, I got it back for you.","startTime":1583840,"duration":2510,"startOfParagraph":false},{"content":"We have the success function, which means given that the API","startTime":1586350,"duration":3580,"startOfParagraph":false},{"content":"successfully returns some data, what do we do with it?","startTime":1589930,"duration":2300,"startOfParagraph":false},{"content":"And it turns out that all we do is call this update menu function","startTime":1592230,"duration":3750,"startOfParagraph":true},{"content":"with whatever the API has returned, so we can search for that","startTime":1595980,"duration":6700,"startOfParagraph":false},{"content":"and see that all we're doing is using a bunch of new syntax here","startTime":1602680,"duration":5290,"startOfParagraph":false},{"content":"to update the HTML and insert this new data.","startTime":1607970,"duration":4250,"startOfParagraph":false},{"content":"What this allows is we have these arrows on either side, and we can click,","startTime":1612220,"duration":4360,"startOfParagraph":false},{"content":"and now we're looking at the data for the next day and again for the next day,","startTime":1616580,"duration":4480,"startOfParagraph":false},{"content":"and each time it's updating that date value and querying the API,","startTime":1621060,"duration":3760,"startOfParagraph":false},{"content":"getting back some data and putting it into the site.","startTime":1624820,"duration":2690,"startOfParagraph":false},{"content":"Again, you can see, super, super useful.","startTime":1627510,"duration":3080,"startOfParagraph":false},{"content":"This app took me a few hours to hack together,","startTime":1630590,"duration":3820,"startOfParagraph":false},{"content":"and I have a bit more experience, obviously, but your CS50 final project","startTime":1634410,"duration":5730,"startOfParagraph":false},{"content":"can look something very much like this.","startTime":1640140,"duration":2730,"startOfParagraph":false},{"content":"APIs are super powerful for the amount of effort that they take.","startTime":1642870,"duration":6670,"startOfParagraph":true},{"content":"The last thing I'm going to go over is a few more APIs broadly.","startTime":1649540,"duration":3260,"startOfParagraph":false},{"content":"I won't get as far into them as far as what they do specifically,","startTime":1652800,"duration":2680,"startOfParagraph":false},{"content":"but I'll give you an idea of what's out there.","startTime":1655480,"duration":3260,"startOfParagraph":false},{"content":"2 really useful ones, if you're interested in data analysis or visualization ","startTime":1658740,"duration":3960,"startOfParagraph":false},{"content":"or anything like that, are Freebase and Wikipedia.","startTime":1662700,"duration":3260,"startOfParagraph":false},{"content":"Wikipedia—presumably you all know—is a free online encyclopedia,","startTime":1665960,"duration":3840,"startOfParagraph":false},{"content":"and it actually has an API, so if you want to, for example,","startTime":1669800,"duration":3430,"startOfParagraph":false},{"content":"get all of the texts and the articles for octopus","startTime":1673230,"duration":3020,"startOfParagraph":false},{"content":"you can very easily do that.","startTime":1676250,"duration":1780,"startOfParagraph":false},{"content":"Just say hey, Wikipedia API, I'd like the data returned as this,","startTime":1678030,"duration":4270,"startOfParagraph":false},{"content":"and I'd like it in this format, and the article I'd like is octopus,","startTime":1682300,"duration":4710,"startOfParagraph":false},{"content":"and very quickly it will give you back that information.","startTime":1687010,"duration":2810,"startOfParagraph":false},{"content":"That can be really useful if you want to make some sort of site","startTime":1689820,"duration":2410,"startOfParagraph":false},{"content":"that's a better viewer for Wikipedia or something like that.","startTime":1692230,"duration":3970,"startOfParagraph":false},{"content":"Freebase is sort of similar, although it's a little bit harder as far as API.","startTime":1696200,"duration":5150,"startOfParagraph":true},{"content":"Freebase is like Wikipedia in that it's an online encyclopedia","startTime":1701350,"duration":3040,"startOfParagraph":false},{"content":"which contains lots and lots of different data about all sorts of different topics,","startTime":1704390,"duration":4660,"startOfParagraph":false},{"content":"but it's stored in a relational database, which is slightly different from Wikipedia.","startTime":1709050,"duration":4100,"startOfParagraph":false},{"content":"Wikipedia has its articles and articles linked to other articles,","startTime":1713150,"duration":3260,"startOfParagraph":false},{"content":"but for the most part, if you want the data for octopus,","startTime":1716410,"duration":2450,"startOfParagraph":false},{"content":"you go to the octopus article, get that data, and you have a bunch of text","startTime":1718860,"duration":3130,"startOfParagraph":false},{"content":"about octopuses, so that's great.","startTime":1721990,"duration":1840,"startOfParagraph":false},{"content":"Freebase works in a slightly more complicated manner in that","startTime":1723830,"duration":3040,"startOfParagraph":false},{"content":"everything is related to one another.","startTime":1726870,"duration":2060,"startOfParagraph":false},{"content":"In other words, if we're searching for octopus","startTime":1728930,"duration":3690,"startOfParagraph":false},{"content":"then it has a bunch of categories associated with it.","startTime":1732620,"duration":2320,"startOfParagraph":false},{"content":"For example, it's an animal, it lives underwater, ","startTime":1734940,"duration":2980,"startOfParagraph":true},{"content":"it has a certain body temperature.","startTime":1737920,"duration":1790,"startOfParagraph":false},{"content":"I don't know.","startTime":1739710,"duration":1500,"startOfParagraph":false},{"content":"And all of these categories are links to other places where you can go ","startTime":1741210,"duration":3020,"startOfParagraph":false},{"content":"to see things with that same category.","startTime":1744230,"duration":2410,"startOfParagraph":false},{"content":"In other words, the octopus data set would contain a link to the data set for all animals,","startTime":1746640,"duration":6810,"startOfParagraph":false},{"content":"and that would let me move around in the database really quickly.","startTime":1753450,"duration":3340,"startOfParagraph":false},{"content":"This can be very useful if you're doing something like comparisons.","startTime":1756790,"duration":4950,"startOfParagraph":false},{"content":"In other words, given a certain thing, you want to see ","startTime":1761740,"duration":2750,"startOfParagraph":false},{"content":"what else it's related to and see what else it's not related to.","startTime":1764490,"duration":3400,"startOfParagraph":false},{"content":"That sort of thing. It can be useful in a number of ways.","startTime":1767890,"duration":2810,"startOfParagraph":false},{"content":"If you're looking for more of a challenge and to be able to do some more complex things","startTime":1770700,"duration":3550,"startOfParagraph":false},{"content":"I would consider taking a look at the Freebase API.","startTime":1774250,"duration":4490,"startOfParagraph":false},{"content":"But largely, Wikipedia is a very simple place to go as far as getting information.","startTime":1778740,"duration":5930,"startOfParagraph":false},{"content":"Another place that I'll look at is Last.fm, and I'm actually going to go to the site","startTime":1784670,"duration":3670,"startOfParagraph":false},{"content":"in case some people aren't familiar, but Last.fm is basically a music","startTime":1788340,"duration":5460,"startOfParagraph":false},{"content":"tastes and recommendations website.","startTime":1793800,"duration":3420,"startOfParagraph":false},{"content":"You can make an account.","startTime":1797220,"duration":1780,"startOfParagraph":false},{"content":"You can start uploading music from your music player","startTime":1799000,"duration":5250,"startOfParagraph":false},{"content":"to the website, and basically it will start giving you music recommendations","startTime":1804250,"duration":3770,"startOfParagraph":false},{"content":"based on what you listen to.","startTime":1808020,"duration":2010,"startOfParagraph":false},{"content":"For example, if you go to your profile page—this is mine—","startTime":1810030,"duration":4240,"startOfParagraph":true},{"content":"you can see you have a list of recently listened to tracks.","startTime":1814270,"duration":3910,"startOfParagraph":false},{"content":"You can see overall favorite artists, all of that sort of thing,","startTime":1818180,"duration":4370,"startOfParagraph":false},{"content":"and again, there's a big API behind Last.fm,","startTime":1822550,"duration":2730,"startOfParagraph":false},{"content":" and you can use it to do lots and lots of really cool things.","startTime":1825280,"duration":4080,"startOfParagraph":false},{"content":"For example, I'll go to a friend's page who has this Last.fm Tools website.","startTime":1829360,"duration":9510,"startOfParagraph":false},{"content":"This is actually another platform that's built on the Last.fm API,","startTime":1838870,"duration":3510,"startOfParagraph":false},{"content":"and it does a number of pretty interesting things.","startTime":1842380,"duration":3040,"startOfParagraph":false},{"content":"If I log in with my user name, for example, ","startTime":1845420,"duration":4840,"startOfParagraph":false},{"content":"I can ask it to generate a tag cloud, for example,","startTime":1850260,"duration":2850,"startOfParagraph":false},{"content":"and what that's going to do is give me back an image of","startTime":1853110,"duration":3370,"startOfParagraph":false},{"content":"all the different genres and that sort of thing that I like to listen to.","startTime":1856480,"duration":3370,"startOfParagraph":false},{"content":"How is it doing this? ","startTime":1859850,"duration":1560,"startOfParagraph":false},{"content":"Very basically it's saying to the Last.fm API here's this user.","startTime":1861410,"duration":4260,"startOfParagraph":false},{"content":"I'd like to know the genre of every song that they've ever listened to,","startTime":1865670,"duration":5040,"startOfParagraph":false},{"content":"and you can do that by making a pretty simple AJAX call to the Last.fm API.","startTime":1870710,"duration":4420,"startOfParagraph":false},{"content":"You'll get back a big list, and then obviously some other stuff is being done","startTime":1875130,"duration":3860,"startOfParagraph":false},{"content":"to turn it into a word cloud, but you can see overall","startTime":1878990,"duration":3290,"startOfParagraph":false},{"content":"it's very easy to access and very easy to use.","startTime":1882280,"duration":3570,"startOfParagraph":false},{"content":"Really nice for a number of things.","startTime":1885850,"duration":4900,"startOfParagraph":false},{"content":"I think that's about all I'll say overall.","startTime":1890750,"duration":5190,"startOfParagraph":true},{"content":"One last thing I'll mention about APIs in general is that ","startTime":1895940,"duration":3100,"startOfParagraph":false},{"content":"you'll sometimes run into something called rate limiting,","startTime":1899040,"duration":2800,"startOfParagraph":false},{"content":"and the idea of rate limiting is you don't want to abuse APIs.","startTime":1901840,"duration":3100,"startOfParagraph":false},{"content":"In other words, it's really nice that a lot of these websites have APIs","startTime":1904940,"duration":3190,"startOfParagraph":false},{"content":"that you can go to and use for free.","startTime":1908130,"duration":2940,"startOfParagraph":false},{"content":"However, if you're making millions or billions of requests per day,","startTime":1911070,"duration":3390,"startOfParagraph":false},{"content":"for example, if you're stuck in an infinite loop that's infinitely querying","startTime":1914460,"duration":3150,"startOfParagraph":false},{"content":"some sort of API and getting back a huge amount of data,","startTime":1917610,"duration":3070,"startOfParagraph":false},{"content":"obviously that's not good, so what a lot of APIs do is have this rate limiting feature","startTime":1920680,"duration":3890,"startOfParagraph":false},{"content":"that says you can only make 1,000 requests per day per IP address or something like that.","startTime":1924570,"duration":5400,"startOfParagraph":false},{"content":"And if you're doing a lot of testing and that sort of thing, ","startTime":1929970,"duration":2570,"startOfParagraph":false},{"content":"you'll sometimes run into that, and suddenly it will shut you off ","startTime":1932540,"duration":2350,"startOfParagraph":false},{"content":"and say no, I'm not giving you any more data.","startTime":1934890,"duration":3390,"startOfParagraph":false},{"content":"What you want to do is play by the rules.","startTime":1938280,"duration":1720,"startOfParagraph":true},{"content":"You want to make sure that you read the API spec carefully.","startTime":1940000,"duration":2950,"startOfParagraph":false},{"content":"If it has certain rules attached to it, like you can only make X queries per day","startTime":1942950,"duration":3380,"startOfParagraph":false},{"content":"or you can only access a part of the database a certain number of times ","startTime":1946330,"duration":3670,"startOfParagraph":false},{"content":"or something like that you want to make sure you stick to that.","startTime":1950000,"duration":2900,"startOfParagraph":false},{"content":"As long as you play within those rules you'll probably have a really nice time using APIs.","startTime":1952900,"duration":5460,"startOfParagraph":false},{"content":"Your overall takeaway is APIs are really, really useful.","startTime":1958360,"duration":3670,"startOfParagraph":false},{"content":"There's an API for almost any big web service out there.","startTime":1962030,"duration":3580,"startOfParagraph":true},{"content":"Pretty much any part of the Google Tools Suite, Google Maps, Google Earth,","startTime":1965610,"duration":5090,"startOfParagraph":false},{"content":"GMail, Google Calendar, all of those things have APIs.","startTime":1970700,"duration":3690,"startOfParagraph":false},{"content":"You can use them to both get data from the server and send data to the server.","startTime":1974390,"duration":3890,"startOfParagraph":false},{"content":"In other words, if you wanted to make a calendar app that can update ","startTime":1978280,"duration":2590,"startOfParagraph":false},{"content":"someone's Google Calendar, there's an API for that.","startTime":1980870,"duration":3320,"startOfParagraph":false},{"content":"If you want to make something that's going to tell you where ","startTime":1984190,"duration":3620,"startOfParagraph":false},{"content":"the location of a certain address is you can use the Google Maps API for that.","startTime":1987810,"duration":4720,"startOfParagraph":false},{"content":"APIs are fantastically useful, and they're everywhere.","startTime":1992530,"duration":3330,"startOfParagraph":false},{"content":"If you're interested in some sort of idea, ","startTime":1995860,"duration":2840,"startOfParagraph":false},{"content":"there's probably a related API that you can use to get a lot of data ","startTime":1998700,"duration":3470,"startOfParagraph":false},{"content":"very quickly and very simply.","startTime":2002170,"duration":2890,"startOfParagraph":false},{"content":"If you're still looking for a project or if you just want to play around ","startTime":2005060,"duration":3080,"startOfParagraph":true},{"content":"with something in general, APIs are definitely worth doing.","startTime":2008140,"duration":3680,"startOfParagraph":false},{"content":"Thanks, and I'm happy to answer any questions that you guys may have.","startTime":2011820,"duration":5380,"startOfParagraph":false},{"content":"Okay, thanks a lot.","startTime":2017200,"duration":7700,"startOfParagraph":false},{"content":"[CS50.TV]","startTime":2024900,"duration":3100,"startOfParagraph":false}]}