package com.cloudbreakers.streamer.stackexchange;
import com.cloudbreakers.producers.SoProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
/**
* Created by kalasd on 8/1/2014.
*/
public class StackExchangeStreamer implements Runnable
{
private final static Logger LOGGER = LoggerFactory.getLogger(StackExchangeStreamer.class);
protected final static String apiPrefix = "http://api.stackexchange.com/2.2/";
//private final static String usersQuery = apiPrefix + "/users/%1?order=desc&sort=reputation&site=stackoverflow&filter=!)scV1RYEKGuXeL9cm6.1";
private final static String queryForAll = apiPrefix + "questions?page=1&&key=jn2TZlYFl61NL)QGX6JL3Q((&pagesize=1&order=desc&sort=activity&site=stackoverflow&filter=!LUcFBSNzkjBg)6FmOlIocX";
private final static int connect_timeout = 30000;
private final static int read_timeout = 30000;
private URL url;
private long begin;
private long end;
public URL getUrl() {
return url;
}
protected void setUrl(URL url) {
this.url = url;
}
public StackExchangeStreamer(long beginTimestamp, long endTimestamp)
throws MalformedURLException {
setUrl( new URL(queryForAll));
begin = beginTimestamp;
end = endTimestamp;
}
public void run() {
SoProducer producer = new SoProducer();
HttpURLConnection request = null;
try
{
request = (HttpURLConnection) url
.openConnection();
request.setConnectTimeout(connect_timeout);
request.setReadTimeout(read_timeout);
request.connect();
if (request.getResponseCode() == 200)
{
StringBuilder sb = new StringBuilder();
String line;
try (InputStream is = request.getInputStream();
GZIPInputStream gzip = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzip);
BufferedReader br = new BufferedReader(isr))
{
while ((line = br.readLine()) != null) {
sb.append(line);
}
String answer = sb.toString();
System.out.println(Long.toString(Thread.currentThread().getId()));
answer = postProcessing(answer);
producer.send("car", answer);
//System.out.println(answer);
}
} else {
LOGGER.warn("Response cde is not 200. RC: " +
Integer.toString(request.getResponseCode()));
}
}
catch (IOException e)
{
LOGGER.error("error while fetching data",e);
} finally {
if (request != null) {
request.disconnect();
}
producer.close();
}
}
public String postProcessing(String ans)
{
return "{ \"body_ans\" : " + ans + ", " +
"\"start_of_stream\" : " + begin + ", \"end_of_stream\" : " +
end + "}";
}
}