package com.teamdev.jxbrowser.chromium.demoTest.jd427;

import java.awt.BorderLayout;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.hyjx.orclJdbcUtil.JDBCUtils;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;


public class sanya_good_num{
public static void main(String[] args) throws Exception {

LoggerProvider.getBrowserLogger().setLevel(Level.SEVERE);
LoggerProvider.getIPCLogger().setLevel(Level.SEVERE);
LoggerProvider.getChromiumProcessLogger().setLevel(Level.SEVERE);
[color=red]//初始化Browser [/color]
final Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
[color=red]
//禁止加载图片
BrowserPreferences preferences = browser.getPreferences();
preferences.setImagesEnabled(false);
preferences.setLoadsImagesAutomatically(true);

browser.setPreferences(preferences);[/color]

[color=red]//Jframe初始化设置[/color]
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(browserView, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("京东商品");

java.sql.Connection conOrcale = null;
try {
conOrcale = JDBCUtils.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = null;
PreparedStatement ps = null;
String sql1 = null;
Statement st2 = null;
ResultSet rs2 = null;
Document doc = null;
try {

sql1 = " select goodurl from z_sanya_jd_goodinfo where bad_num is null";
st2 = (java.sql.Statement) conOrcale.createStatement();
rs2 = st2.executeQuery(sql1);

} catch (Exception e) {
e.printStackTrace();
}
int i = 1;
for (; rs2.next();) {
System.out.println(i);
i++;
final String goodurl = rs2.getString("goodurl");
search(goodurl, frame, browser);
}

}
static void search(final String goodurl, JFrame frame, final Browser browser)throws Exception{
java.sql.Connection conOrcale = null;
try {
conOrcale = JDBCUtils.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = null;
PreparedStatement ps = null;
try {
sql = " update z_sanya_jd_goodinfo set bad_num = ? where goodurl = ? ";
ps = conOrcale.prepareStatement(sql);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("goodurl--------------》"+goodurl);
invokeAndWaitReady(browser, new Runnable() {
public void run() {
browser.loadURL(goodurl+"#comment");
}
});

// try {
// Thread.sleep(3000*1);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
String html = browser.getHTML();
Document document = Jsoup.parse(html);

String bad_num = "";
try {
bad_num = document.select("ul.filter-list li:eq(5) em").text();
} catch (Exception e) {
// TODO Auto-generated catch block
bad_num = "";
}
System.out.println("bad_num:"+bad_num+"\n下一个");
try {
ps.setString(1,bad_num);
ps.setString(2,goodurl);
ps.executeUpdate();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


public static void invokeAndWaitReady(Browser browser, Runnable runnable) {
final CountDownLatch latch = new CountDownLatch(1);
LoadAdapter listener = new LoadAdapter() {
@Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) {
latch.countDown();
}
}
};
browser.addLoadListener(listener);
try {
runnable.run();
try {
if (!latch.await(60, TimeUnit.SECONDS)) {
//throw new RuntimeException(new TimeoutException());
}
} catch (InterruptedException ignore) {
ignore.printStackTrace();
Thread.currentThread().interrupt();
}
} finally {
browser.removeLoadListener(listener);
}
}
}


//点击

browser.executeJavaScriptAndReturnValue("document.getElementsByClassName('cities-group')[3].getElementsByTagName('dd')[0].getElementsByTagName('ul')[0].getElementsByTagName('li')[10].getElementsByTagName('a')[0].click();");