botyo-command-reverse-image-search
Version:
Reverse Image Search command for Botyo
104 lines • 4.81 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const botyo_api_1 = require("botyo-api");
const GooglUrlShortener_1 = require("./shortener/GooglUrlShortener");
const IdentityUrlShortener_1 = require("./shortener/IdentityUrlShortener");
class ReverseImageSearchCommand extends botyo_api_1.AbstractCommandModule {
constructor() {
super();
const config = this.getRuntime().getConfiguration();
const shouldShorten = config.getOrElse("shortenUrls", false);
if (shouldShorten) {
const googlApiKey = config.getProperty("googlApiKey");
this.shortener = new GooglUrlShortener_1.default(googlApiKey);
}
else {
this.shortener = new IdentityUrlShortener_1.default();
}
this.recentMessagesCount = config.getOrElse("recentMessagesCount", 20);
}
getCommand() {
return ["reverse", "ris", "whosthis", "whodis", "whatsthis"];
}
getDescription() {
return "Runs a reverse image search on the last picture";
}
getUsage() {
return "";
}
validate(msg, args) {
return true;
}
execute(msg, args) {
return __awaiter(this, void 0, void 0, function* () {
const lastPhotoUrl = yield this.getLastPhotoUrl(msg);
if (lastPhotoUrl) {
const message = yield this.getResultMessage(lastPhotoUrl);
return this.getRuntime().getChatApi().sendMessage(msg.threadID, message);
}
return this.getRuntime().getChatApi().sendMessage(msg.threadID, "I can't see any photos :/");
});
}
getLastPhotoUrl(msg) {
return __awaiter(this, void 0, void 0, function* () {
const history = yield this.getRuntime().getChatApi()
.getThreadHistory(msg.threadID, this.recentMessagesCount);
const photos = history
.filter(m => m.attachments.length > 0)
.filter(m => m.attachments.every((a) => a.type == "photo" || a.type == "image"))
.sort((m1, m2) => m2.timestamp - m1.timestamp);
if (photos.length === 0) {
return;
}
const lastPhoto = photos[0].attachments[0];
let url;
try {
url = yield this.getRuntime().getChatApi().resolvePhotoUrl(lastPhoto.attachmentID);
}
catch (err) {
url = lastPhoto.hiresUrl || lastPhoto.largePreviewUrl || lastPhoto.previewUrl || lastPhoto.thumbnailUrl;
}
if (!url)
throw new Error("Could not get photo's URL");
return url;
});
}
getResultMessage(imageUrl) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield Promise.all([
this.shortener.shorten(ReverseImageSearchCommand.getGoogleUrl(imageUrl)),
this.shortener.shorten(ReverseImageSearchCommand.getBingUrl(imageUrl)),
this.shortener.shorten(ReverseImageSearchCommand.getTinEyeUrl(imageUrl))
]);
const googleShortUrl = result[0];
const bingShortUrl = result[1];
const tinEyeShortUrl = result[2];
let text = "\u{1F50D} Reverse image search:\n\n";
text += "\u{1F516} Google: " + googleShortUrl + " \n";
text += "\u{1F516} Bing: " + bingShortUrl + " \n";
text += "\u{1F516} TinEye: " + tinEyeShortUrl;
return text;
});
}
static getGoogleUrl(url) {
return "https://images.google.com/searchbyimage?image_url=" + encodeURIComponent(url);
}
static getBingUrl(url) {
return "https://www.bing.com/images/search?q=imgurl:"
+ encodeURIComponent(url)
+ "&view=detailv2&selectedIndex=0&pageurl=&mode=ImageViewer&iss=sbi";
}
static getTinEyeUrl(url) {
return "https://tineye.com/search?url=" + encodeURIComponent(url);
}
}
exports.default = ReverseImageSearchCommand;
//# sourceMappingURL=ReverseImageSearchCommand.js.map
;