Skip to content

Instantly share code, notes, and snippets.

@chrismetcalf
Created November 5, 2011 19:39
Show Gist options
  • Save chrismetcalf/1341919 to your computer and use it in GitHub Desktop.
Save chrismetcalf/1341919 to your computer and use it in GitHub Desktop.
Weather Client
package com.socrata.demo;
import java.io.IOException;
import java.io.StringReader;
import com.socrata.api.Connection;
import com.socrata.api.HttpConnection;
import com.socrata.api.RequestException;
import com.socrata.data.View;
import com.socrata.data.View.Row;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Weather {
/**
* @param args
* @throws RequestException
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static void main(String[] args) throws RequestException, ParserConfigurationException, SAXException, IOException {
// Connect to the Google weather API and retrieve the current weather
Client client = Client.create();
WebResource weather = client.resource("http://www.google.com/ig/api");
ClientResponse response = weather.queryParam("weather", "Columbia, MD").get(ClientResponse.class);
if(response.getStatus() != 200) {
System.err.println("Error calling Google Weather: " + response.getEntity(String.class));
}
// Parse it...
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(response.getEntity(String.class)));
Document doc = db.parse(src);
// Pull out the bits we care about
Element currentConditions = (Element)doc.getElementsByTagName("current_conditions").item(0);
String description = ((Element)currentConditions.getElementsByTagName("condition").item(0)).getAttribute("data");
Float temp = Float.parseFloat(((Element)currentConditions.getElementsByTagName("temp_f").item(0)).getAttribute("data"));
String wind = ((Element)currentConditions.getElementsByTagName("wind_condition").item(0)).getAttribute("data");
System.out.println("Conditions: " + description);
System.out.println("Temperature: " + temp);
System.out.println("Wind: " + wind);
// Connect to the Socrata API
Connection c = new HttpConnection("saxon.socrata.com", "foo", "bar", "quo");
View v = View.find("wfpb-dq6h", c);
// Create a new row
Row r = new Row();
int now = (int)(System.currentTimeMillis() / 1000);
r.putDataField(v.getColumnById(3416927), now);
r.putDataField(v.getColumnById(3416928), description);
r.putDataField(v.getColumnById(3416929), temp);
r.putDataField(v.getColumnById(3416930), wind);
System.out.println("Will send: " + r.getDataFieldsForSerialization());
// Append the new row
r = v.appendRow(r, c);
System.out.println("Appended new row successfully: " + r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment