AdminTriggerController.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.goafanti.admin.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import com.goafanti.common.bo.Result;
  10. import com.goafanti.common.controller.BaseApiController;
  11. import com.goafanti.common.utils.StringUtils;
  12. import com.goafanti.crawler.RequestUtils;
  13. import com.goafanti.crawler.callback.TransitFutureCallback;
  14. import com.goafanti.fragment.service.FragmentService;
  15. @RestController
  16. @RequestMapping(value = "/api/admin/trigger")
  17. public class AdminTriggerController extends BaseApiController {
  18. @Value(value = "${upload.path}")
  19. private String uploadPath = null;
  20. @Autowired
  21. private FragmentService fragmentService;
  22. @RequestMapping(value = "/vacationcrawler", method = RequestMethod.GET)
  23. public Result vacationCrawlerStart(String year) {
  24. Result res = new Result();
  25. if (StringUtils.isBlank(year)) {
  26. return res.error(buildError("", "需要年份信息。"));
  27. }
  28. try {
  29. TransitFutureCallback cb = new TransitFutureCallback("<a.*?href=\"(.*?)\".*?节假日安排.*?</a>");
  30. Map<String, String> json = new HashMap<>();
  31. cb.setResult(json);
  32. RequestUtils.startRequest(
  33. "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",
  34. cb);
  35. if (json.containsKey(year)) {
  36. fragmentService.save("vacations" + year, json.get(year));
  37. }
  38. res.setData(json);
  39. } catch (Exception e) {
  40. res.getError().add(buildError("", e.getMessage()));
  41. }
  42. return res;
  43. }
  44. }