์ „๊ณต ์ง€์‹์ด ์—†์–ด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ฐค ๊ฐ™์€ ๋ฐ ์งˆ๋ฌธ ์˜ฌ๋ฆฌ๊ธด ๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ๋‹ค ์”€..
์Šฌ๋”์Šค๋ผ๋Š” ๊ฒŒ์ž„ ๋ชจ๋“œ ๋งŒ๋“œ๋Š” ์ค‘์ธ๋ฐ gpt์— ์˜์ง€ํ•ด์„œ ์“ฐ๋‹ค๊ฐ€ ๋„๋ฌด์ง€ ์ดํ•ดํ•  ์ˆ˜ ์—†๋Š” ๋ฒ„๊ทธ๊ฐ€ ๋‚˜์™”์Œ

getLinks()๋Š” ๊ณต๋žต๊ธ€ ๋งํฌ์—์„œ ์นด๋“œ ์ด๋ฆ„๊ณผ ์—ฐ๊ฒฐ๋œ ๋งํฌ๋ฅผ ํฌ๋กค๋งํ•ด์„œ hashmap์— ์ €์žฅํ•ด์„œ ๋ฆฌํ„ดํ•˜๋Š”ย ํ•จ์ˆ˜๊ณ 
getComment()๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๋””์‹œ ๊ธ€์—์„œ ๋Œ“๊ธ€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ธ๋ฐ
getLinks()๋Š” ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•ด์„œ ์นด๋“œ ์ด๋ฆ„๊ณผ ๋งํฌ๊ฐ€ ์ž˜ ๋งค์นญ์ด ๋๋Š”๋ฐ getComment๋Š” ์ž๊พธ no comment๋งŒ ๋ฆฌํ„ดํ•จ
setSSL()์€ ์˜ค๋ฅ˜ ๋œจ๊ธธ๋ž˜ ๊ตฌ๊ธ€์— ๊ฒ€์ƒ‰ํ•ด์„œ ๋‚˜์˜จ ๊ฑฐ ๋ณต๋ถ™ํ•จ

public static void setSSL() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultHostnameVerifier(
new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

public static HashMap<String, String> getLinks() {
String url = "https://gall.dcinside.com/mgallery/board/view/?id=slay&no=171241";
try {
if(url.indexOf("https://") >= 0) {
JsoupWeb.setSSL();
}
Document doc = Jsoup.connect(url).get();

Elements links = doc.select("a.lnk[href*='slay']");

HashMap<String, String> linkMap = new HashMap<>();

for (Element link : links) {
String linkText = link.text();
String linkHref = link.attr("abs:href");
linkMap.put(linkText, linkHref);
}
return linkMap;

} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
}

public static String getComment(String link) {
String url = link;
try {
if(url.indexOf("https://") >= 0) {
JsoupWeb.setSSL();
}
Document doc = Jsoup.connect(url).get();

Elements paragraphs = doc.select("p.usertxt.ub-word");

List<String> texts = new ArrayList<>();

for (Element paragraph : paragraphs) {
texts.add(paragraph.text());
}

if (!texts.isEmpty()) {
Random random = new Random();
int randomIndex = random.nextInt(texts.size());
return texts.get(randomIndex);
} else {
return "no comment";
}

} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
}