Using Yahoo Query Language in Liferay(YQL)
Yahoo Query Language (YQL)
The
Yahoo Query Language is an expressive SQL-like language that lets you query,
filter, and join data across Web services. With YQL, apps run faster with fewer
lines of code and a smaller network footprint.
Download
Liferay YQL portlet from following location
You can
find source and war file
Note: Portlet developed in Liferay 6.1GA2 EE
version
If you
want deploy in CE version you just do changes in liferay-plugin-package.properties
Liferay 6.1 EE version
name=LiferayYQL
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay,
Inc.
licenses=EE
liferay-versions=6.1.20
|
Liferay 6.1 CE version
name LiferayYQL
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay,
Inc.
licenses=LGPL
liferay-versions=6.1.1
|
Understanding YQL:
Yql
provide syntax’s like sql so that we can fetch data from yahoo web services.
We already
know about web service like SOAP and REST but YQL is very simple to use in web
sites.
This is
very special language to yahoo to fetch yahoo data .
But we
can also provide our data available to YQL using data tables.
The following scenarios we can
use YQL
Fetch Weather Information
Fetch Stock Market Information Up to date
Currency conversion
Fetch yahoo news
How to use YQL:
Open
Yahoo console from fowling URL
Select
any tables which are left side
Select
any tables it will populate sample query in console text box
You can
click on Test button and see the result.
Here YQL
provide to kinds of data JSON and XML before click on test button you can
select any of formats then data will be showed in result part.
Once the
result is desirable then copy the URL which showed in bottom this URL can be
used to get content.
Once get
content you can display into your website
Example:
Lets us
assume we want display weather information in our web site:
Go to
Yahoo console
Execute
following YQL query
select *
from weather.forecast where location=90210
Select
JSON and Test
In the
result you can see the data related to weather you can identify json or xml structure
and retrieve in website.
We can use YQL in different ways:
I
suggest you use two ways
YUI YQL java script library
Simple jquery AJAX
YUI YQL java script library:
To use
YQL in your application first we need following YQL and YUI library
<script
src="http://yui.yahooapis.com/3.13.0/build/yui/yui-min.js"></script>
And use
YUI use method and add required modules we need to add yql module when we work
with YQL
The following is complete code
<script src="http://yui.yahooapis.com/3.13.0/build/yui/yui-min.js"></script>
<script>
YUI().use('node','yql',
function(Y) {
var res = Y.one('#res');
var woeid = '2502265';
Y.YQL('select * from weather.forecast
where woeid='+woeid, function(r) {
var el =
Y.Node.create('<div class="mod"></div>');
el.set('innerHTML',
'<h2>Weather for ' + zip + '</h2>' +
r.query.results.channel.item.description);
res.set('innerHTML',r.query.results.channel.item.description);
});
});
</script>
<div
id="res">Weather Info Here</div>
|
Simple jquery AJAX:
We can
also used jquey Ajax to execute YQL queries
Generally
All YQL queries requested by following URL
We need
to pass following parameters to above URL
q : the
YQL query which you desire
format: type
of data xml or json
env: env is type of parameter which specified
where data table are available
For more info we
need read about data tables
diagnostics: is Boolean value it will give error information in response
if anything fails
callback: is optional parameter
|
The following is example:
http://query.yahooapis.com/v1/public/yql?q=select
* from weather.forecast where
woeid=2502265&format=json&diagnostics=true&env=http://datatables.org/alltables.env&callback=
Note:
When we execute
any YQL statement in YQL console for each statement we will gate URL in the
bottom
If see
URL you can identify url and its query string but here all parameters are
encoded format.
For know
what parameters using you yql statement you can decode URL. The following is
URL decoding in online.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script
type="text/javascript">
$(document).on('ready',function(){
var weatherQuery='select * from
weather.forecast where woeid=2502265’
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
dataType:"json",
data: {
q:weatherQuery,
format:"json",
diagnostics:"true",
env:"http://datatables.org/alltables.env",//env:"store://datatables.org/alltableswithkeys",
callback:""
},
type: "get",
success: function(r){
$('#res').html(r.query.results.channel.item.description);
},
beforeSend: function(){
},
complete:
function(){
},
error: function(){
}
});
});
</script>
<div
id="res">Weather Info Here</div>
|
YQL can
fetch against yahoo web services
The
following are some information about data tables
You can see all tables and
execute query in YQL console and find the data structure and use
Get Stocks quotes information:
Table:
yahoo.finance.quotes
Query:
select * from yahoo.finance.quotes where symbol in
("YHOO","AAPL","GOOG","MSFT")
Get Stock Market Indexes
Table:
yahoo.finance.quoteslist/ yahoo.finance.quotes
Query: select
* from yahoo.finance.quotes where symbol in
("^hsi","^bsesn","^nsei","^dji","^ixic")
Get Top Stories from news table:
Table:
yahoo feed
Query:
select * from feed where url='http://news.yahoo.com/rss/topstories'
Get Weather Organization ID(Weoid) from Longitude and Latitude
Table:
geo.placefinder
Query:
select * from geo.placefinder where text="22.2500,114.1667" and
gflags="R"
For more
info about gflags: http://developer.yahoo.com/boss/geo/docs/control-parameters.html
Get Weather Organization ID(Weoid) from Address
Table:
geo.placefinder
Query: select
* from geo.placefinder where text='Central,Honk Kong' and gflags='R'
WE can also fetch html from
websites using YQL
If some
time we did not find any web services simple from web page you can fetch data
and you can filter the content.
Here in
where condition we need to pass xpath
notation
Fetch
some use full data from Yahoo finance page like all Stock indexes
select *
from html where url="http://finance.yahoo.com/" and
xpath='//div[@class="desc"]'
select *
from html where url="http://finance.yahoo.com/q?s=yhoo" and
xpath='//div[@id="yfi_headlines"]/div[2]/ul/li/a'
Note:
We can
open page and identify the required content from page using Chrome inspect
element option.
Find the
xpath of content and apply to YQL.
Important Points:
- YQL is like sql statements to fetch content from yahoo web services.
- These all information they provided as data tables. So that we can use YQL against data tables to fetch the content.
- YQL provide xml and JSON formats.
- Each YQL statement can be provided as web URL so that we can directly execute in browser and see the result.
- To fetch the content from data table yahoo provides one URL and we need to use that public URL and pass some query parameter to get result. By this we can send Ajax calls.
- Yahoo provide special module YQL module from their java script library i.e. YUI.
- We can use YUI yql library to get content from data tables.
- YQL very convenient way to use in web site because we can use this with java script libraries like jquery,YUI or other java script libraries.
- YQL also provide way to fetch html content for web page as json or xml format so that we can use this data in website this scenarios happen when we don’t have web services.
Reference Links
YQL tutorial
YQL console
YQL complete tutorial
Data Tables
Xpath tutorial:
Online URL encodes and decodes:
Author: Meera Prince
0 comments :
Post a Comment