cloudroam
4 天以前 46715d892da947c31f07796fdc79dbbef06677b3
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
package com.mzl.flower.schedule;
 
import com.mzl.flower.constant.Constants;
import com.mzl.flower.entity.film.AiContentTaskConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import java.util.Date;
 
import org.springframework.stereotype.Component;
 
@Component
public class TaskExecutor {
 
    private final RestTemplate restTemplate;
 
    @Autowired
    public TaskExecutor(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
 
    public void executeTask(AiContentTaskConfig config) {
        String paramsJson = config.getAiParams();
        System.out.println("发送的 JSON 参数: " + paramsJson); // 添加这行
        System.out.println("定时任务执行 - 时间: " + new Date());
 
        try {
            System.out.println("开始执行调用AI服务: ");
            if(Constants.GENERATOR_CONTENT.film_content.name().equals(config.getType())){
                System.out.println("开始执行调用内容生成服务: ");
                String aiResult = callPythonContentService(paramsJson);
                System.out.println("AI 服务返回结果: " + aiResult);
            }else{
                System.out.println("开始执行调用作品名称生成服务: ");
                String aiResult = callPythonFilmNameService(paramsJson);
                System.out.println("AI 服务返回结果: " + aiResult);
            }
            // 这里处理结果入库逻辑...
        } catch (Exception e) {
            System.err.println("调用 AI 服务失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
 
    public String callPythonFilmNameService(String paramsJson) {
        System.out.println("开始执行调用python crawl: ");
        String url = "http://14.103.144.28:5000/crawl-douban";
 
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        HttpEntity<String> request = new HttpEntity<>(paramsJson, headers);
 
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
 
        return response.getBody();
    }
 
    public String callPythonContentService(String paramsJson) {
        System.out.println("开始执行调用python generate: ");
//        String url = "http://192.168.1.213:5000/generate-film-content";
        String url = "http://14.103.144.28:5000/generate-film-content";
 
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        HttpEntity<String> request = new HttpEntity<>(paramsJson, headers);
 
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
 
        return response.getBody();
    }
}