Skip to content

Instantly share code, notes, and snippets.

@Miguel000
Last active February 13, 2016 12:43
Show Gist options
  • Save Miguel000/dbd50e2719009fb1012b to your computer and use it in GitHub Desktop.
Save Miguel000/dbd50e2719009fb1012b to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2014 sinfonier-project
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.sinfonier.bolts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.Map;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class TrafficAccidentsZgz extends BaseSinfonierBolt {
private int rows;
private String sort;
private String q;
private String distance;
private String lat;
private String longi;
public TrafficAccidentsZgz (String xmlFile) {
super(xmlFile);
}
@Override
public void userprepare() {
this.q = this.getParam("q");
this.rows = Integer.parseInt(getParam("rows",true));
this.sort = this.getParam("sort");
this.lat = this.getParam("lat");
this.longi = this.getParam("longi");
this.distance = this.getParam("distance");
}
@Override
public void userexecute() {
try{
String latitude = (String) this.getField(this.lat);
String longitude = (String) this.getField(this.longi);
String point = longitude + "," + latitude;
String urlString = "http://www.zaragoza.es/api/recurso/urbanismo-infraestructuras/incidencia/accidente.json?q="+q+"&sort="+URLEncoder.encode(sort, "UTF-8")+"&rows="+rows+"&point="+point+"&distance="+distance+"&srsname=wgs84";
URL url = new URL(urlString);
InputStream is = url.openStream();
int ptr = 0;
StringBuilder builder = new StringBuilder();
while ((ptr = is.read()) != -1) {
builder.append((char) ptr);
}
String jsonString = builder.toString();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> json = new HashMap<String, Object>();
json = mapper.readValue(jsonString,new TypeReference<Map<String, Object>>() {});
this.addField("InfoAccident", json);
} catch (Exception e) {
this.addField("InfoAccident-error",e.toString());
}
this.emit();
}
public void usercleanup() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment