ニコ生API・認証APIにアクセス
公式のニコ生アラートAPIがリリースされたので、とりあえず認証APIを用いてコメントサーバから
データを受信するために必要なサーバアドレス、ポート番号、スレッドIDを取得するためのクラスを
作ってみた。
言語はJavaです。
※ 例外処理はユルユルです。
ツッコミ、リファクタリング大いに歓迎。
package com.sunrise.tool; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.SortedSet; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /* * ニコニコ生放送APIを利用した認証クラス */ public class NiconamaAuthz { private static final String AUTH_API_1_URL = "https://secure.nicovideo.jp/secure/login?site=nicolive_antenna"; private static final String AUTH_API_2_URL = "http://live.nicovideo.jp/api/getalertstatus"; private static final String USER_AGENT = "NicoLiveAlert 1.0.0"; private static final String REGEX = "nicolive_antenna_[0-9]+"; private String addr = ""; private int port = 0; private String thread = ""; /* * コメントサーバアドレスを取得する。 */ public String getComServAddres() { return this.addr; } /* * コメントサーバポート番号を取得する。 */ public int getComServPort() { return this.port; } /* * スレッドIDを取得する。 */ public String getThreadId() { return this.thread; } /** * 認証APIにより認証を行う。 */ public boolean doAuthz(String mail_add, String passwd) { PrintStream ps = null; BufferedReader br = null; String ticket = null; try { URL auth_url = new URL(AUTH_API_1_URL); URLConnection con = auth_url.openConnection(); con.setDoOutput(true); con.setRequestProperty("User-Agent", USER_AGENT); OutputStream os = con.getOutputStream(); ps = new PrintStream(os); ps.print("mail=" + mail_add + "&" + "password=" + passwd + ""); InputStream is = con.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); Pattern pattern = Pattern.compile(REGEX); String line; while(null != (line = br.readLine())) { Matcher matcher = pattern.matcher(line); while(matcher.find()) { ticket = matcher.group(); } } } catch (MalformedURLException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { if (null != ps) { ps.close(); } if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } if(!this.getAlertStatus(ticket)) { return false; } return true; } /* * 認証APIにより各種ステータス情報を取得する。 */ private boolean getAlertStatus(String ticket) { PrintStream ps = null; BufferedReader br = null; try { URL auth_url = new URL(AUTH_API_2_URL); URLConnection con = auth_url.openConnection(); con.setDoOutput(true); con.setRequestProperty("User-Agent", USER_AGENT); OutputStream os = con.getOutputStream(); ps = new PrintStream(os); ps.print("ticket=" + ticket); InputStream is = con.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); String line; String content = ""; while(null != (line = br.readLine())) { content = content + line; } this.getInfos(content); } catch (MalformedURLException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { if (null != ps) { ps.close(); } if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /* * 認証API2のレスポンスXMLから情報を抽出する。 */ private void getInfos(String content) { try { System.out.println(content); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(content.getBytes())); this.addr = doc.getElementsByTagName("addr").item(0).getFirstChild().getNodeValue(); this.port = Integer.parseInt(doc.getElementsByTagName("port").item(0).getFirstChild().getNodeValue()); this.thread = doc.getElementsByTagName("thread").item(0).getFirstChild().getNodeValue(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }