package kakaotalk;
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.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]){
URL url;7
InputStream is = null;
BufferedReader br;
String line;
try {
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
String contentType = connection.getContentType();
System.out.println(contentType);
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
if(responseCode== 200){ // 성공
if(contentType != null ){
if(contentType .contains("text/html")){
// 파싱 시작
br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String fullStr = "";
StringBuffer bf = new StringBuffer();
while ((line = br.readLine()) != null) {
// System.out.println(line);
bf .append(line).append("\n");
}
fullStr = bf.toString();
// 전체
System.out.println(fullStr);
long start = System.currentTimeMillis();
KakaoData data = getData(fullStr);
System.out.println(System.currentTimeMillis()-start);
}
}
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
class KakaoData {
public String imagePath =null;
public String content = null;
}
/**
* 메타 tag에서 content와 property 값을 가진 라인을 찾는다
* @param str
* @return
*/
public static KakaoData getData(String str) {
String reg = "<meta.*?((content=['\"].*['\"])|(property=['\"].*['\"])).*?((content=['\"].*['\"])|(property=['\"].*['\"])).*?>";
Pattern pattern2 = Pattern.compile(reg,
Pattern.CASE_INSENSITIVE);
Matcher matcher2 = pattern2.matcher(str);
while(matcher2.find()){
String match2 = matcher2.group();
System.out.println(" 0 : " + matcher2.group(0));
System.out.println(" 1 : " + matcher2.group(1));
// System.out.println(" 2 : " + matcher2.group(2));
// System.out.println(" 3 : " + matcher2.group(3));
System.out.println(" 4 : " + matcher2.group(4));
// System.out.println(" 5 : " + matcher2.group(5));
// System.out.println(" 6 : " + matcher2.group(6));
System.out.println();
}
// 한번더 필터링을 거친후 실제 데이터를 입력을 해주어야함
return null;
}
}