<?php
require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");

//CapitolWords API Doc = http://capitolwords.org/api/
//SunlightLabs API Doc = http://wiki.sunlightlabs.com/index.php/Legislators.get%28List%29

define("SUNLIGHT_API_KEY","a912a8d4a1e8be1c5fe3af83a28459b0");
define("SUNLIGHT_BASE_URL","http://services.sunlightlabs.com/api/legislators.get.json");
define("CAPITOLWORDS_BASE_URL","http://capitolwords.org/api/lawmaker/");

class CapitolWords{
	private $id; //CapitolWords ID (from Sunlight Foundation query) for the given legislator (resulting from gov_track_ID lookup)
	public $words;

	function __construct($gov_track_ID,$limit){
		$gov_track_ID = (int)$gov_track_ID;
		$limit = (int)$limit;
		
		//Sunlight Labs API connect
		//api.method.format?apikey=YOUR_API_KEY&params
		$api_call_url = SUNLIGHT_BASE_URL."?govtrack_id=".$gov_track_ID."&apikey=".SUNLIGHT_API_KEY;			
		$data = json_decode(curl_get_file_contents($api_call_url));
		$this->id = $data->response->legislator->bioguide_id; //Get the ID that CapitolWords uses
		
		//Use ID to query CapitolWords.org
		//http://capitolwords.org/api/lawmaker/<lawmaker_id>/latest/top<maxrows>.<format>
		$api_call_url = CAPITOLWORDS_BASE_URL.$this->id."/".date("Y")."/top".$limit.".json";		
		$data = json_decode(curl_get_file_contents($api_call_url));

		foreach($data as $row){ //Format for handling scripts as assoc array
			$this->words[$row->word] = $row->word_count;
		}
	}
}

?>