-
-
Save ffr4nz/ff1c3dae65e4c35f3e78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import org.json.JSONObject; | |
import org.json.JSONArray; | |
public class RIPEGeolocation extends BaseSinfonierBolt { | |
private String field; | |
public RIPEGeolocation(String xmlFile) { | |
super(xmlFile); | |
} | |
@Override | |
public void userprepare() { | |
this.field = (String)this.getParam("queryfield"); | |
} | |
@Override | |
public void userexecute() { | |
try { | |
String ip = (String)this.getField(this.field); | |
String address = "https://stat.ripe.net/data/geoloc/data.json?resource="+ip; | |
URL url = new URL(address); | |
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.connect(); | |
int responseCode = connection.getResponseCode(); | |
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
String inputLine; | |
String json = ""; | |
while ((inputLine = in.readLine()) != null) | |
json += inputLine; | |
in.close(); | |
JSONObject jobj = new JSONObject(json); | |
JSONArray jarr = jobj.getJSONObject("data").getJSONArray("locations"); | |
String lat = jarr.getJSONObject(0).get("latitude").toString(); | |
String lon = jarr.getJSONObject(0).get("longitude").toString(); | |
this.addField("latitude",lat); | |
this.addField("longitude",lon); | |
} catch(Exception ex){ | |
this.addField("latitude","0.0"); | |
this.addField("longitude","0.0"); | |
} | |
this.emit(); | |
} | |
public void usercleanup() { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment