Wednesday, November 23, 2016

Yahoo Top News Stories using YQL and yahoo API

///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd" https="true">
    <meta>
        <author>Paul Donnelly</author>
        <documentationURL>http://developer.netflix.com/docs/REST_API_Reference#0_52696</documentationURL>
    </meta>
      <bindings>
        <select itemPath="" produces="XML" >
          <urls>
            <url>http://api-public.netflix.com/catalog/titles/</url>
          </urls>
         <paging model="offset">
           <start id="start_index" default="0" />
           <pagesize id="max_results" max="100" />
           <total default="20" />
         </paging>
          <inputs>
                <key id="term" type="xs:string" paramType="query" required="true" />
                <key id="ck" type="xs:string" paramType="variable" required="true" />
                <key id="cs" type="xs:string" paramType="variable" required="true" />
          </inputs>
          <execute><![CDATA[
// Include the OAuth libraries from oauth.net
y.include("http://oauth.googlecode.com/svn/code/javascript/oauth.js");
y.include("http://oauth.googlecode.com/svn/code/javascript/sha1.js");

// Collect all the parameters
var encodedurl = request.url;
var accessor = { consumerSecret: cs, tokenSecret: ""};
var message = { action: encodedurl, method: "GET", parameters: [["oauth_consumer_key",ck],["oauth_version","1.0"]]};
OAuth.setTimestampAndNonce(message);

// Sign the request
OAuth.SignatureMethod.sign(message, accessor);

try {
   // get the content from service along with the OAuth header, and return the result back out
   response.object = request.contentType('application/xml').header("Authorization", OAuth.getAuthorizationHeader("netflix.com", message.parameters)).get().response;
} catch(err) {
   response.object = {'result':'failure', 'error': err};
}
          ]]></execute>
        </select>
      </bindings>
</table>

<html>
  <head><title>YQL and RSS: Yahoo Top News Stories</title>      <style type='text/css'>
      #results{ width: 40%; margin-left: 30%; border: 1px solid gray; padding: 5px; height: 200px; overflow: auto; }
    </style>
    <script type='text/javascript'>
      function top_stories(o){
        var items = o.query.results.item;
        var output = '';
        var no_items=items.length;
        for(var i=0;i<no_items;i++){
          var title = items[i].title;
          var link = items[i].link;
          var desc = items[i].description;
          output += "<h3><a href='" + link + "'>"+title+"</a></h3>" + desc + "<hr/>";    
        }
        document.getElementById('results').innerHTML = output;
      }
    </script>
  </head>
  <body>
    <div id='results'></div>
    <script src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22&format=json&diagnostics=false&callback=top_stories'></script>
  </body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////





No comments:

Post a Comment