using yql sensibly - yuiconf 2010

114
Caching, collating and filtering data Using YQL sensibly

Upload: christian-heilmann

Post on 05-Dec-2014

6.409 views

Category:

Documents


5 download

DESCRIPTION

YQL is an amazing tool to use and offer APIs to the world. As you can do the lot in JavaScript it is pretty simple to get started. There is however also the option that you do things wrong and make your end users and yourself unhappy. This talk works around some of the issues you might face.

TRANSCRIPT

Page 1: Using YQL Sensibly - YUIConf 2010

Caching, collating and filtering data

Using YQL sensibly

Page 2: Using YQL Sensibly - YUIConf 2010

What is the web?

Page 3: Using YQL Sensibly - YUIConf 2010

Was ist das Web?

Data + Interfaces

Page 4: Using YQL Sensibly - YUIConf 2010

Lots of yummy, yummy data.

Page 5: Using YQL Sensibly - YUIConf 2010

Everybody benefits from APIs.

Page 6: Using YQL Sensibly - YUIConf 2010

Companies get their data into environments they could never reach.

Page 7: Using YQL Sensibly - YUIConf 2010

Developers can build products without buying data or writing code.

Page 8: Using YQL Sensibly - YUIConf 2010

Let’s play with two examples.

Page 9: Using YQL Sensibly - YUIConf 2010

Build a system to calculate the distance between two places on Earth.

Page 10: Using YQL Sensibly - YUIConf 2010

Use a map service?

Page 11: Using YQL Sensibly - YUIConf 2010

Raw data and info about the places would be better to have.

Page 12: Using YQL Sensibly - YUIConf 2010

Simple Plan:1. Find the location of the two places

on Earth

2. Calculate the distance.

Page 13: Using YQL Sensibly - YUIConf 2010

Earth Data = Yahoo GeoPlanet

Page 14: Using YQL Sensibly - YUIConf 2010

Yahoo GeoPlanet is a data set that has information about the location of places on Earth.

http://developer.yahoo.com/geo/geoplanet/

Page 15: Using YQL Sensibly - YUIConf 2010
Page 17: Using YQL Sensibly - YUIConf 2010

= Latitude+Longitude

Page 18: Using YQL Sensibly - YUIConf 2010

Distance?

Page 20: Using YQL Sensibly - YUIConf 2010

Putting it all together...

Page 21: Using YQL Sensibly - YUIConf 2010

Putting it all together...

Page 22: Using YQL Sensibly - YUIConf 2010
Page 23: Using YQL Sensibly - YUIConf 2010
Page 24: Using YQL Sensibly - YUIConf 2010
Page 25: Using YQL Sensibly - YUIConf 2010

A few annoyances1. Multiple script generation (order?

what if one breaks?)

2. Access keys readable in source.

Page 26: Using YQL Sensibly - YUIConf 2010

Building a system to translate foreign tweets.

Page 27: Using YQL Sensibly - YUIConf 2010

Twitter is multilingual but doesn’t translate.

Page 28: Using YQL Sensibly - YUIConf 2010

Google has a translation service though.

Page 29: Using YQL Sensibly - YUIConf 2010

A simple plan:1. Investigate Twitter’s search API and

Google’s translation API and if needed, get keys.

2. Get the results from Twitter for a certain search.

3. Loop over the results, see which ones are not in English, and then translate them with the Google Translation API.

Page 30: Using YQL Sensibly - YUIConf 2010

Really not that much difference in code.

Page 31: Using YQL Sensibly - YUIConf 2010

It also suffers from the same issues.

Page 32: Using YQL Sensibly - YUIConf 2010

1. Asynchronous lookups with generated script nodes are a pain to get right - what if one breaks?

2. Depending on how many Tweets are not in English, you have to hammer Google’s translation API which slows down your overall app.

Page 33: Using YQL Sensibly - YUIConf 2010

YUI fixes a few of those issues.1. Using JSONP you can have success

and failure events.

2. You can also provide timeouts

IO

JSON

JSON-P

JSON-P

YQL-QueryGET

Page 34: Using YQL Sensibly - YUIConf 2010

Still, it would be nice to have one request, right?

Page 35: Using YQL Sensibly - YUIConf 2010

Simplifying access.

Page 36: Using YQL Sensibly - YUIConf 2010

YQL http://developer.yahoo.com/yql/console/

Page 37: Using YQL Sensibly - YUIConf 2010

YQL http://developer.yahoo.com/yql/console/

select {what} from {where} where {conditions}

Page 38: Using YQL Sensibly - YUIConf 2010

Foreign Tweets?

Page 39: Using YQL Sensibly - YUIConf 2010

select text from twitter.search where q=”ft2010” and iso_language_code=”pl”

Page 40: Using YQL Sensibly - YUIConf 2010

select * from google.translate where q in (select text from twitter.search where q=”ft2010” and iso_language_code=”pl”) and target=”en”

Page 41: Using YQL Sensibly - YUIConf 2010

Re-using cool data on the web?

Page 43: Using YQL Sensibly - YUIConf 2010
Page 44: Using YQL Sensibly - YUIConf 2010
Page 47: Using YQL Sensibly - YUIConf 2010

Instead of going crazy filtering and sorting in JS...

Page 48: Using YQL Sensibly - YUIConf 2010

...use the YQL server and then have a very simple JS for displaying.

Page 49: Using YQL Sensibly - YUIConf 2010

Using web services with YQL in JS.

Page 50: Using YQL Sensibly - YUIConf 2010

YQL is a web service endpoint on its own...

Page 51: Using YQL Sensibly - YUIConf 2010

https://query.yahooapis.com/v1/public/yql?q={uri-encoded-query}&format={xml|json}&diagnostics={true|false}&callback={function}&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

Page 52: Using YQL Sensibly - YUIConf 2010
Page 53: Using YQL Sensibly - YUIConf 2010

Special case: Scraping

http://www.flickr.com/photos/fdtate/4426760544/

Page 55: Using YQL Sensibly - YUIConf 2010
Page 57: Using YQL Sensibly - YUIConf 2010

http://y.ahoo.it/r/ENSPGm

Page 59: Using YQL Sensibly - YUIConf 2010

HTML as JSON is not fun.

Page 60: Using YQL Sensibly - YUIConf 2010

JSON-P-X = HTML as a string in a JSON-P container!

Page 62: Using YQL Sensibly - YUIConf 2010

Using YQL re-use of web content is very easy indeed.

Page 63: Using YQL Sensibly - YUIConf 2010

YUI3’s YQL-Query makes it even better!

Page 64: Using YQL Sensibly - YUIConf 2010

Be safe, be good...

Page 65: Using YQL Sensibly - YUIConf 2010

Don’t rely on data arriving - test for it!

Page 66: Using YQL Sensibly - YUIConf 2010

XML to JSON?

Page 67: Using YQL Sensibly - YUIConf 2010

XML to JSON?

Page 68: Using YQL Sensibly - YUIConf 2010

Using JSON is easy with libraries.

Page 69: Using YQL Sensibly - YUIConf 2010

$.getJSON(url+'&callback=?', function(data){});

JSON-P and jQuery:

Page 70: Using YQL Sensibly - YUIConf 2010

$.ajax({ url: url, dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'ohyeah'});function ohyeah(data){}

JSON-P and jQuery:

Page 71: Using YQL Sensibly - YUIConf 2010

Which one to use?

Page 72: Using YQL Sensibly - YUIConf 2010

getJSON() is dangerous with other people’s data.

Page 74: Using YQL Sensibly - YUIConf 2010

Cachebreaking is not a good idea.

Page 75: Using YQL Sensibly - YUIConf 2010

Local caching is a good idea.

Page 76: Using YQL Sensibly - YUIConf 2010

Cookies suck, though.

Page 77: Using YQL Sensibly - YUIConf 2010

Would be good to have a better solution for that.

Page 78: Using YQL Sensibly - YUIConf 2010

localStorage = cookies on steroids.

Page 79: Using YQL Sensibly - YUIConf 2010

if(('localStorage' in window) && window['localStorage'] !== null){localStorage.setItem( 'cake', 'much better than cookies')}

Page 80: Using YQL Sensibly - YUIConf 2010

if(('localStorage' in window) && window['localStorage'] !== null){var what = localStorage.getItem( 'cake')// what -> 'much better than cookies'}

Page 81: Using YQL Sensibly - YUIConf 2010

localStorage only stores Strings - use JSON to work around that.

Page 82: Using YQL Sensibly - YUIConf 2010

if(('localStorage' in window) && window['localStorage'] !== null){localStorage.setItem( 'cake', JSON.stringify( {yummy:‘yes’,candles:5} ));}

Page 83: Using YQL Sensibly - YUIConf 2010

if(('localStorage' in window) && window['localStorage'] !== null){var what = JSON.parse( localStorage.getItem('cake'));// what -> Object{...} // and not [Object object]}

Page 84: Using YQL Sensibly - YUIConf 2010

Let’s wrap this up in a function.

Page 85: Using YQL Sensibly - YUIConf 2010

yql - the queryid - storage key namecacheage - how long to cachecallback - obvious, isn’t it?

https://github.com/codepo8/yql-localcache

Page 86: Using YQL Sensibly - YUIConf 2010

Browsers supporting localStorage fetch the data every hour.

Page 87: Using YQL Sensibly - YUIConf 2010

Others still work, but load the data every time.

Page 88: Using YQL Sensibly - YUIConf 2010

callback gets an object with two properties:

data - guess what?type - cached|live|freshcache

Page 89: Using YQL Sensibly - YUIConf 2010

Libraries offer storage fallbacks for legacy browers via Flash - YUI is of course one of them.

Page 90: Using YQL Sensibly - YUIConf 2010

Offering your own API.

Page 91: Using YQL Sensibly - YUIConf 2010

To get your own API into YQL all you need to do is write an XML schema and put it on GitHub.

Page 93: Using YQL Sensibly - YUIConf 2010

YQL allows you to write “executable tables”...

Page 94: Using YQL Sensibly - YUIConf 2010

...which means you can convert data with JavaScript that will be executed server-side.

Page 95: Using YQL Sensibly - YUIConf 2010

Our earlier examples as YQL APIs are...

Page 96: Using YQL Sensibly - YUIConf 2010

Twitter translate example:

Page 97: Using YQL Sensibly - YUIConf 2010

Offering your own API.

Page 98: Using YQL Sensibly - YUIConf 2010

Offering your own API.

Page 99: Using YQL Sensibly - YUIConf 2010
Page 100: Using YQL Sensibly - YUIConf 2010

SELECT * FROM twitter.translate WHERE language="en" and search="warszawa" and amount="20"

Page 101: Using YQL Sensibly - YUIConf 2010
Page 102: Using YQL Sensibly - YUIConf 2010
Page 103: Using YQL Sensibly - YUIConf 2010

Distance example:

Page 104: Using YQL Sensibly - YUIConf 2010
Page 105: Using YQL Sensibly - YUIConf 2010
Page 106: Using YQL Sensibly - YUIConf 2010

SELECT * FROM geo.distance WHERE place1=”london” and place2="warsaw"

Page 107: Using YQL Sensibly - YUIConf 2010
Page 109: Using YQL Sensibly - YUIConf 2010

Using your JS tables.

Page 110: Using YQL Sensibly - YUIConf 2010

Write your schema, put it on the web...

Page 111: Using YQL Sensibly - YUIConf 2010

use “http://awesomeserver.com/distance.xml” as distance; SELECT * FROM distance WHERE place1=”london” and place2="warsaw"

use USE to use it!

Page 112: Using YQL Sensibly - YUIConf 2010

Both problems solved and released as an API - in JS!

Page 113: Using YQL Sensibly - YUIConf 2010

In summaryUse YQL instead of wasting time reading API docs for a simple taskFilter data in the service and get the info back in formats you need.Use the fast YQL server instead of doing lots of requests.Write your own JS APIs using execute.Use local storage and don’t break caching.Go and use the web.Go easy on effects.