1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
| @FXMLController public class DashBoardController extends BaseController implements Initializable { private static Logger logger = LoggerFactory.getLogger(DashBoardController.class);
private Image rootIcon; private Image dayIcon; private Image keyWordIcon; private Image demoIcon;
@FXML private MenuItem exit; @FXML private MenuItem setting; @FXML private MenuItem about;
@FXML private TreeView<String> treeView;
@FXML private TextField url; @FXML private TextField keys; @FXML private TextField maxDepth; @FXML private TextField htmlThreadNum; @FXML private TextField parseThreadNum; @FXML private TextField storeThreadNum; @FXML private ComboBox<StoreType> storeType; @FXML private TextField localPath;
@FXML private Button startBtn; @FXML private Button stopBtn;
@FXML private StatusBar statusBar;
@Autowired private SettingMapper settingMapper; @Autowired private SpiderHistoryMapper spiderHistoryMapper; @Autowired private Spider spider;
@Override public void initialize(URL location, ResourceBundle resources) { rootIcon = new Image(this.getClass().getResourceAsStream("/images/history.png"), 25, 25, false, false); dayIcon = new Image(this.getClass().getResourceAsStream("/images/date.png"), 25, 25, false, false); keyWordIcon = new Image(this.getClass().getResourceAsStream("/images/keyword.png"), 25, 25, false, false); demoIcon = new Image(this.getClass().getResourceAsStream("/images/demo.png"), 25, 25, false, false);
initMenus();
ObservableList<StoreType> storeValues = FXCollections.observableArrayList(StoreType.values()); storeType.getItems().addAll(storeValues); storeType.getSelectionModel().select(StoreType.MYSQL);
Setting setting = settingMapper.selectByPrimaryKey(Constants.SettingDefaultId); final DirectoryChooser fileChooser = new DirectoryChooser(); String workDir = System.getProperties().getProperty("user.dir"); String storeLocalPath = StringUtils.isBlank(setting.getLocalPath()) ? workDir : setting.getLocalPath(); localPath.setText(storeLocalPath); localPath.setOnMouseClicked(event -> { configureFileChooser(fileChooser, storeLocalPath); File file = fileChooser.showDialog(localPath.getParent().getScene().getWindow()); if (file != null) { logger.info("localPath: {}", file); localPath.setText(file.getAbsolutePath()); } });
ChangeListener<String> numberValidListener = (observable, oldValue, newValue) -> { if (!newValue.matches("\\d*")) { maxDepth.setText(newValue.replaceAll("[^\\d]", "")); } }; maxDepth.textProperty().addListener(numberValidListener); htmlThreadNum.textProperty().addListener(numberValidListener); parseThreadNum.textProperty().addListener(numberValidListener); storeThreadNum.textProperty().addListener(numberValidListener);
initTreeView(); }
private void initMenus() { exit.setOnAction(actionEvent -> Platform.exit()); setting.setOnAction(event -> { SpiderApplication.showView(SettingView.class, Modality.WINDOW_MODAL); }); about.setOnAction(event -> { Dialog<?> dialog = new Dialog<>(); dialog.setTitle("关于幻猿·简易爬虫"); dialog.setContentText("\n\t一个简易的爬虫系统。\n\n" + "\t基于SpringBoot2、MyBatis、JavaFx技术实现。\n\n" + "\t\t\t\t\t\t\tversion: 0.0.1\n\n"); dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE); Node closeButton = dialog.getDialogPane().lookupButton(ButtonType.CLOSE); closeButton.managedProperty().bind(closeButton.visibleProperty()); closeButton.setVisible(false); dialog.showAndWait(); }); }
private static void configureFileChooser(final DirectoryChooser fileChooser, String defaultPath) { fileChooser.setTitle("选择文件夹"); if (StringUtils.isNotBlank(defaultPath)) { File file = new File(defaultPath); if (file.exists()) { fileChooser.setInitialDirectory(file); } } }
public void initTreeView() { TreeItem<String> root = new TreeItem<>("近30天记录", new ImageView(rootIcon)); root.setExpanded(true); treeView.setRoot(root);
SpiderHistoryExample example = new SpiderHistoryExample(); SpiderHistoryExample.Criteria criteria = example.createCriteria();
LocalDate thirtyDaysAgo = LocalDate.now().minusDays(30); criteria.andDayGreaterThanOrEqualTo(Integer.parseInt(DateTimeFormatter.BASIC_ISO_DATE.format(thirtyDaysAgo))); example.setOrderByClause("DAY DESC"); List<SpiderHistory> historyList = spiderHistoryMapper.selectByExample(example);
for (SpiderHistory history : historyList) { TreeItem<String> keyWordsNode = new TreeItem<>(history.getKeyWords(), new ImageView(keyWordIcon)); boolean found = false; for (TreeItem<String> dayNode : root.getChildren()) { if (dayNode.getValue().contentEquals("" + history.getDay())) { dayNode.getChildren().add(keyWordsNode); found = true; break; } } if (!found) { TreeItem<String> dayNode = new TreeItem<>( "" + history.getDay(), new ImageView(dayIcon) ); root.getChildren().add(dayNode); dayNode.getChildren().add(keyWordsNode); } }
TreeItem<String> day = new TreeItem<>("Demo", new ImageView(demoIcon)); Arrays.asList("Java", "Python", "JavaScript", "JavaFx", "SpringBoot").forEach(s -> { TreeItem<String> node = new TreeItem<>(s, new ImageView(keyWordIcon)); day.getChildren().add(node); }); root.getChildren().add(day); }
public void treeViewClick() { TreeItem<String> selectedItem = treeView.getSelectionModel().getSelectedItem(); if (null != selectedItem && selectedItem.isLeaf()) { fillData(selectedItem.getValue(), selectedItem.getParent().getValue()); } }
private void fillData(String keyword, String day) { String maxDepth1 = "2"; String htmlThreadNum1 = "2"; String parseThreadNum1 = "2"; String storeThreadNum1 = "2"; String storeLocalPath1 = System.getProperties().getProperty("user.dir"); String storeType1 = StoreType.MYSQL.getType(); String url1 = "https://stackoverflow.com/questions"; if (!"Demo".equals(day)) { SpiderHistoryExample example = new SpiderHistoryExample(); SpiderHistoryExample.Criteria criteria = example.createCriteria(); criteria.andDayEqualTo(Integer.parseInt(day)).andKeyWordsEqualTo(keyword); SpiderHistory history = spiderHistoryMapper.selectByExample(example).get(0); if (null != history) { maxDepth1 = "" + history.getMaxDepth(); htmlThreadNum1 = "" + history.getHtmlThreadNum(); parseThreadNum1 = "" + history.getParseThreadNum(); storeThreadNum1 = "" + history.getStoreThreadNum(); storeLocalPath1 = history.getStoreLocalPath(); storeType1 = history.getStoreType(); url1 = history.getUrl(); } } url.setText(url1); keys.setText(keyword); maxDepth.setText(maxDepth1); htmlThreadNum.setText(htmlThreadNum1); parseThreadNum.setText(parseThreadNum1); storeThreadNum.setText(storeThreadNum1); storeType.getSelectionModel().select(StoreType.valueOf(storeType1)); localPath.setText(storeLocalPath1); }
private void startTask() { Task<Void> task = new Task<Void>() { @Override protected Void call() throws Exception { while (!Spider.isStopping) { Thread.sleep(200); updateMessage(String.format("已爬取页面:%d | 待爬取页面:%d | 待分析页面:%d | 待存储页面:%d", SpiderQueue.getUrlSetSize(), SpiderQueue.getUnVisitedSize(), SpiderQueue.waitingMineSize(), SpiderQueue.getStoreSize())); } done(); return null; } };
statusBar.textProperty().bind(task.messageProperty()); statusBar.progressProperty().bind(task.progressProperty());
task.setOnSucceeded(event -> { statusBar.textProperty().unbind(); statusBar.progressProperty().unbind(); });
new Thread(task).start(); }
public void start() { SpiderHtmlConfig spiderHtmlConfig = SpiderHtmlConfig.builder() .keys(Arrays.asList(StringUtils.replace(keys.getText(), ",", ",").split(","))) .maxDepth(Integer.parseInt(maxDepth.getText())) .minerHtmlThreadNum(Integer.parseInt(htmlThreadNum.getText())) .minerParseThreadNum(Integer.parseInt(parseThreadNum.getText())) .minerStoreThreadNum(Integer.parseInt(storeThreadNum.getText())) .storeType(storeType.getSelectionModel().getSelectedItem()) .storeLocalPath(localPath.getText()) .build();
spider.start(spiderHtmlConfig, url.getText());
startBtn.setDisable(true); startTask(); }
public void stop() { stopBtn.setDisable(true);
spider.stop(); startBtn.setDisable(false); stopBtn.setDisable(false); statusBar.textProperty().unbind(); statusBar.progressProperty().unbind(); statusBar.setProgress(0); } }
|