Skip to content

Instantly share code, notes, and snippets.

@oboenikui
Forked from jetkiwi/RainCommand.java
Last active August 29, 2015 14:02
Show Gist options
  • Save oboenikui/993ec4a4201032f97605 to your computer and use it in GitHub Desktop.
Save oboenikui/993ec4a4201032f97605 to your computer and use it in GitHub Desktop.
package net.ja731j.twitter.autoreply.command;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import twitter4j.JSONArray;
import twitter4j.JSONException;
import twitter4j.JSONObject;
public class RainCommand {
private final String appId = "";
private final Pattern updatePattern = Pattern.compile("^@ja731j rain .{1,30}$");
private final Pattern removePattern = Pattern.compile("@ja731j rain ");
public static void main(String[] args) throws URISyntaxException, IOException, NumberFormatException, JSONException {
RainCommand command = new RainCommand();
Map<String, String> map = command.getCoord("大阪");
System.out.println(command.getRain(map.get("coord")));
}
private Map<String, String> getCoord(String str) throws URISyntaxException, IOException, HttpResponseException, JSONException {
URIBuilder geoApi = new URIBuilder("http://geo.search.olp.yahooapis.jp/OpenLocalPlatform/V1/geoCoder");
geoApi.addParameter("appid", appId);
geoApi.addParameter("query", str);
geoApi.addParameter("output", "json");
HttpGet httpGet = new HttpGet(geoApi.build());
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() != 200) {
throw new HttpResponseException(response.getStatusLine().getStatusCode(), "Unacceptable status code");
}
Map<String, String> result = new HashMap<String, String>();
HttpEntity entity = response.getEntity();
String str1 = EntityUtils.toString(entity, "Shift-JIS");
JSONObject obj = new JSONObject(str1);
JSONArray array = obj.getJSONArray("Feature");
String targetName = null;
String targetCoord = null;
for (int i=0;i<array.length();i++) {
if (array.get(i) instanceof JSONObject) {
JSONObject geo = array.getJSONObject(i).getJSONObject("Geometry");
String type = geo.getString("Type");
if (type.equals("point")) {
targetName = array.getJSONObject(i).getString("Name");
targetCoord = geo.getString("Coordinates");
break;
}
}
}
result.put("name", targetName);
result.put("coord", targetCoord);
return result;
}
public List<Double> getRain(String str) throws URISyntaxException, IOException, NumberFormatException, JSONException {
URIBuilder rainApi = new URIBuilder("http://weather.olp.yahooapis.jp/v1/place");
rainApi.addParameter("appid", appId);
rainApi.addParameter("coordinates", str);
rainApi.addParameter("output", "json");
HttpGet httpGet = new HttpGet(rainApi.build());
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() != 200) {
throw new HttpResponseException(response.getStatusLine().getStatusCode(), "Unacceptable status code");
}
List<Double> result = new ArrayList<>();
HttpEntity entity = response.getEntity();
String str1 = EntityUtils.toString(entity, "Shift-JIS");
System.out.println(str1);
JSONObject obj = new JSONObject(str1);
JSONObject feature = obj.getJSONArray("Feature").getJSONObject(0);
JSONObject prop = feature.getJSONObject("Property");
JSONArray weatherList = prop.getJSONObject("WeatherList").getJSONArray("Weather");
for (int i=0;i<weatherList.length();i++) {
if (weatherList.get(i) instanceof JSONObject) {
JSONObject weather = weatherList.getJSONObject(i);
Double rain = Double.parseDouble(weather.getString("Rainfall"));
result.add(rain);
}
}
return result;
}
private static String convertStreamToString(java.io.InputStream is) {
Scanner s = new Scanner(is);
s.useDelimiter("\\A");
try{
return s.hasNext() ? s.next() : "";
}finally{
s.close();
}
}
private enum Decision {
NOT_RAINING, NO_DIFFERENCE, GO, WAIT
}
private Decision decide(List<Double> rains) {
double sum = 0;
for (Double rain : rains) {
sum += rain;
}
double average = sum / rains.size();
if (average == 0) {
return Decision.NOT_RAINING;
} else {
double relative = rains.get(0) / average;
if (relative < 0.8) {
return Decision.GO;
} else if (relative > 1.2) {
return Decision.WAIT;
} else {
return Decision.NO_DIFFERENCE;
}
}
}
private String getAdviceText(Decision d) {
switch (d) {
case NOT_RAINING:
return "降っていないのでいつ行っても大丈夫です。";
case NO_DIFFERENCE:
return "いつ行っても同じです。";
case GO:
return "今行ったほうがいいです。";
case WAIT:
return "少し待ってから行ったほうがいいです。";
default:
return "ごめんなさい。バグりました。";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment