diff --git a/PicSearcherApp.js b/PicSearcherApp.js index 12bcd33..97ebe7a 100644 --- a/PicSearcherApp.js +++ b/PicSearcherApp.js @@ -5,6 +5,67 @@ exports.processMessageImages = processMessageImages; exports.getFileBlob = getFileBlob; const App_1 = require("@rocket.chat/apps-engine/definition/App"); const messages_1 = require("@rocket.chat/apps-engine/definition/messages"); +const settings_1 = require("@rocket.chat/apps-engine/definition/settings"); +var SourceType; +(function (SourceType) { + SourceType["Pixiv"] = "Pixiv"; + SourceType["DeviantArt"] = "DeviantArt"; + SourceType["AniDB"] = "AniDB"; + SourceType["MyAnimeList"] = "MyAnimeList"; + SourceType["AniList"] = "AniList"; + SourceType["BCY"] = "BCY"; + SourceType["EHentai"] = "E-Hentai"; + SourceType["IMDB"] = "IMDB"; + SourceType["Twitter"] = "Twitter"; + SourceType["Danbooru"] = "Danbooru"; + SourceType["YandeRe"] = "Yande.re"; + SourceType["Gelbooru"] = "Gelbooru"; + SourceType["AnimePictures"] = "AnimePictures"; + SourceType["Unknown"] = "Unknown"; +})(SourceType || (SourceType = {})); +function getUrlType(url) { + const lowerUrl = url.toLowerCase(); + if (lowerUrl.includes('pixiv.net')) { + return SourceType.Pixiv; + } + if (lowerUrl.includes('twitter.com') || lowerUrl.includes('x.com')) { + return SourceType.Twitter; + } + if (lowerUrl.includes('danbooru.donmai.us')) { + return SourceType.Danbooru; + } + if (lowerUrl.includes('yande.re')) { + return SourceType.YandeRe; + } + if (lowerUrl.includes('gelbooru.com')) { + return SourceType.Gelbooru; + } + if (lowerUrl.includes('anime-pictures.net')) { + return SourceType.AnimePictures; + } + if (lowerUrl.includes('deviantart.com')) { + return SourceType.DeviantArt; + } + if (lowerUrl.includes('anidb.net')) { + return SourceType.AniDB; + } + if (lowerUrl.includes('myanimelist.net')) { + return SourceType.MyAnimeList; + } + if (lowerUrl.includes('anilist.co')) { + return SourceType.AniList; + } + if (lowerUrl.includes('bcy.net')) { + return SourceType.BCY; + } + if (lowerUrl.includes('e-hentai.org')) { + return SourceType.EHentai; + } + if (lowerUrl.includes('imdb.com')) { + return SourceType.IMDB; + } + return SourceType.Unknown; +} const defaultSauceNAOResponse = { header: { user_id: '', @@ -14,7 +75,7 @@ const defaultSauceNAOResponse = { long_remaining: 0, short_remaining: 0, status: 0, - results_requested: 0, + results_requested: '', index: {}, search_depth: '', minimum_similarity: 0, @@ -30,10 +91,12 @@ class PicSearcherApp extends App_1.App { constructor(info, logger, accessors) { super(info, logger, accessors); this.rootUrl = ''; + this.sauceNAOApiKey = ''; myLogger = logger; } async initialize(configurationExtend, environmentRead) { this.rootUrl = await environmentRead.getEnvironmentVariables().getValueByName('ROOT_URL'); + this.sauceNAOApiKey = await environmentRead.getSettings().getValueById('SauceNAOApiKey'); myLogger.log('rootUrl:', this.rootUrl); return super.initialize(configurationExtend, environmentRead); } @@ -47,9 +110,26 @@ class PicSearcherApp extends App_1.App { } myLogger.info('message.attachments:', message.attachments); const imageBlobs = await processMessageImages(message, http, read); - const result = await searchImageOnSauceNAO(imageBlobs[0].blob, imageBlobs[0].suffix); + const result = await searchImageOnSauceNAO(this.sauceNAOApiKey, imageBlobs[0].blob, imageBlobs[0].suffix); await sendSearchResults(modify, message.room, result, author); } + async onSettingUpdated(setting, configurationModify, read, http) { + if (setting.id === 'SauceNAOApiKey') { + this.sauceNAOApiKey = setting.value; + } + return super.onSettingUpdated(setting, configurationModify, read, http); + } + async extendConfiguration(configuration, environmentRead) { + await configuration.settings.provideSetting({ + id: 'SauceNAOApiKey', + type: settings_1.SettingType.STRING, + packageValue: '', + required: true, + public: false, + i18nLabel: 'searcher_api_key_label', + i18nDescription: 'searcher_api_key_description', + }); + } async sendMessage(textMessage, room, author, modify) { const messageBuilder = modify.getCreator().startMessage({ text: textMessage, @@ -92,79 +172,21 @@ async function getFileBlob(fileId, read) { } return new Blob(); } -async function searchImageOnSauceNAO(image, suffix) { +async function searchImageOnSauceNAO(apiKey, image, suffix) { const formData = new FormData(); formData.append('file', image, 'image.' + suffix); + formData.append('api_key', apiKey); + formData.append('output_type', '2'); try { const response = await fetch('https://saucenao.com/search.php', { method: 'POST', body: formData, }); if (response.ok) { - const results = []; - const html = await response.text(); - const resultBlocks = html.match(/