|
|
@@ -1,11 +1,9 @@
|
|
|
package com.goafanti.admin.controller;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileWriter;
|
|
|
-import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
@@ -13,21 +11,28 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import com.goafanti.common.bo.Result;
|
|
|
import com.goafanti.common.controller.BaseApiController;
|
|
|
-import com.goafanti.common.utils.FileUtils;
|
|
|
-import com.goafanti.common.utils.LoggerUtils;
|
|
|
+import com.goafanti.common.model.HtmlFragment;
|
|
|
+import com.goafanti.common.utils.StringUtils;
|
|
|
import com.goafanti.crawler.RequestUtils;
|
|
|
import com.goafanti.crawler.callback.TransitFutureCallback;
|
|
|
+import com.goafanti.fragment.service.FragmentService;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/api/admin/trigger")
|
|
|
public class AdminTriggerController extends BaseApiController {
|
|
|
|
|
|
@Value(value = "${upload.path}")
|
|
|
- private String uploadPath = null;
|
|
|
+ private String uploadPath = null;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FragmentService fragmentService;
|
|
|
|
|
|
@RequestMapping(value = "/vacationcrawler", method = RequestMethod.GET)
|
|
|
- public Result vacationCrawlerStart(String id) {
|
|
|
+ public Result vacationCrawlerStart(String year) {
|
|
|
Result res = new Result();
|
|
|
+ if (StringUtils.isBlank(year)) {
|
|
|
+ return res.error(buildError("", "需要年份信息。"));
|
|
|
+ }
|
|
|
try {
|
|
|
TransitFutureCallback cb = new TransitFutureCallback("<a.*?href=\"(.*?)\".*?节假日安排.*?</a>");
|
|
|
Map<String, String> json = new HashMap<>();
|
|
|
@@ -35,32 +40,17 @@ public class AdminTriggerController extends BaseApiController {
|
|
|
RequestUtils.startRequest(
|
|
|
"http://sousuo.gov.cn/list.htm?n=40&p=0&t=paper&sort=pubtime&subchildtype=gc189&location=%E7%BB%BC%E5%90%88%E6%94%BF%E5%8A%A1%E5%85%B6%E4%BB%96&timetype=timeqb",
|
|
|
cb);
|
|
|
- for (String j : json.keySet()) {
|
|
|
- saveToFile("/json/vacations/" + j + ".json", json.get(j));
|
|
|
+ if (json.containsKey(year)) {
|
|
|
+ String id = "vacations" + year;
|
|
|
+ HtmlFragment hf = new HtmlFragment();
|
|
|
+ hf.setContent(json.get(year));
|
|
|
+ hf.setId(id);
|
|
|
+ fragmentService.save(id, hf);
|
|
|
}
|
|
|
+ res.setData(json);
|
|
|
} catch (Exception e) {
|
|
|
res.getError().add(buildError("", e.getMessage()));
|
|
|
}
|
|
|
return res;
|
|
|
}
|
|
|
-
|
|
|
- private void saveToFile(String filePath, String s) {
|
|
|
- File file = new File(uploadPath + filePath);
|
|
|
- file.getParentFile().mkdirs();
|
|
|
- FileWriter fw = null;
|
|
|
- try {
|
|
|
- fw = new FileWriter(file);
|
|
|
- fw.write(s);
|
|
|
- } catch (IOException e) {
|
|
|
- LoggerUtils.error(FileUtils.class, e.getMessage(), e);
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (fw != null) {
|
|
|
- fw.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- fw = null;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|