Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.07 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package darkww;
  7.  
  8. import java.awt.Desktop;
  9. import java.awt.Toolkit;
  10. import java.awt.datatransfer.Clipboard;
  11. import java.awt.datatransfer.StringSelection;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.io.PrintStream;
  20. import java.io.PrintWriter;
  21. import java.io.UnsupportedEncodingException;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.nio.charset.StandardCharsets;
  25. import java.nio.file.Files;
  26. import java.nio.file.Paths;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Random;
  30. import java.util.ResourceBundle;
  31. import java.util.Timer;
  32. import java.util.TimerTask;
  33. import java.util.logging.Level;
  34. import java.util.logging.Logger;
  35. import java.util.prefs.Preferences;
  36. import javafx.application.Platform;
  37. import javafx.beans.property.SimpleIntegerProperty;
  38. import javafx.beans.value.ChangeListener;
  39. import javafx.beans.value.ObservableValue;
  40. import javafx.event.ActionEvent;
  41. import javafx.fxml.FXML;
  42. import javafx.fxml.Initializable;
  43. import javafx.scene.control.Alert;
  44. import javafx.scene.control.Alert.AlertType;
  45. import javafx.scene.control.Button;
  46. import javafx.scene.control.Control;
  47. import javafx.scene.control.Label;
  48. import javafx.scene.control.RadioButton;
  49. import javafx.scene.control.TextArea;
  50. import javafx.scene.control.ToggleGroup;
  51. import org.jsoup.Jsoup;
  52. import org.jsoup.nodes.Document;
  53. import org.openqa.selenium.By;
  54. import org.openqa.selenium.WebDriver;
  55. import org.openqa.selenium.WebElement;
  56. import org.openqa.selenium.firefox.FirefoxDriver;
  57. import org.openqa.selenium.firefox.FirefoxOptions;
  58.  
  59. /**
  60. *
  61. * @author Piotr
  62. */
  63. public class FXMLDocumentController implements Initializable {
  64.  
  65. @FXML
  66. private Label labelKoniec, labelTimer, labelTimer1;
  67. @FXML
  68. private TextArea ta1, ta2;
  69. @FXML
  70. private Button button, button1, buttonPrzerwij;
  71. @FXML
  72. private RadioButton radiotz, radiotp;
  73. // @FXML
  74. //private ToggleGroup group;
  75. WebDriver fb = null;
  76.  
  77. @FXML
  78. private void handleButtonAction(ActionEvent event) throws FileNotFoundException {
  79. labelTimer.setText("");
  80. if (ta1.getText().trim().isEmpty()) {
  81. alert();
  82. ta1.clear();
  83. ta1.requestFocus();
  84. } else {
  85. Timer timer1 = new Timer();
  86. MyTimerTask timer1_task = new MyTimerTask();
  87. timer1.schedule(timer1_task, 0, 1000);
  88.  
  89. labelKoniec.setText("KONIEC");
  90. ta2.clear();
  91. labelKoniec.setVisible(false);
  92. editableButton(true);
  93. button1.setDisable(true);
  94. new Thread(() -> {
  95.  
  96. boolean czyLinkObslugiwany = true;
  97.  
  98. try {
  99. FileOutputStream writer = new FileOutputStream("src/linki.txt");
  100. zapiszDoPliku(ta1.getText(), "linki");
  101.  
  102. FileOutputStream writer1 = new FileOutputStream("src/wygenerowane.txt");
  103. List<String> linki = Files.readAllLines(Paths.get("src/linki.txt"), StandardCharsets.UTF_8);
  104. List<String> konta = Files.readAllLines(Paths.get("src/konta.txt"), StandardCharsets.UTF_8);
  105. System.setProperty("webdriver.gecko.driver", "C://gecodriver/geckodriver.exe");
  106. System.setProperty("webdriver.firefox.bin", "C:\\Users\\Piotr\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
  107.  
  108. FirefoxOptions options = new FirefoxOptions();
  109. if (radiotz.isSelected()) {
  110. options.addArguments("--headless");
  111. }
  112.  
  113. fb = new FirefoxDriver(options);
  114.  
  115. System.out.println("selenium uruchomione");
  116. Platform.runLater(() -> buttonPrzerwij.setDisable(false));
  117.  
  118. Preferences p = Preferences.userRoot();
  119.  
  120. int index = p.getInt("darkw", 0);
  121.  
  122. if (index < 0 || index > konta.size() - 1) {
  123. index = 0;
  124. p.putInt("darkw", 0);
  125. }
  126. //int index = 0;
  127.  
  128. System.out.println("rejestr: " + p.getInt("darkw", 0) + " liczba kont: " + konta.size());
  129.  
  130. p:
  131. for (int i = 0; i < linki.size(); i++) {
  132. fb.get(linki.get(i));
  133.  
  134. List<String> hostingi;
  135.  
  136. try {
  137. hostingi = host(linki.get(i));
  138. } catch (Exception e) {
  139. System.out.println("wygasniety link");
  140. continue;
  141. }
  142.  
  143. String rozmiar = "";
  144. String strona = "";
  145.  
  146. if (hostingi.size() == 2) {
  147. rozmiar = hostingi.get(0);
  148. strona = hostingi.get(1);
  149. } else {
  150.  
  151. System.out.println("nieobslugiwany link: " + linki.get(i));
  152. System.out.println("generowanie przerwane");
  153. czyLinkObslugiwany = false;
  154. Platform.runLater(() -> labelKoniec.setText("Nieobsługiwany link"));
  155. break p;
  156. }
  157.  
  158. fb.get("https://darkw.pl/new-topic-f7/");
  159.  
  160. // Random r = new Random();
  161. // int index = r.nextInt(7);
  162. if (index == konta.size()) {
  163. index = 0;
  164. }
  165. String[] loginHaslo = konta.get(index).split(":");
  166.  
  167. fb.findElement(By.cssSelector("input[name='loginUserLogin']")).sendKeys(loginHaslo[0]);
  168. fb.findElement(By.cssSelector("input[name='loginUserPass']")).sendKeys(loginHaslo[1]);
  169.  
  170. index++;
  171. p.putInt("darkw", index);
  172.  
  173. fb.findElement(By.cssSelector("button[onclick='loader(); document.userLogin.submit();'")).click();
  174.  
  175. fb.findElement(By.cssSelector("input[name='postTitle']")).sendKeys(strona + " " + rozmiar);
  176. fb.findElement(By.cssSelector("textarea[name='postMessage']")).sendKeys("[code]" + linki.get(i) + "[/code]");
  177. fb.findElement(By.cssSelector("div[onclick=\"$('#postOption').val('0'); loader(); document.newTopic.submit();\"")).click();
  178.  
  179. System.out.println("dodaje post: " + (1 + i) + " z " + linki.size());
  180.  
  181. // ustawPole("dodaję post: " + (1 + i) + " z " + linki.get(i));
  182. //Thread.sleep(45000);
  183. int licznik = 0;
  184.  
  185. labelTimer1.setVisible(true);
  186. while (true) {
  187. Timer timer2 = new Timer();
  188. MyTimerTask1 timer2_task = new MyTimerTask1();
  189. timer2.schedule(timer2_task, 0, 1000);
  190.  
  191. Thread.sleep(10000);
  192. timer2_task.cancel();
  193. fb.get(fb.getCurrentUrl());
  194. List<WebElement> code = fb.findElements(By.cssSelector("code"));
  195. if (code.size() > 1) {
  196. System.out.println("zapis do pliku: " + code.get(1).getText());
  197. zapiszDoPliku(code.get(1).getText(), "wygenerowane");
  198. String content = new String(Files.readAllBytes(Paths.get("src/wygenerowane.txt")), "UTF-8");
  199. ta2.setText(content);
  200. ta2.positionCaret(content.length());
  201. //System.out.println(code.get(1).getText());
  202. fb.get("https://darkw.pl/logout/");
  203. break;
  204. } else {
  205. licznik++;
  206. //Thread.sleep(10000);
  207. System.out.println("Odswiezam post: " + (1 + i) + " z " + linki.size());
  208. // ustawPole("Odświeżam post: " + fb.getCurrentUrl());
  209. System.out.println("licznik: " + licznik);
  210. //ustawPole("licznik: " + licznik);
  211. }
  212. if (licznik == 24) {
  213. i--;
  214. fb.get("https://darkw.pl/logout/");
  215. System.out.println("continue");
  216. // ustawPole("continue");
  217. continue p;
  218. }
  219. }
  220. }
  221. fb.quit();
  222. timer1_task.stop();
  223.  
  224. if (czyLinkObslugiwany) {
  225. String content = new String(Files.readAllBytes(Paths.get("src/wygenerowane.txt")), "UTF-8");
  226.  
  227. StringSelection stringSelection = new StringSelection(content);
  228. Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  229. clipboard.setContents(stringSelection, null);
  230.  
  231. ta2.setText(content);
  232.  
  233. }
  234.  
  235. labelKoniec.setVisible(true);
  236. editableButton(false);
  237. labelTimer1.setVisible(false);
  238. Platform.runLater(() -> buttonPrzerwij.setDisable(true));
  239. Platform.runLater(() -> button1.setDisable(false));
  240. //System.out.println("koniec");
  241. // ustawPole("koniec");
  242.  
  243. //System.exit(0);
  244. } catch (IOException ex) {
  245. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  246. System.out.println(ex.getMessage());
  247. fb.quit();
  248. editableButton(false);
  249. Platform.runLater(() -> buttonPrzerwij.setDisable(true));
  250. Platform.runLater(() -> button1.setDisable(false));
  251. timer1_task.stop();
  252. } catch (InterruptedException ex) {
  253. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  254. System.out.println(ex.getMessage());
  255. fb.quit();
  256. editableButton(false);
  257. timer1_task.stop();
  258. Platform.runLater(() -> buttonPrzerwij.setDisable(true));
  259. Platform.runLater(() -> button1.setDisable(false));
  260. } catch (Exception ex) {
  261. try {
  262. fb.quit();
  263. timer1_task.stop();
  264. System.out.println(ex.getMessage());
  265. } catch (Exception e) {
  266. System.out.println("przerwane przez uzytkownika");
  267. timer1_task.stop();
  268. }
  269. editableButton(false);
  270. Platform.runLater(() -> buttonPrzerwij.setDisable(true));
  271. Platform.runLater(() -> button1.setDisable(false));
  272. }
  273. }).start();
  274. }
  275. }
  276.  
  277. @FXML
  278.  
  279. private void otworzWygenerowane(ActionEvent event) throws IOException {
  280. Desktop.getDesktop().open(new File("src/wygenerowane.txt"));
  281. }
  282.  
  283. @FXML
  284. private void otworzLinki(ActionEvent event) throws IOException {
  285. Desktop.getDesktop().open(new File("src/wypucowane.txt"));
  286. }
  287.  
  288. @FXML
  289. private void otworzWypucowane(ActionEvent event) throws IOException {
  290. Desktop.getDesktop().open(new File("src/wypucowane.txt"));
  291. }
  292.  
  293. @FXML
  294. private void otworzDobre(ActionEvent event) throws IOException {
  295. Desktop.getDesktop().open(new File("src/dobre.txt"));
  296. }
  297.  
  298. @FXML
  299. private void sprawdzLinki(ActionEvent event) throws IOException {
  300. ta2.clear();
  301. labelKoniec.setText("KONIEC");
  302. labelKoniec.setVisible(false);
  303. FileOutputStream writer = new FileOutputStream("src/dobre.txt");
  304. FileOutputStream writer1 = new FileOutputStream("src/wypucowane.txt");
  305. new Thread(() -> {
  306.  
  307. try {
  308. Timer timer1 = new Timer();
  309. MyTimerTask timer1_task = new MyTimerTask();
  310. timer1.schedule(timer1_task, 0, 1000);
  311.  
  312. String linki[] = ta1.getText().split("\n");
  313.  
  314. java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
  315.  
  316. Document doc = null;
  317. double rozmiarc = 0;
  318. int wypucowane = 0;
  319. int dobre = 0;
  320. for (int i = 0; i < linki.length; i++) {
  321. try {
  322. doc = Jsoup.connect(linki[i]).get();
  323. String rozmiar = doc.select(".title>small").text();
  324. Platform.runLater(() -> {
  325. ta2.clear();
  326. });
  327. System.out.println((1 + i) + " z " + linki.length);
  328. if (rozmiar.trim().isEmpty()) {
  329. zapiszDoPliku(linki[i], "wypucowane");
  330. System.out.println("wypucowane");
  331. wypucowane++;
  332. } else {
  333. zapiszDoPliku(linki[i], "dobre");
  334. dobre++;
  335. String m[] = rozmiar.split(" ");
  336. double rd = Double.parseDouble(m[0]);
  337. if (m[1].equals("MB")) {
  338. //System.out.println("mb");
  339. rd = Double.parseDouble(m[0]) / 1024;
  340. }
  341. rozmiarc += rd;
  342. }
  343. } catch (IOException ex) {
  344. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  345. }
  346.  
  347. }
  348.  
  349. String content = new String(Files.readAllBytes(Paths.get("src/dobre.txt")), "UTF-8");
  350.  
  351. Platform.runLater(() -> {
  352. ta2.clear();
  353. });
  354.  
  355. System.out.println("wypucowane linki: " + wypucowane);
  356. System.out.println("rozmiar wszystkich linkow: " + df.format(rozmiarc) + " GB");
  357. System.out.println("dobre linki: " + dobre);
  358. System.out.println("wszystkie linki: " + linki.length);
  359. System.out.println("dobre: ");
  360. System.out.println("");
  361.  
  362. Platform.runLater(() -> {
  363. ta2.appendText(content);
  364. });
  365.  
  366. labelKoniec.setVisible(true);
  367. timer1_task.stop();
  368. } catch (UnsupportedEncodingException ex) {
  369. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  370. } catch (IOException ex) {
  371. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  372. }
  373.  
  374. }).start();
  375. }
  376.  
  377. @FXML
  378. private void breakSelenium(ActionEvent event) throws IOException {
  379. Platform.runLater(() -> {
  380. fb.close();
  381. buttonPrzerwij.setDisable(true);
  382. button.setDisable(false);
  383. });
  384.  
  385. }
  386.  
  387. @Override
  388. public void initialize(URL url, ResourceBundle rb) {
  389.  
  390.  
  391. /* SimpleIntegerProperty count = new SimpleIntegerProperty(20);
  392. int rowHeight = 10;
  393.  
  394. ta1.prefHeightProperty().bindBidirectional(count);
  395. ta1.minHeightProperty().bindBidirectional(count);
  396. ta1.maxHeight(20);
  397. ta1.scrollTopProperty().addListener(new ChangeListener<Number>() {
  398. @Override
  399. public void changed(ObservableValue<? extends Number> ov, Number oldVal, Number newVal) {
  400. if (newVal.intValue() > rowHeight && newVal.intValue()<20) {
  401. count.setValue(count.get() + newVal.intValue());
  402. }
  403. }
  404. });*/
  405. Platform.runLater(() -> ta1.requestFocus());
  406.  
  407. OutputStream out = new OutputStream() {
  408. @Override
  409. public void write(int b) throws IOException {
  410. appendText(String.valueOf((char) b));
  411. }
  412. };
  413. System.setOut(new PrintStream(out, true));
  414.  
  415. }
  416.  
  417. public void appendText(String str) {
  418. Platform.runLater(() -> ta2.appendText(str));
  419. }
  420.  
  421. public void editableButton(boolean t) {
  422. Platform.runLater(() -> button.setDisable(t));
  423. }
  424.  
  425. public static void zapiszDoPliku(String text, String nazwa) {
  426.  
  427. try (FileWriter fw = new FileWriter("src/" + nazwa + ".txt", true);
  428. BufferedWriter bw = new BufferedWriter(fw);
  429. PrintWriter out = new PrintWriter(bw)) {
  430. out.println(text);
  431.  
  432. } catch (IOException e) {
  433.  
  434. System.out.println("brak pliku");
  435. }
  436.  
  437. }
  438.  
  439. public List<String> host(String link) throws MalformedURLException {
  440.  
  441. List<String> hosty = new ArrayList<>();
  442. if (link.contains("rapidu.net")) {
  443. hosty.add(fb.findElement(By.cssSelector(".title>small")).getText().replace(".", ","));
  444. } else if (link.contains("fileshark.pl")) {
  445. hosty.add(fb.findElement(By.cssSelector(".size-file > strong:nth-child(1)")).getText().replace(".", ","));
  446. }
  447.  
  448. //System.out.println("host = " + getDomain(link));
  449. hosty.add(getDomain(link));
  450.  
  451. return hosty;
  452. }
  453.  
  454. public String getDomain(String link) throws MalformedURLException {
  455. URL url = new URL(link);
  456. return url.getHost().replace("www.", "").replaceAll("\\..*", "");
  457. }
  458.  
  459. public void alert() {
  460.  
  461. Alert alert = new Alert(AlertType.ERROR);
  462. alert.setTitle("Info");
  463. alert.setHeaderText(null);
  464. alert.setContentText("Nie podałeś żadnych linków!");
  465.  
  466. alert.showAndWait();
  467. }
  468.  
  469. class MyTimerTask extends TimerTask {
  470.  
  471. int sekundy = 0;
  472. int minuty = 0;
  473. int godziny = 0;
  474. String g;
  475. String m;
  476. String s;
  477.  
  478. public void run() {
  479. sekundy++;
  480. if (sekundy == 60) {
  481. minuty++;
  482. sekundy = 0;
  483. if (minuty == 60) {
  484. godziny++;
  485. minuty = 0;
  486. }
  487. }
  488. g = godziny + "";
  489. m = minuty + "";
  490. s = sekundy + "";
  491. if (godziny < 10) {
  492. g = "0" + godziny;
  493. }
  494. if (minuty < 10) {
  495. m = "0" + minuty;
  496. }
  497. if (sekundy < 10) {
  498. s = "0" + sekundy;
  499. }
  500. Platform.runLater(() -> labelTimer.setText("czas działania: " + g + ":" + m + ":" + s));
  501. }
  502.  
  503. public void stop() {
  504. cancel();
  505. }
  506. }
  507.  
  508. class MyTimerTask1 extends TimerTask {
  509.  
  510. int sekundy = 10;
  511.  
  512. public void run() {
  513.  
  514. // if (sekundy < 10) {
  515. // s = "0" + sekundy;
  516. //}
  517. Platform.runLater(() -> labelTimer1.setText("odświeżam post za: " + sekundy + " s"));
  518. sekundy--;
  519. }
  520.  
  521. public void stop() {
  522. Platform.runLater(() -> labelTimer1.setText(""));
  523. cancel();
  524. }
  525. }
  526.  
  527. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement