Created
January 20, 2022 18:47
-
-
Save ffr4nz/d06dab6cd3f890b41582a31ba575fdd9 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
package bolts; | |
import backtype.storm.task.OutputCollector; | |
import backtype.storm.task.TopologyContext; | |
import backtype.storm.topology.OutputFieldsDeclarer; | |
import backtype.storm.topology.base.BaseRichBolt; | |
import backtype.storm.tuple.Fields; | |
import backtype.storm.tuple.Tuple; | |
import backtype.storm.tuple.Values; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Deweet extends BaseRichBolt{ | |
private OutputCollector _collector; | |
public static String getHTML(String urlToRead) throws Exception { | |
StringBuilder result = new StringBuilder(); | |
URL url = new URL(urlToRead); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String line; | |
while ((line = rd.readLine()) != null) { | |
result.append(line); | |
} | |
rd.close(); | |
return result.toString(); | |
} | |
@Override | |
public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) { | |
_collector = outputCollector; | |
} | |
@Override | |
public void execute(Tuple tuple) { | |
Long lat = tuple.getLongByField("lat"); | |
Long lon = tuple.getLongByField("lon"); | |
String msg = tuple.getStringByField("entity"); | |
try { | |
String res = getHTML("https://dweet.io/dweet/for/eoi2022?lat="+lat.toString()+"&lon="+lon.toString()+"&word="+ URLEncoder.encode(msg,"utf-8")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
_collector.ack(tuple); | |
} | |
@Override | |
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment