Skip to content

Commit

Permalink
Added more debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
Hronom committed Nov 18, 2015
1 parent f020cec commit f738601
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.hronom</groupId>
<artifactId>scrape-dat-website</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>jar</packaging>

<name>scrape-dat-website</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;

public class ScrapeDatWebsiteApp {
private static final Logger logger = LogManager.getLogger();

public static void main(String[] args) {
logger.info(ScrapeDatWebsiteApp.class.getSimpleName());
// Total number of processors or cores available to the JVM.
logger.info("Available processors (cores): " + Runtime.getRuntime().availableProcessors());
// Total amount of free memory available to the JVM.
logger.info("Free memory (bytes): " + Runtime.getRuntime().freeMemory());
// This will return Long.MAX_VALUE if there is no preset limit.
long maxMemory = Runtime.getRuntime().maxMemory();
// Maximum amount of memory the JVM will attempt to use.
logger.info("Maximum memory (bytes): " + (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
// Total memory currently in use by the JVM.
logger.info("Total memory (bytes): " + Runtime.getRuntime().totalMemory());
// Get a list of all filesystem roots on this system.
File[] roots = File.listRoots();
// For each filesystem root, print some info.
for (File root : roots) {
logger.info("File system root: " + root.getAbsolutePath());
logger.info("Total space (bytes): " + root.getTotalSpace());
logger.info("Free space (bytes): " + root.getFreeSpace());
logger.info("Usable space (bytes): " + root.getUsableSpace());
}

ScrapeView scrapeView = new ScrapeView();
new ScrapeButtonController(scrapeView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,59 +53,74 @@ public ActionListener createScrapeButtonActionListener() {
public void actionPerformed(ActionEvent event) {
Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
logger.info("Start processing...");
long beginTime = System.currentTimeMillis();

// Output input parameters.
if (!scrapeView.getWebsiteUrl().isEmpty() &&
!scrapeView.getSelector().isEmpty()) {
logger.info("Input parameters: " +
scrapeView.getWebsiteUrl() + ", " +
scrapeView.getSelector());
}
long beginTime = System.currentTimeMillis();

// Disable fields in view.
scrapeView.setWebsiteUrlTextFieldEnabled(false);
scrapeView.setSelectorTextFieldEnabled(false);
scrapeView.setScrapeButtonEnabled(false);
scrapeView.setWorkInProgress(true);
try {
scrapeView.setOutput("");
scrapeView.setOutput("");

// Process.
try {
URL url = new URL(scrapeView.getWebsiteUrl());
logger.info("Requesting page...");
HtmlPage page = webClient.getPage(url);
logger.info("Requesting of page completed.");


logger.info("View page as XML");
String xml = page.asXml();

// Unescape html.
logger.info("Unescape html");
xml = StringEscapeUtils.unescapeHtml4(xml);

logger.info("Get selector");
String selector = scrapeView.getSelector();
if (!xml.isEmpty() && !selector.isEmpty()) {
logger.info("Parse XML");
Document doc = Jsoup.parse(xml);
Elements selectedElements = doc.select(selector);

if (!selectedElements.isEmpty()) {
logger.info("Parse extracted elements");
StringBuilder sb = new StringBuilder();

for (Element element : selectedElements) {
String body = element.html();
sb.append(body);
sb.append("\n");
sb.append("\n");
}

scrapeView.setOutput(sb.toString());
}
}
} catch (IOException e) {
logger.error(e);
}

logger.info("Close WebClient.");
webClient.close();

// Enable fields in view.
scrapeView.setWorkInProgress(false);
scrapeView.setScrapeButtonEnabled(true);
scrapeView.setSelectorTextFieldEnabled(true);
scrapeView.setWebsiteUrlTextFieldEnabled(true);

long endTime = System.currentTimeMillis();
logger.info("Process time: " + (endTime - beginTime) + " ms.");
logger.info("Processing complete.");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ScrapeView() {

{
progressBar = new JProgressBar();
progressBar.setString("Working...");
progressBar.setString("Working, please wait...");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
progressBar.setVisible(false);
Expand Down

0 comments on commit f738601

Please sign in to comment.