12 05 2012

facebookの秘密グループに投稿されたウォール、コメントを自動的に取得し、棒読みちゃんでよみあげるコード。
JSONIC使ってます。
GET通信はここを参考にしました。http://d.hatena.ne.jp/yoheiM/20110131
ライブラリとID類は適当にどこかから。
整理する時間がないので汚いです。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.math.BigDecimal;

import net.arnx.jsonic.JSON;

public class Boyomi {
	private Date lastDate;
	private String token="";
	private final static String GROUPID="";
	private final static String APPID="アプリID";
	private final static String APPSECRETID="アプリシークレットID";
	private final static String REDIRECTURL="リダイレクトURL";
	private final static String AUTHCODE="authorizeメソッドで帰ってくるコード";

	public static void main(String[] args) {

		Boyomi boyomi=new Boyomi();
		try {

			while(true){
				LinkedHashMap lhm=boyomi.getFeed();

			String sayString=boyomi.pullSpeakData(lhm);
			System.out.println(sayString);
			if(sayString!=null){
			boyomi.speak(sayString);
			}
			Thread.sleep(300000);
			}

			//System.out.println(separateComment.get("message"));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	Boyomi(){
		lastDate=new Date();
	}
	private String changeDateFormat(String stringDate) throws ParseException{
		SimpleDateFormat sdfBef=new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ssz");	//2012-05-11T08:42:12+0000
		Date date=sdfBef.parse(stringDate);
		SimpleDateFormat sdfAft=new SimpleDateFormat("M'月'd'日'H'時'm'分'");
		return sdfAft.format(date);
	}
	private Date toDate(String stringDate) throws ParseException{
		SimpleDateFormat sdfBef=new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ssz");	//2012-05-11T08:42:12+0000

		return sdfBef.parse(stringDate);

	}
	public void speak(String speakData) throws IOException{
		Runtime.getRuntime().exec("\"RemoteTalk.exe\" /T \""+speakData+"\"");
	}
	public String pullSpeakData(LinkedHashMap lh){
		ArrayList dataLists=(ArrayList)lh.get("data");
		LinkedHashMap separateData=(LinkedHashMap)dataLists.get(0);

		LinkedHashMap from=(LinkedHashMap)separateData.get("from");

		String saySentence="";

		try {
			Date tmpDate=toDate((String)separateData.get("updated_time"));
			if(lastDate.compareTo(tmpDate)==0){//比較し、同一だったらなにもしない
				return null;
			}else{
				lastDate=tmpDate;

			};

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if(!separateData.get("updated_time").equals(separateData.get("created_time"))){
				saySentence+="コメントアップデート。";
			}else{
				saySentence+="アップデート。";
			}
			saySentence+=changeDateFormat((String)separateData.get("created_time"))+" ";
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		saySentence+=(String)from.get("name")+"。"+(String) separateData.get("message")+"。";

		LinkedHashMap comment=(LinkedHashMap)separateData.get("comments");

		ArrayList commentLists=(ArrayList) comment.get("data");

		int tmp_int=((BigDecimal)comment.get("count")).intValue();
		int count=0;
		if(tmp_int>3){
			count+=tmp_int-3;
		}
		for(;count < tmp_int;count++){
			LinkedHashMap separateComment=(LinkedHashMap) commentLists.get(count);
			//System.out.println(separateComment.get("message"));
			LinkedHashMap commentFrom=(LinkedHashMap)separateComment.get("from");

			try {
				saySentence+="コメント、"+changeDateFormat((String)separateComment.get("created_time"))+" ";
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			saySentence+=(String)commentFrom.get("name")+"  "+(String)separateComment.get("message")+"。  ";
		}
		return saySentence;

	}
	public LinkedHashMap getFeed() {
		String receiveHTML;
		while(true){
		try {
			receiveHTML = getHTML("https://graph.facebook.com/"+GROUPID+"/feed?access_token="+token);
			return toJSON(receiveHTML);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			try {
				String tmp_str=getHTML("https://graph.facebook.com/oauth/access_token?client_id="+APPID+"&client_secret="+APPSECRETID+"&redirect_uri="+REDIRECTURL+"&code="+AUTHCODE);
				System.out.println(tmp_str);
				token=tmp_str.substring(tmp_str.indexOf("access_token=")+13,tmp_str.indexOf("&",tmp_str.indexOf("access_token")));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}

	}
	private String getHTML(String geturl) throws IOException{
		// URLを作成してGET通信を行う
		URL url = new URL(geturl);
		HttpURLConnection http = (HttpURLConnection)url.openConnection();
		http.setRequestMethod("GET");
		http.connect();

		// サーバーからのレスポンスを標準出力へ出す
		BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
		String html = "", line = "";
		while((line = reader.readLine()) != null){
			html += line;
		}
			//System.out.println(html);

			reader.close();
		return html;
	}
	private LinkedHashMap toJSON(String data){

		return JSON.decode(data);

	}
}

広告