-
Notifications
You must be signed in to change notification settings - Fork 0
/
AskPlantnet.js
118 lines (109 loc) · 5.42 KB
/
AskPlantnet.js
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
import {clone, isSet, loadJsonResource} from "../lib/Common.js";
import {firstImageOf, postImageOf, postInfoOf, postLinkOf, postTextOf} from "../domain/post.js";
import {IDENTIFY_RESULT, PLANTNET_MINIMAL_PERCENT} from "../servicesExternal/PlantnetApiService.js";
export default class AskPlantnet {
constructor(config, loggerService, blueskyService, pluginsCommonService, plantnetCommonService, plantnetApiService) {
this.isAvailable = false;
this.logger = loggerService.getLogger().child({label: 'AskPl@ntNet'});
this.blueskyService = blueskyService;
this.plantnetSimulate = (config.bot.plantnetSimulate === true);
this.pluginsCommonService = pluginsCommonService;
this.plantnetCommonService = plantnetCommonService;
this.plantnetApiService = plantnetApiService;
try {
this.asks = loadJsonResource('src/data/askPlantnet.json');
this.isAvailable = plantnetApiService.isReady();
this.logger.info((this.isAvailable ? "available" : "not available") +
" with " + this.asks.length + " asks");
} catch (exception) {
pluginsCommonService.logError("init", exception);
}
}
getName() {
return "AskPlantnet";
}
getPluginTags() {
return ["#BeSAskPlantnet", "#IndentificationDePlantes"].join(' ');
}
getQuestions() {
return clone(this.asks);
}
isReady() {
return this.isAvailable;
}
async process(config) {
const pluginName = this.getName();
let {doSimulate, simulateIdentifyCase, context} = config;
const {
plantnetSimulate, pluginsCommonService, plantnetCommonService, plantnetApiService, blueskyService,
logger, "asks": questions
} = this;
const doSimulateIdentify = plantnetSimulate || isSet(simulateIdentifyCase);// if at least one want to simulate then simulate
let candidate = null;
let step = "searchNextCandidate";
try {
const maxHoursOld = 72;
const hasNoReply = false;
const hasNoReplyFromBot = true;
const threadGetLimited = true;
// keep hasImages=false as this is mention post's parent which include flower image
const candidate = await pluginsCommonService.searchNextCandidate({...config, questions, maxHoursOld,
hasNoReply, hasNoReplyFromBot, threadGetLimited});
if (candidate === null) {
return pluginsCommonService.resultNoCandidate(pluginName, context);
}
step = "getParentPostOf";
const parentPost = await blueskyService.getParentPostOf(candidate.uri);
if (parentPost === null) {
return await pluginsCommonService.resultNoCandidateParent(candidate, pluginName, context);
}
// logger.info(parentPost);
logger.debug(`CANDIDATE's PARENT:${parentPost ? postTextOf(parentPost) : "NONE"}`);
step = "firstImageOf";
const parentPhoto = firstImageOf(parentPost);
if (!parentPhoto) {
return await pluginsCommonService.rejectNoCandidateParentImage(candidate, parentPost, pluginName, context);
}
logger.debug(`post Candidate Parent : ${postLinkOf(parentPost)}\n` +
`\t${postInfoOf(parentPost)}\n` +
`\t${postImageOf(parentPhoto)}`, context);
step = "plantnetIdentify";
const tags = this.getPluginTags();
const identifyOptions = {
"imageUrl": parentPhoto?.fullsize,
doSimulate,
doSimulateIdentify,
simulateIdentifyCase,
candidate,
tags,
context
};
const {
result,
plantnetResult = null
} = await plantnetApiService.plantnetIdentify(identifyOptions);
step = "handle plantnetIdentification response";
if (result === IDENTIFY_RESULT.OK) {
const {scoredResult, firstImageOriginalUrl, firstImageText} = plantnetResult;
return await plantnetCommonService.replyToWithIdentificationResult(candidate,
{tags, doSimulate, context},
{scoredResult, firstImageOriginalUrl, firstImageText}
);
} else if (result === IDENTIFY_RESULT.BAD_SCORE) {
return await pluginsCommonService.handleWithoutScoredResult(pluginName, PLANTNET_MINIMAL_PERCENT,
{doSimulate, "candidate": parentPost, "replyTo": candidate, "muteAuthor": false, context}
);
} else {
if (result !== IDENTIFY_RESULT.NONE) {
logger.warn(`unable to handle plantnetService.plantnetIdentify result:${result} so consider it as NONE`);
}
return await pluginsCommonService.handleWithNoIdentificationResult(pluginName,
{doSimulate, "candidate": parentPost, "replyTo": candidate, "muteAuthor": false, context}
);
}
} catch (err) {
// DEBUG / console.log("stack >>", err.stack);// print stack
return pluginsCommonService.rejectWithParentIdentifyError(step, candidate, pluginName, err, context);
}
}
}