Tag: API

Thursday Quote: Kenneth Reitz

February 16, 2012 » Geek

“The API is all that matters. Everything else is secondary.”

– Kenneth Reitz
Python for Humans

Tags: , , ,

Charting Weight Change With Google Visualizations

July 5, 2010 » Consume, Geek, Life

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.

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.


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.

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn( 'string', 'Date' );
  data.addColumn( 'number', 'Weight' );

  data.addRows(  );
        
  
  data.setValue( 
    , 
    0,
    ''
  );

  data.setValue(
    ,
    1,
    
  );
  
    
  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.

Example Chart

See it in action at http://static.velvetcache.org/weight.php

Get the full source at http://gist.github.com/459148.

Use xmllint to Pretty Print API Output

June 10, 2010 » Geek

I’ve been messing with the Foursquare API and I was getting frustrated by it’s unreadable output. Too much XML, not enough whitespace!

[email protected]:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697

170922Peter Kiewit Institute79037College & Education:UniversityUniversityhttp://foursquare.com/img/categories/education/default.png
1110 South 67th Street
at Pacific StOmahaNE6818241.2436984-96.014670204025543333PKI255
345883Liv Lounge79168Nightlife:LoungeLoungehttp://foursquare.com/img/categories/nightlife/lounge.png
2279 S 67th St
at Center StOmahaNebraska6810641.2436984-96.01467020255
245578Walmart79221Shops:Department StoreDepartment Storehttp://foursquare.com/img/categories/shops/default.png
1606 S 72nd St
at Pine StOmahaNE6812441.2440158-96.024115804023939560541
254875Aksarben Village79196Parks & Outdoors:Plaza / SquarePlaza / Squarehttp://foursquare.com/img/categories/parks_outdoors/default.png
6600 Shirley St
at Center StOmahaNE6810641.2414632-96.01205190518
170916Scott Technology Center79115Home / Work / Other:Corporate / OfficeCorporate / Officehttp://foursquare.com/img/categories/building/default.png
6825 Pine Street
OmahaNE6810641.243385-96.017697040250260000
2468550Juice Stop79071Food:Juice BarJuice Barhttp://foursquare.com/img/categories/food/default.png
2119 S 67th St
Shirley StOmahaNE6810641.2403021-96.014660704029343870427
199022Scott Conference Center96353Home / Work / Other:Event SpaceEvent Spacehttp://foursquare.com/img/categories/building/default.png
6450 Pine Street
OmahaNE41.244514-96.0134890373
772128Courtyard Aksarben VIllage79273Travel:HotelHotelhttp://foursquare.com/img/categories/travel/hotel.png
1625 S 67th St
at Pine StOmahaNE6810641.243304-96.01434904029514300280
247344Wohlner's Market96359Shops:Food & Drink:GourmetGourmethttp://foursquare.com/img/categories/shops/default.png
2289 S 67th St
at Center StOmahaNE6810641.23806024488222-96.013898849487304025516875672
2564306First Data79115Home / Work / Other:Corporate / OfficeCorporate / Officehttp://foursquare.com/img/categories/building/default.png
6855 Pacific Street
OmahaNE6810641.2487029-96.01925530606
[email protected]:~$

How can we rectify this situation? xmllint! This is part of libxml and should exist on most systems.

[email protected]:~$ 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


  
    
      170922
      Peter Kiewit Institute
      
        79037
        College & Education:University
        University
        http://foursquare.com/img/categories/education/default.png
      
      
1110 South 67th Street
at Pacific St Omaha NE 68182 41.2436984 -96.0146702 0 4025543333 PKI 255
... snip ... 2564306 First Data 79115 Home / Work / Other:Corporate / Office Corporate / Office http://foursquare.com/img/categories/building/default.png
6855 Pacific Street
Omaha NE 68106 41.2487029 -96.0192553 0 606
[email protected]:~$

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 -