Created
August 23, 2016 17:20
-
-
Save caioketo/2667a60505bd862ec995b7b9f1b34e20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ui; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Scanner; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import org.w3c.dom.Node; | |
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.concurrent.Worker; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.RadioButton; | |
import javafx.scene.control.ScrollPane; | |
import javafx.scene.control.TableCell; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.ToggleGroup; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Circle; | |
import javafx.scene.web.WebEngine; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
import javafx.util.Callback; | |
public class Principal extends Application { | |
private TableView<BlackListedSites> table = new TableView<BlackListedSites>(); | |
private static ObservableList<BlackListedSites> data = FXCollections.observableArrayList(); | |
private static String[] sites = null; | |
private static int checkingIndex = 0; | |
private static int lastIndex = -1; | |
private static int tries = 0; | |
private static int triesOut = 2; | |
private static Circle circle; | |
private static Timer timer; | |
public static TextArea textArea; | |
static WebEngine webEngine; | |
static WebView webView; | |
public static String getOS() { | |
String os = System.getProperty("os.name").toLowerCase(); | |
if(os.indexOf("mac") >= 0){ | |
return "MAC"; | |
} | |
else if(os.indexOf("win") >= 0){ | |
return "WIN"; | |
} | |
else if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0){ | |
return "LINUX/UNIX"; | |
} | |
else if(os.indexOf("sunos") >= 0){ | |
return "SOLARIS"; | |
} | |
return ""; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public void start(Stage stage) { | |
stage.setTitle("Blacklists Alert"); | |
stage.setWidth(500); | |
stage.setHeight(500); | |
stage.setMaximized(true); | |
Scene scene = new Scene(new Group()); | |
VBox root = new VBox(); | |
//data.add(new BlackListedSites("google.com.br", false, 0)); | |
webView = new WebView(); | |
webView.setCache(false); | |
webEngine = new WebEngine(); //webView.getEngine(); | |
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Object>() { | |
@Override | |
public void changed(ObservableValue<?> observableValue, Object state, Object newState) { | |
System.out.println("old state: " + state + ", new state: " + newState + ", arg0: " + observableValue); | |
if (newState.equals(Worker.State.SUCCEEDED)) { | |
String setLastName = " $('#txtInput2').val(\"" + sites[checkingIndex] + "\"); " + | |
"setCommand2('blacklist','Blacklist Check');"; | |
String click = " $('#btnAction3').click();"; | |
webEngine.executeScript(setLastName); | |
webEngine.executeScript(click); | |
new Timer().schedule(new TimerTask() { | |
@Override | |
public void run() { | |
// TODO Auto-generated method stub | |
ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
try { | |
printDocument(webEngine.getDocument(), output); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (TransformerException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
String out = ""; | |
try { | |
out = new String(output.toByteArray(), "UTF-8"); | |
} catch (UnsupportedEncodingException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
if (out.contains("<DIV class=\"tool-result-body\">")) { | |
String teste = out.substring(out.indexOf("<DIV class=\"tool-result-body\">"), | |
out.indexOf("<TABLE class=\"table table-striped")); | |
System.out.print(teste); | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
SetBlackListURL(getBlacklisted(html2text(teste))); | |
} | |
}); | |
} | |
else { | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
CheckBlacklist(); | |
} | |
}); | |
} | |
} | |
}, 5000); | |
/*Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
//webEngine.executeScript(setLastName); | |
//webEngine.executeScript(click); | |
try { | |
Thread.sleep(4000); | |
} catch (InterruptedException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} | |
});*/ | |
return; | |
/**/ | |
} | |
} | |
}); | |
textArea = new TextArea(); | |
final Button btn = new Button(); | |
btn.setText("Check All Domains"); | |
btn.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
circle.setFill(Color.GREEN); | |
String allSites = textArea.getText(); | |
sites = allSites.split("\\r?\\n"); | |
checkingIndex = 0; | |
webEngine.load("http://mxtoolbox.com/SuperTool.aspx"); | |
//CheckURLs(); | |
} | |
}); | |
final Button btn2 = new Button(); | |
btn2.setText("Save"); | |
btn2.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
try { | |
String fileName = ""; | |
if (getOS().equals("WIN")) { | |
fileName = System.getProperty("user.dir") + "\\siteList.txt"; | |
} | |
else { | |
fileName = System.getProperty("user.dir") + "//siteList.txt"; | |
} | |
PrintWriter out = new PrintWriter(fileName); | |
String[] saves = textArea.getText().split("\\r?\\n"); | |
for (String s : saves) { | |
out.println(s); | |
} | |
out.close(); | |
} catch (FileNotFoundException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} | |
}); | |
final Button btn3 = new Button(); | |
btn3.setText("Load"); | |
btn3.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
try { | |
String fileName2 = ""; | |
if (getOS().equals("WIN")) { | |
fileName2 = System.getProperty("user.dir") + "\\siteList.txt"; | |
} | |
else { | |
fileName2 = System.getProperty("user.dir") + "//siteList.txt"; | |
} | |
String content = new Scanner(new File(fileName2)).useDelimiter("\\Z").next(); | |
textArea.setText(content); | |
} catch (FileNotFoundException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
}); | |
HBox box1 = new HBox(); | |
VBox vbox1 = new VBox(); | |
VBox vbox2 = new VBox(); | |
Pane p = new Pane(); | |
ToggleGroup toggleGroup = new ToggleGroup(); | |
RadioButton radioItem1 = new RadioButton("Check Once Every Day"); | |
radioItem1.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent e) { | |
timer = new Timer(); | |
Date d = new Date(); | |
Calendar c = Calendar.getInstance(); | |
c.add(Calendar.DAY_OF_MONTH, 1); | |
c.set(Calendar.HOUR_OF_DAY, 0); | |
c.set(Calendar.MINUTE, 0); | |
c.set(Calendar.SECOND, 0); | |
c.set(Calendar.MILLISECOND, 0); | |
long howMany = (c.getTimeInMillis()-System.currentTimeMillis()); | |
timer.schedule(new CustomTask(), howMany / 2, 86400000); | |
} | |
}); | |
radioItem1.setToggleGroup(toggleGroup); | |
RadioButton radioItem2 = new RadioButton("Check Once Every Week"); | |
radioItem2.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent e) { | |
timer = new Timer(); | |
timer.schedule(new CustomTask(), 86400000 * 7, 86400000 * 7); | |
} | |
}); | |
radioItem2.setToggleGroup(toggleGroup); | |
RadioButton radioItem3 = new RadioButton("Check Once Every 2 Week"); | |
radioItem3.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent e) { | |
timer = new Timer(); | |
timer.schedule(new CustomTask(), 86400000 * 14, 86400000 * 14); | |
} | |
}); | |
radioItem3.setToggleGroup(toggleGroup); | |
Label lbl = new Label("My Domains"); | |
table.setEditable(false); | |
textArea.setPrefWidth(500); | |
Pane pText = new Pane(); | |
pText.getChildren().addAll(lbl, textArea, btn); | |
vbox1.getChildren().addAll(pText); | |
Pane p2 = new Pane(); | |
final ImageView imv = new ImageView(); | |
final Image image2 = new Image(getClass().getClassLoader().getResourceAsStream("button.png")); | |
imv.setImage(image2); | |
p2.getChildren().addAll(imv, radioItem1, radioItem2, radioItem3, btn2, btn3); | |
radioItem1.setLayoutY(110); | |
radioItem2.setLayoutY(130); | |
radioItem3.setLayoutY(150); | |
//btn.setLayoutY(80); | |
btn2.setLayoutY(105); | |
btn3.setLayoutY(145); | |
btn3.setLayoutX(290); | |
radioItem1.setLayoutX(10); | |
radioItem2.setLayoutX(10); | |
radioItem3.setLayoutX(10); | |
btn2.setLayoutX(290); | |
btn2.setPrefWidth(100); | |
btn3.setPrefWidth(100); | |
vbox2.getChildren().addAll(p2); | |
btn.setLayoutY(190); | |
textArea.setPrefHeight(150); | |
textArea.setLayoutY(20); | |
p.getChildren().addAll(vbox2, vbox1); | |
vbox1.setLayoutX(550); | |
imv.setLayoutY(10); | |
box1.getChildren().addAll(p); | |
TableColumn<BlackListedSites, String> siteCol = new TableColumn<BlackListedSites, String>("Domain"); | |
TableColumn<BlackListedSites, String> blacklistCol = new TableColumn<BlackListedSites, String>("Status"); | |
TableColumn<BlackListedSites, String> numberCol = new TableColumn<BlackListedSites, String>("Details"); | |
TableColumn<BlackListedSites, String> lastCheckCol = new TableColumn<BlackListedSites, String>("Last Checked"); | |
siteCol.setCellValueFactory(new PropertyValueFactory<BlackListedSites,String>("Site")); | |
blacklistCol.setCellValueFactory(new PropertyValueFactory<BlackListedSites,String>("Blacklisted")); | |
numberCol.setCellValueFactory(new PropertyValueFactory<BlackListedSites,String>("BlacklistedOn")); | |
lastCheckCol.setCellValueFactory(new PropertyValueFactory<BlackListedSites,String>("LastChecked")); | |
siteCol.setPrefWidth(200); | |
lastCheckCol.setPrefWidth(200); | |
blacklistCol.setPrefWidth(200); | |
numberCol.setPrefWidth(200); | |
table.setItems(data); | |
table.getColumns().addAll(siteCol, lastCheckCol, blacklistCol, numberCol); | |
numberCol.setCellFactory(new Callback() { | |
@Override | |
public TableCell call(Object param) { | |
TableCell cell = new TableCell<BlackListedSites, String>() { | |
@Override | |
public void updateItem(String item, boolean empty) { | |
super.updateItem(item, empty); | |
setText(empty ? null : getString()); | |
setGraphic(null); | |
} | |
private String getString() { | |
return getItem() == null ? "" : getItem().toString(); | |
} | |
}; | |
//if (((TableColumn)param).getText().equals("Details")) { | |
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent event) { | |
if (event.getClickCount() == 1) { | |
TableCell c = (TableCell) event.getSource(); | |
final BlackListedSites rowData = (BlackListedSites)c.getTableRow().getItem(); | |
VBox root2 = new VBox(); | |
final WebView webView = new WebView(); | |
final WebEngine webEngine2 = webView.getEngine(); | |
root2.getChildren().add(webView); | |
ScrollPane scrollPane = new ScrollPane(); | |
scrollPane.setContent(webView); | |
webEngine2.getLoadWorker().stateProperty().addListener(new ChangeListener<Object>() { | |
@Override | |
public void changed(ObservableValue<?> observableValue, Object state, Object newState) { | |
System.out.println("old state: " + state + ", new state: " + newState + ", arg0: " + observableValue); | |
if (newState.equals(Worker.State.SUCCEEDED)) { | |
String setLastName = " $('#txtInput2').val(\"" + rowData.getSite() + "\"); " + | |
"setCommand2('blacklist','Blacklist Check');"; | |
String click = " $('#btnAction3').click();"; | |
webEngine2.executeScript(setLastName); | |
webEngine2.executeScript(click); | |
} | |
} | |
}); | |
webEngine2.load("http://mxtoolbox.com/SuperTool.aspx"); | |
System.out.println(webEngine2.getLocation()); | |
Stage stage2 = new Stage(); | |
stage2.setTitle("Detail: " + rowData.getSite()); | |
Scene scene2 = new Scene(new Group(), 800, 600); | |
root2.getChildren().addAll(scrollPane); | |
scene2.setRoot(root2); | |
stage2.setScene(scene2); | |
stage2.show(); | |
} | |
} | |
}); | |
//} | |
return cell; | |
} | |
}); | |
HBox box = new HBox(); | |
circle = new Circle(10, Color.RED); | |
Pane tableP = new Pane(); | |
tableP.getChildren().addAll(table, circle); | |
table.setLayoutY(10); | |
table.setLayoutX(10); | |
table.setPrefWidth(1100); | |
circle.setLayoutY(430); | |
circle.setLayoutX(10); | |
root.getChildren().addAll(box, box1, tableP); | |
circle.setLayoutX(30); | |
scene.setRoot(root); | |
stage.setScene(scene); | |
try { | |
String fileName = ""; | |
if (getOS().equals("WIN")) { | |
fileName = System.getProperty("user.dir") + "\\siteList.txt"; | |
} | |
else { | |
fileName = System.getProperty("user.dir") + "//siteList.txt"; | |
} | |
String content = new Scanner(new File(fileName)).useDelimiter("\\Z").next(); | |
textArea.setText(content); | |
} catch (FileNotFoundException e1) { | |
e1.printStackTrace(); | |
} | |
stage.show(); | |
} | |
public class CustomTask extends TimerTask { | |
@Override | |
public void run() { | |
// TODO Auto-generated method stub | |
if (textArea.getText().isEmpty()) { | |
return; | |
} | |
circle.setFill(Color.GREEN); | |
String allSites = textArea.getText(); | |
sites = allSites.split("\\r?\\n"); | |
checkingIndex = 0; | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
// TODO Auto-generated method stub | |
webEngine.load("http://mxtoolbox.com/SuperTool.aspx"); | |
} | |
}); | |
} | |
} | |
public static void CheckURLs() { | |
if (checkingIndex >= sites.length) { | |
circle.setFill(Color.RED); | |
//JOptionPane.showMessageDialog(null, "Checking DONE!", "Checking", JOptionPane.INFORMATION_MESSAGE); | |
return; | |
} | |
CheckBlacklist(); | |
} | |
public static void CheckBlacklist() { | |
if (lastIndex == checkingIndex) { | |
tries++; | |
} | |
else { | |
tries = 0; | |
lastIndex = checkingIndex; | |
} | |
if (tries >= triesOut) { | |
checkingIndex++; | |
CheckURLs(); | |
return; | |
} | |
String url = ""; | |
url = "http://mxtoolbox.com/SuperTool.aspx";//?action=blacklist" + URLEncoder.encode(":") + sites[checkingIndex]; | |
System.out.println(url); | |
webEngine.load(url); | |
} | |
public static void SetBlackListURL(int blacklistedInt) { | |
BlackListedSites bls = new BlackListedSites(sites[checkingIndex], blacklistedInt != 0, blacklistedInt); | |
bls.setLastChecked(new Date().getTime()); | |
data.add(bls); | |
checkingIndex++; | |
CheckURLs(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
public static int getBlacklisted(String result) { | |
String numberBlacklisted = result.substring(result.indexOf("Listed") + 7, result.indexOf("times") - 1); | |
return Integer.parseInt(numberBlacklisted); | |
} | |
public static String html2text(String inp) { | |
boolean intag = false; | |
String outp = ""; | |
for (int i=0; i < inp.length(); ++i) | |
{ | |
if (!intag && inp.charAt(i) == '<') | |
{ | |
intag = true; | |
continue; | |
} | |
if (intag && inp.charAt(i) == '>') | |
{ | |
intag = false; | |
continue; | |
} | |
if (!intag) | |
{ | |
outp = outp + inp.charAt(i); | |
} | |
} | |
return outp.replaceAll("\\s\\s", ""); | |
} | |
public static void printDocument(org.w3c.dom.Document document, OutputStream out) throws IOException, TransformerException { | |
TransformerFactory tf = TransformerFactory.newInstance(); | |
Transformer transformer = tf.newTransformer(); | |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); | |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); | |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); | |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); | |
transformer.transform(new DOMSource((Node) document), | |
new StreamResult(new OutputStreamWriter(out, "UTF-8"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment