<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>yken.org &#187; bash</title>
	<atom:link href="http://yken.org/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://yken.org</link>
	<description>...it depends</description>
	<lastBuildDate>Sun, 07 Mar 2010 17:47:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to get a real-time stock quote using Google API</title>
		<link>http://yken.org/2009/01/05/how-to-get-a-real-time-stock-quote-using-google-api/</link>
		<comments>http://yken.org/2009/01/05/how-to-get-a-real-time-stock-quote-using-google-api/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 22:39:28 +0000</pubDate>
		<dc:creator>ikendra</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[stocks]]></category>

		<guid isPermaLink="false">http://yken.org/2009/01/05/how-to-get-a-real-time-stock-quote-using-google-api/</guid>
		<description><![CDATA[After resolving the problem with authentication, I continued playing around with Google Finance API, using HTTP and XML. The API offers some nice functionality to retrieve user portfolio content, but it doesn&#8217;t take long to realize that there is no support for retrieving (real-time) stock quotes, probably for some good reason like licensing. But then, [...]]]></description>
			<content:encoded><![CDATA[<p>After resolving <a href="http://yken.org/2008/12/29/troubleshooting-401-with-googlelogin-authorization-header/">the problem with authentication</a>, I continued playing around with <a href="http://code.google.com/apis/finance/developers_guide_protocol.html">Google Finance API</a>, using HTTP and XML. The API offers some nice functionality to retrieve user portfolio content, but it doesn&#8217;t take long to realize that there is no support for retrieving (real-time) stock quotes, probably for some good reason like licensing. But then, there is a way of getting real-time stock quotes on your Google Spreadsheet using the GoogleFinance() formula. Can this fact get us closer to retrieving real-time stock quotes using Google Data API?</p>
<p><span id="more-71"></span><br />
Yes it can. It is relatively straightforward to write a simple program to retrieve real-time stock quote using just <a href="http://code.google.com/apis/spreadsheets/docs/2.0/developers_guide_protocol.html">Google Spreadsheet Data API</a>. Below is a simple proof-of-concept script which does the job. To make it work, create a new Spreadsheet on Google Docs and int the cell A1 fill in a formula for your favorite stock quote, something like:</p>
<pre>
=GoogleFinance("GOOG")
</pre>
<p>You will see the price in your spreadsheet:</p>
<p><img src="http://yken.org/wp-content/firefox-google-spreadsheet-sample.jpg" alt="Firefox Google Spreadsheet Sample" /></p>
<p>The script to retrieve the quote:</p>
<pre>
#!/bin/bash

wget -O ClientLogin.txt --no-check-certificate --post-file=post.txt \
"https://www.google.com/accounts/ClientLogin" &gt;/dev/null 2&gt;&amp;1

TOKEN=`cat ClientLogin.txt | grep Auth \
| sed "s#Auth=##" | xargs echo -n`

wget -O Spreadsheets.txt --header="Authorization: GoogleLogin auth=${TOKEN}" \
--header="GData-Version: 2" \
http://spreadsheets.google.com/feeds/spreadsheets/private/full &gt;/dev/null 2&gt;&amp;1

WORKSHEET=`cat Spreadsheets.txt | \
sed "s#\(.*\)src=\([']*\)\([^']*\)\([']\)\(.*\)#\3#"`

wget -O Worksheet.txt --header="Authorization: GoogleLogin auth=${TOKEN}" \
--header="GData-Version: 2" ${WORKSHEET} &gt;/dev/null 2&gt;&amp;1

cat Worksheet.txt | sed 's#\&gt;#\&gt;\
#g' &gt;WorksheetFormatted.txt

CELLSFEED=`cat WorksheetFormatted.txt | grep cellsfeed | \
sed "s#\(.*\)href=\([']*\)\([^']*\)\([']\)\(.*\)#\3#"`

wget -O Cells.txt --header="Authorization: GoogleLogin auth=${TOKEN}" \
--header="GData-Version: 2" ${CELLSFEED} &gt;/dev/null 2&gt;&amp;1

cat Cells.txt | sed 's#\&gt;#\&gt;\
#g' &gt;CellsFormatted.txt

REALTIMEQUOTE=`cat CellsFormatted.txt | grep numericValue | \
sed "s#\(.*\)numericValue=\([']*\)\([^']*\)\([']\)\(.*\)#\3#"`

echo "Quote = ${REALTIMEQUOTE}"
</pre>
<p>where post.txt is something like:</p>
<pre>
POST /accounts/ClientLogin HTTP/1.0
Content-type: application/x-www-form-urlencoded

accountType=HOSTED_OR_GOOGLE&amp;Email=__EMAIL__&amp;Passwd=__PASSWD__
&amp;service=finance&amp;source=yken.org-GoogleStockQuote-0.1
</pre>
<p><!--more--><br />
A few notes on the script:</p>
<ul>
<li>Replace __EMAIL__ and __PASSWD__ in post.txt with your Google credentials</li>
<li>This will only work when you have just one spreadsheet on your Google Docs</li>
<li>The script does not parse the XML replies, it just gets the values it needs using brute force (as it is just a proof of concept).</li>
<li>Three requests (four with login) just to get a quote is definitely over the top. The Google API allows batch operations, too.</li>
<li>I tried this on Max OS X 10.5 only</li>
</ul>
<p><!--more--><br />
To make the script actually useful, it would be good to modify it to take some parameters, for instance at least the desired stock symbol and then to use that stock symbol to write the =GoogleFinance() formula to A1. Google Data API allows it. This way, the script could be used for retrieving any quote (available on Google Finance), not only just the hardcoded one. Also, there are many <a href="http://code.google.com/apis/gdata/clientlibs.html">Google Data API Client Libraries</a> available, allowing to rewrite this script into something proper. I will try both and will post the result here.</p>
<p>Important note: all stock data obtained by the above script can be used for personal informational purposes only, see the <a href="http://www.google.com/intl/en/help/stock_disclaimer.html">Google disclaimer</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://yken.org/2009/01/05/how-to-get-a-real-time-stock-quote-using-google-api/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Troubleshooting 401 with GoogleLogin Authorization header</title>
		<link>http://yken.org/2008/12/29/troubleshooting-401-with-googlelogin-authorization-header/</link>
		<comments>http://yken.org/2008/12/29/troubleshooting-401-with-googlelogin-authorization-header/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 13:54:32 +0000</pubDate>
		<dc:creator>ikendra</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://yken.org/2008/12/29/troubleshooting-401-with-googlelogin-authorization-header/</guid>
		<description><![CDATA[I was writing a small script to retrieve some data from Google Finance using the relevant Google Data API. I&#8217;m writing a stand-alone, desktop application and have therefore used the ClientLogin username and password authentication. The login worked fine, I was receiving HTTP status 200 and the appropriate response with the authentication token. Yet any [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a small script to retrieve some data from <a href="http://finance.google.com/">Google Finance</a> using the relevant Google Data API. I&#8217;m writing a stand-alone, desktop application and have therefore used the ClientLogin username and password authentication. The login worked fine, I was receiving HTTP status 200 and the appropriate response with the authentication token. Yet any subsequent attempt to use the Data API (to insance to retrieve the portfolio data) failed with HTTP 401 Token Invalid error.</p>
<p><span id="more-70"></span><br />
It turned out hat the cause of getting the 401 error was in the way I set up the Authorization header sent with my requests. Unlike with AuthSub Authorization header used for web applications, the token value for ClientLogin authentication must not be enclosed in (double-)quotes. This is a mistake easy to make when switching from AuthSub to ClientLogin, resulting in HTTP status 401 reply for any Data API request. Remove the quotes around the token and the Data API request will work fine.<br />
<!--more--><br />
A small sample:</p>
<pre>
#!/bin/bash

if [ -e ClientLogin ]
then
&nbsp;&nbsp;rm ClientLogin
fi

wget --no-check-certificate --post-file=post.txt \
https://www.google.com/accounts/ClientLogin

TOKEN=`cat ClientLogin | grep Auth | \
sed "s#Auth=##" | xargs echo -n`

wget --header="Authorization: GoogleLogin auth=${TOKEN}" \
http://finance.google.com/finance/feeds/default/portfolios</span>
</pre>
<p>where post.txt is something like:</p>
<pre>
POST /accounts/ClientLogin HTTP/1.0
Content-type: application/x-www-form-urlencoded

accountType=HOSTED_OR_GOOGLE&amp;Email=__EMAIL__&amp;Passwd=__PASSWD__
&amp;service=finance&amp;source=yken.org-learningGoogleAPI-0.1</span>
</pre>
<p>Replace __EMAIL__ and __PASSWD__ with appropriate value. When the script is finished, you will have the relevant portfolio information saved in the &#8220;portfolio&#8221; file.</p>
]]></content:encoded>
			<wfw:commentRss>http://yken.org/2008/12/29/troubleshooting-401-with-googlelogin-authorization-header/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
