Thursday Quote: Kenneth Reitz
“The API is all that matters. Everything else is secondary.”
- Kenneth Reitz
Python for Humans
“The API is all that matters. Everything else is secondary.”
- Kenneth Reitz
Python for Humans
I started trying to lose weight a while back, since we both know I’m a bit heavy and sitting in front of a computer isn’t going to lose the weight for me.
Naturally, it’s important that I incorporate technology into my weight loss somehow, right? So I decided to give the Google Visualizations API a spin.
I worked up a quick data format and a method to pop the data out. Nothing fancy, just a fixed width flat file. This doesn’t deserve a database.
1 2 | 2010-06-30 235.4 2010-06-29 236.8 |
Easy to read, easy to edit, and easy to consume. Every morning I just hop on the server, add the day’s weight and log off.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php // Get the max days back we want to look. $max_days = 7; if( isset( $_REQUEST['days'] ) and ! empty( $_REQUEST['days'] ) ) $max_days = intval( $_REQUEST['days'] ); $i = 0; $lines = array(); $fh = fopen( 'data.txt', 'r' ); while( ! feof( $fh ) and ++$i <= $max_days ) { $line = fgets( $fh ); if( empty( $line ) ) { continue; } array_unshift( $lines, $line ); } fclose( $fh ); |
Now I just needed to represent it. The API is very object oriented and easy to work with. I wish there was a less verbose way of presenting the data, but you can’t have everything.
Actually, there may be a better way, I just didn’t come across it while speed reading the docs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | function drawChart() { var data = new google.visualization.DataTable(); data.addColumn( 'string', 'Date' ); data.addColumn( 'number', 'Weight' ); data.addRows( <?php echo count( $lines ); ?> ); <?php $i = 0; foreach( $lines as $line ): ?> data.setValue( <?php echo $i; ?>, 0, '<?php echo substr( $line, 0, 10 ); ?>' ); data.setValue( <?php echo $i; ?>, 1, <?php echo floatval( substr( $line, 11 ) ); ?> ); <?php ++$i; endforeach; ?> var chart_div = document.getElementById( 'chart_div' ); var chart = new google.visualization.LineChart( chart_div ); chart.draw( data, { width: 800, height: 600, title: 'Weight Over Time' } ); } |
And there you have it, fancy charting in no time.
See it in action at http://static.velvetcache.org/weight.php
Get the full source at http://gist.github.com/459148.
I’ve been messing with the Foursquare API and I was getting frustrated by it’s unreadable output. Too much XML, not enough whitespace!
jmhobbs@katya:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697 <?xml version="1.0" encoding="UTF-8"?> <venues><group type="Nearby"><venue><id>170922</id><name>Peter Kiewit Institute</name><primarycategory><id>79037</id><fullpathname>College & Education:University</fullpathname><nodename>University</nodename><iconurl>http://foursquare.com/img/categories/education/default.png</iconurl></primarycategory><address>1110 South 67th Street</address><crossstreet>at Pacific St</crossstreet><city>Omaha</city><state>NE</state><zip>68182</zip><geolat>41.2436984</geolat><geolong>-96.0146702</geolong><stats><herenow>0</herenow></stats><phone>4025543333</phone><twitter>PKI</twitter><distance>255</distance></venue><venue><id>345883</id><name>Liv Lounge</name><primarycategory><id>79168</id><fullpathname>Nightlife:Lounge</fullpathname><nodename>Lounge</nodename><iconurl>http://foursquare.com/img/categories/nightlife/lounge.png</iconurl></primarycategory><address>2279 S 67th St</address><crossstreet>at Center St</crossstreet><city>Omaha</city><state>Nebraska</state><zip>68106</zip><geolat>41.2436984</geolat><geolong>-96.0146702</geolong><stats><herenow>0</herenow></stats><distance>255</distance></venue><venue><id>245578</id><name>Walmart</name><primarycategory><id>79221</id><fullpathname>Shops:Department Store</fullpathname><nodename>Department Store</nodename><iconurl>http://foursquare.com/img/categories/shops/default.png</iconurl></primarycategory><address>1606 S 72nd St</address><crossstreet>at Pine St</crossstreet><city>Omaha</city><state>NE</state><zip>68124</zip><geolat>41.2440158</geolat><geolong>-96.0241158</geolong><stats><herenow>0</herenow></stats><phone>4023939560</phone><distance>541</distance></venue><venue><id>254875</id><name>Aksarben Village</name><primarycategory><id>79196</id><fullpathname>Parks & Outdoors:Plaza / Square</fullpathname><nodename>Plaza / Square</nodename><iconurl>http://foursquare.com/img/categories/parks_outdoors/default.png</iconurl></primarycategory><address>6600 Shirley St</address><crossstreet>at Center St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.2414632</geolat><geolong>-96.0120519</geolong><stats><herenow>0</herenow></stats><distance>518</distance></venue><venue><id>170916</id><name>Scott Technology Center</name><primarycategory><id>79115</id><fullpathname>Home / Work / Other:Corporate / Office</fullpathname><nodename>Corporate / Office</nodename><iconurl>http://foursquare.com/img/categories/building/default.png</iconurl></primarycategory><address>6825 Pine Street</address><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.243385</geolat><geolong>-96.017697</geolong><stats><herenow>0</herenow></stats><phone>4025026000</phone><distance>0</distance></venue><venue><id>2468550</id><name>Juice Stop</name><primarycategory><id>79071</id><fullpathname>Food:Juice Bar</fullpathname><nodename>Juice Bar</nodename><iconurl>http://foursquare.com/img/categories/food/default.png</iconurl></primarycategory><address>2119 S 67th St</address><crossstreet>Shirley St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.2403021</geolat><geolong>-96.0146607</geolong><stats><herenow>0</herenow></stats><phone>4029343870</phone><distance>427</distance></venue><venue><id>199022</id><name>Scott Conference Center</name><primarycategory><id>96353</id><fullpathname>Home / Work / Other:Event Space</fullpathname><nodename>Event Space</nodename><iconurl>http://foursquare.com/img/categories/building/default.png</iconurl></primarycategory><address>6450 Pine Street</address><city>Omaha</city><state>NE</state><geolat>41.244514</geolat><geolong>-96.013489</geolong><stats><herenow>0</herenow></stats><distance>373</distance></venue><venue><id>772128</id><name>Courtyard Aksarben VIllage</name><primarycategory><id>79273</id><fullpathname>Travel:Hotel</fullpathname><nodename>Hotel</nodename><iconurl>http://foursquare.com/img/categories/travel/hotel.png</iconurl></primarycategory><address>1625 S 67th St</address><crossstreet>at Pine St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.243304</geolat><geolong>-96.014349</geolong><stats><herenow>0</herenow></stats><phone>4029514300</phone><distance>280</distance></venue><venue><id>247344</id><name>Wohlner's Market</name><primarycategory><id>96359</id><fullpathname>Shops:Food & Drink:Gourmet</fullpathname><nodename>Gourmet</nodename><iconurl>http://foursquare.com/img/categories/shops/default.png</iconurl></primarycategory><address>2289 S 67th St</address><crossstreet>at Center St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.23806024488222</geolat><geolong>-96.0138988494873</geolong><stats><herenow>0</herenow></stats><phone>4025516875</phone><distance>672</distance></venue><venue><id>2564306</id><name>First Data</name><primarycategory><id>79115</id><fullpathname>Home / Work / Other:Corporate / Office</fullpathname><nodename>Corporate / Office</nodename><iconurl>http://foursquare.com/img/categories/building/default.png</iconurl></primarycategory><address>6855 Pacific Street</address><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.2487029</geolat><geolong>-96.0192553</geolong><stats><herenow>0</herenow></stats><distance>606</distance></venue></group></venues> jmhobbs@katya:~$ |
How can we rectify this situation? xmllint! This is part of libxml and should exist on most systems.
jmhobbs@katya:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697 | xmllint --format -
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 5265 101 5265 0 0 13778 0 --:--:-- --:--:-- --:--:-- 29914
<?xml version="1.0" encoding="UTF-8"?>
<venues>
<group type="Nearby">
<venue>
<id>170922</id>
<name>Peter Kiewit Institute</name>
<primarycategory>
<id>79037</id>
<fullpathname>College & Education:University</fullpathname>
<nodename>University</nodename>
<iconurl>http://foursquare.com/img/categories/education/default.png</iconurl>
</primarycategory>
<address>1110 South 67th Street</address>
<crossstreet>at Pacific St</crossstreet>
<city>Omaha</city>
<state>NE</state>
<zip>68182</zip>
<geolat>41.2436984</geolat>
<geolong>-96.0146702</geolong>
<stats>
<herenow>0</herenow>
</stats>
<phone>4025543333</phone>
<twitter>PKI</twitter>
<distance>255</distance>
</venue>
... snip ...
<venue>
<id>2564306</id>
<name>First Data</name>
<primarycategory>
<id>79115</id>
<fullpathname>Home / Work / Other:Corporate / Office</fullpathname>
<nodename>Corporate / Office</nodename>
<iconurl>http://foursquare.com/img/categories/building/default.png</iconurl>
</primarycategory>
<address>6855 Pacific Street</address>
<city>Omaha</city>
<state>NE</state>
<zip>68106</zip>
<geolat>41.2487029</geolat>
<geolong>-96.0192553</geolong>
<stats>
<herenow>0</herenow>
</stats>
<distance>606</distance>
</venue>
</group>
</venues>
jmhobbs@katya:~$ |
So there you go, a handy trick of the day for you.
In case you missed it the short form is:
curl -l [url] | xmllint --format -