15
Fetch Statistics from the Yahoo Finance and save it into a Google Sheet document

Node

Embed Size (px)

Citation preview

Page 1: Node

Fetch Statistics from the Yahoo Finance and save it

into a Google Sheet document

Page 2: Node

• An Engineering Student at the International Institute of Technology

• Article Writer at CodeProject.com

About me

Page 3: Node

• A powerful platform to let you run JS on the server side• How? Uses Google’s V8 Engine• V8 is built in C• V8 is the fatest JS Engine on the planet• Great way to build modern web apps on both Client and

Server side!

What is Node.js?

Page 4: Node

Timeline

2009 2010 2011 2012

Jan 2009Created Ryan Dahl

April 2010 Heroku launches node support

Nov 2011 Windows Azure support

EBay releases API built on node

Cloud9IDE azuresupport

Oct 2011 node.js overtakes Ruby as most popular repo on gitHub

Walmart Launch mobile site on node.js

Feb 2012 App Harbour support

July 2011

LinkedIn adopts node for mobile platform

port to Windows

IISNode

July 2010 Yammer adopts node.js

Nov 2010 Cloud9IDE launches

Page 5: Node

Companies support Node.js

• Yahoo!• Microsoft • LinkedIn• Ebay• Fidelizoo• Paypal• CHESS• …

Page 6: Node

What Can I Do in Node?

• Anything you want!• Chat servers, Analytics & Crazy fast backends• Socket.io library is a wicked way to build real time apps• Build a social Network! LinkedIn, Dropbox all using Node.js

Page 7: Node

What Can’t I Do in Node?

• Contradicts previous slide but Node.js is not a web framework i.e Sinatra Modules for node.js make it into web framework i.e Express Node.js is not Multi-threaded A single thread to rule them all

Page 8: Node

Non-Blocking? Blocking? I’m so confused

• By introducing callbacks. Node can move on to other requests and whenever the callback is called, node will process is.

• You should read non-blocking code as « put function and params in queue and fire callback when you reach the end of the queue »

• Blocking= return Non-Blocking= no return. Only callbacks

Page 9: Node

Fetch Statistics from the Yahoo Finance

• We have to fetch data from the link shown below:

http://finance.yahoo.com/q/ks?s=LVS

The Company in our example is LVS.

Page 10: Node

• You need to install Node.js Of course and it 3 packages: npm install request – So we could work with URLs in an easy

way. npm install cheerio – jQuery for the server side. This will make

the code 10x shorter. npm install edit-google-spreadsheet – to integrate with

Google docs/sheets with 2-3 lines of code.

Fetch Statistics from the Yahoo Finance

Page 11: Node

Fetch Statistics from the Yahoo Finance

• // Some parameters • var ticker = "LVS";• var yUrl = "http://finance.yahoo.com/q/ks?s=" + ticker;• var financeDetails = new Array();• var keyStr = new Array();

Page 12: Node

• // The main call to fetch the data, parse it and work on it.

• request(yUrl, function (error, response, body) {• if (!error && response.statusCode == 200) {• var $ = cheerio.load(body);• // the keys - We get them from a certain class attribute• var td = $('.yfnc_tablehead1');• $(td).each(function(j, val) {• keyStr[j] = $(val).text();• });

Fetch Statistics from the Yahoo Finance

Page 13: Node

Working with Google Sheet Document

• // upload our data to Google sheet• Spreadsheet.create({• debug: true,• username: 'TODO-fill',• password: 'TODO-fill',• debug: true,• spreadsheetName: 'TODO-yourSheetName',• worksheetName: 'TODO-Sheet1orAbetterName',• callback: sheetReady• });

Page 14: Node

Demo

Page 15: Node

Thank you

[email protected]