Search in sources :

Example 1 with PicUrlToBase64DTO

use of cn.dubidubi.model.dto.PicUrlToBase64DTO in project dubidubi by lzzzz4.

the class MailJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    // 得到传入的jobDataMap,每次执行任务需要 图片的地址与发件人的地址
    JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
    String str = jobDataMap.getString("str");
    try (ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(Base64.decodeBase64(str));
        ObjectInputStream objectInputStream = new ObjectInputStream(arrayInputStream)) {
        PicUrlToBase64DTO toBase64DTO = (PicUrlToBase64DTO) objectInputStream.readObject();
        List<String> mail = toBase64DTO.getMail();
        int length = mail.size();
        for (int i = 0; i < length; i++) {
            MailDTO dto = new MailDTO();
            dto.setMail(mail.get(i));
            dto.setTitle("欢迎订阅图片服务");
            // 得到所有的图片链接
            List<String> list = toBase64DTO.getList();
            StringBuilder builder = new StringBuilder();
            for (String string : list) {
                builder.append("<img src='" + string + "' />");
            }
            dto.setContent(builder.toString());
            MailUtils.sendPicMail(dto);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) PicUrlToBase64DTO(cn.dubidubi.model.dto.PicUrlToBase64DTO) ByteArrayInputStream(java.io.ByteArrayInputStream) MailDTO(cn.dubidubi.model.base.dto.MailDTO) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with PicUrlToBase64DTO

use of cn.dubidubi.model.dto.PicUrlToBase64DTO in project dubidubi by lzzzz4.

the class PyBeautifulGirlServiceImpl method waitForComplete.

@Async
@Override
public void waitForComplete(Future<PicUrlToBase64DTO> future) throws InterruptedException, ExecutionException, IOException {
    while (true) {
        // 完成的话获取其中的值,并将该值存入quartz中
        if (future.isDone()) {
            PicUrlToBase64DTO base64dto = future.get();
            // 获得目标邮箱
            base64dto.setMail(mailInfoMapper.listMailByOpenid(base64dto.getOpenId()));
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(arrayOutputStream);
            objectOutputStream.writeObject(base64dto);
            // 得到了对象的序列化数据
            String str = Base64.encodeBase64URLSafeString(arrayOutputStream.toByteArray());
            String openId = base64dto.getOpenId();
            Quartz.addJob(randomString(openId, "job"), randomString(openId, "jobGroup"), randomString(openId, "trigger"), randomString(openId, "triggerGroup"), MailJob.class, base64dto.getCron(), "str", str);
            break;
        }
    }
}
Also used : PicUrlToBase64DTO(cn.dubidubi.model.dto.PicUrlToBase64DTO) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Async(org.springframework.scheduling.annotation.Async)

Example 3 with PicUrlToBase64DTO

use of cn.dubidubi.model.dto.PicUrlToBase64DTO in project dubidubi by lzzzz4.

the class PyBeautifulGirlServiceImpl method startPy.

@Override
@Async
public Future<PicUrlToBase64DTO> startPy(String word, String uid, String time) {
    List<String> list = null;
    try {
        word = URLEncoder.encode(word, "utf-8");
        String pyPath = this.getClass().getClassLoader().getResource("py/jiumei.py").getPath();
        // 得到项目路径
        String webPath = this.getClass().getResource("/").getPath();
        String[] cmd = { "python", StringUtils.substring(pyPath, 1), word, StringUtils.substring(webPath, 1, webPath.indexOf("WEB-INF")) + "pic/", uid };
        Process process = Runtime.getRuntime().exec(cmd);
        InputStream inputStream = process.getInputStream();
        list = IOUtils.readLines(inputStream);
        process.waitFor();
        IOUtils.closeQuietly(inputStream);
    } catch (UnsupportedEncodingException e) {
        LoggerFactory.getLogger(this.getClass()).warn("关键词encode错误");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    PicUrlToBase64DTO base64dto = new PicUrlToBase64DTO();
    base64dto.setList(list);
    base64dto.setOpenId(uid);
    base64dto.setCron(dateToCron(time));
    return new AsyncResult<PicUrlToBase64DTO>(base64dto);
}
Also used : PicUrlToBase64DTO(cn.dubidubi.model.dto.PicUrlToBase64DTO) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Async(org.springframework.scheduling.annotation.Async)

Example 4 with PicUrlToBase64DTO

use of cn.dubidubi.model.dto.PicUrlToBase64DTO in project dubidubi by lzzzz4.

the class GetPicByKeyController method key.

/**
 * @throws IOException
 * @throws ExecutionException
 * @throws InterruptedException
 * @Description: 界面输入关键词,执行爬虫,异步将内容发送至用户的邮箱
 * @data :@param getPicDTO
 * @data :@param file
 * @data :@param request
 * @data :@return
 * @date :2018年3月20日下午12:55:12
 */
@RequestMapping("/key")
@ResponseBody
public AjaxResultDTO key(GetPicDTO getPicDTO, @RequestParam(required = false) MultipartFile file, HttpServletRequest request) throws InterruptedException, ExecutionException, IOException {
    AjaxResultDTO resultDTO = new AjaxResultDTO();
    WxInfoDTO wxInfoDTO = (WxInfoDTO) request.getSession().getAttribute("user");
    if (mailInfoMapper.listMailByOpenid(wxInfoDTO.getOpenid()) == null || mailInfoMapper.listMailByOpenid(wxInfoDTO.getOpenid()).size() == 0) {
        resultDTO.setCode(500);
    }
    // 当为九妹源时
    if ("2".equals(getPicDTO.getSource())) {
        // System.out.println("九妹源");
        resultDTO.setUid(wxInfoDTO.getOpenid());
        Future<PicUrlToBase64DTO> list = pyBeautifuGirlSerivce.startPy(getPicDTO.getKey(), wxInfoDTO.getOpenid(), getPicDTO.getTime());
        pyBeautifuGirlSerivce.waitForComplete(list);
        resultDTO.setUrl("/9mei_suc.html");
        resultDTO.setCode(9);
    } else {
        // System.out.println("百度源");
        if (getPicDTO.getAmount() == null) {
            getPicDTO.setAmount(4);
        }
        int pn = RandomUtils.nextInt(0, 10) * getPicDTO.getAmount();
        // 链接 标题为一组
        List<String> strings = pyBeautifuGirlSerivce.startPy(0, 0, getPicDTO.getKey(), pn, getPicDTO.getAmount());
        PicUrlToBase64DTO picUrlToBase64DTO = new PicUrlToBase64DTO();
        picUrlToBase64DTO.setOpenId(wxInfoDTO.getOpenid());
        picUrlToBase64DTO.setList(strings);
        picUrlToBase64DTO.setMail(mailInfoMapper.listMailByOpenid(wxInfoDTO.getOpenid()));
        // 将对象序列化为base64字符串
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(arrayOutputStream);
        objectOutputStream.writeObject(picUrlToBase64DTO);
        // 将序列化的对象存入job中执行
        String str = Base64.encodeBase64URLSafeString(arrayOutputStream.toByteArray());
        Quartz.addJob(pyBeautifuGirlSerivce.randomString(wxInfoDTO.getOpenid(), "job"), pyBeautifuGirlSerivce.randomString(wxInfoDTO.getOpenid(), "jobgroup"), pyBeautifuGirlSerivce.randomString(wxInfoDTO.getOpenid(), "trigger"), pyBeautifuGirlSerivce.randomString(wxInfoDTO.getOpenid(), "triggergroup"), BaiduMailJob.class, pyBeautifuGirlSerivce.dateToCron(getPicDTO.getTime()), "str", str);
        resultDTO.setCode(200);
        resultDTO.setUrl("/9mei_suc.html");
        IOUtils.closeQuietly(arrayOutputStream);
        IOUtils.closeQuietly(objectOutputStream);
    }
    // System.out.println(resultDTO);
    return resultDTO;
}
Also used : PicUrlToBase64DTO(cn.dubidubi.model.dto.PicUrlToBase64DTO) AjaxResultDTO(cn.dubidubi.model.base.dto.AjaxResultDTO) WxInfoDTO(cn.dubidubi.model.dto.WxInfoDTO) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with PicUrlToBase64DTO

use of cn.dubidubi.model.dto.PicUrlToBase64DTO in project dubidubi by lzzzz4.

the class BaiduMailJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    JobDetail detail = context.getJobDetail();
    JobDataMap jobDataMap = detail.getJobDataMap();
    String str = jobDataMap.getString("str");
    try (ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(str));
        ObjectInputStream inputStream = new ObjectInputStream(in)) {
        PicUrlToBase64DTO pic = (PicUrlToBase64DTO) inputStream.readObject();
        System.out.println(pic);
        List<String> list = pic.getMail();
        List<String> picList = pic.getList();
        System.out.println(picList);
        MailDTO dto = new MailDTO();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < picList.size(); i++) {
            // 为奇数
            if (i % 2 == 0 || i == 0) {
                builder.append("<img src='").append(picList.get(i)).append("'/><br/>");
            } else {
                builder.append("<h2>").append(picList.get(i)).append("</h2>");
            }
        }
        // 为订阅者的openid每个邮箱发送邮件
        for (String string : list) {
            System.out.println(string);
            dto.setMail(string);
            dto.setTitle("百度图片");
            // 发送所有的图片链接
            dto.setContent(builder.toString());
            MailUtils.sendPicMail(dto);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) PicUrlToBase64DTO(cn.dubidubi.model.dto.PicUrlToBase64DTO) ByteArrayInputStream(java.io.ByteArrayInputStream) MailDTO(cn.dubidubi.model.base.dto.MailDTO) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

PicUrlToBase64DTO (cn.dubidubi.model.dto.PicUrlToBase64DTO)5 IOException (java.io.IOException)3 MailDTO (cn.dubidubi.model.base.dto.MailDTO)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 JobDataMap (org.quartz.JobDataMap)2 Async (org.springframework.scheduling.annotation.Async)2 AjaxResultDTO (cn.dubidubi.model.base.dto.AjaxResultDTO)1 WxInfoDTO (cn.dubidubi.model.dto.WxInfoDTO)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JobDetail (org.quartz.JobDetail)1 AsyncResult (org.springframework.scheduling.annotation.AsyncResult)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1