Skip to content

Commit

Permalink
Redo data handling due to additions to SauceNAO API (kudos to Xamayon)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovyerus committed Sep 27, 2017
1 parent 7166c8b commit 41f9e90
Showing 1 changed file with 131 additions and 95 deletions.
226 changes: 131 additions & 95 deletions lib/Sagiri.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,120 @@
const FormData = require('form-data');
const fs = require('fs');

// I didn't know what to call it ¯\_(ツ)_/¯
const SITE_VALUES = {
5: {
name: 'Pixiv',
backupURL: data => `https://www.pixiv.net/member_illust.php?mode=medium&illust_id=${data.data.pixiv_id}`,
URLRegex: /(?:https?:\/\/)?(?:www\.)?pixiv\.net\/member_illust\.php\?mode=.+&illust_id=\d{8}/i
},
8: {
name: 'Nico Nico Seiga',
backupURL: data => `http://seiga.nicovideo.jp/seiga/im${data.data.seiga_id}`,
URLRegex: /(?:http:\/\/)?seiga\.nicovideo\.jp\/seiga\/im\d{7}/i
},
9: {
name: 'Danbooru',
backupURL: data => `https://danbooru.donmai.us/posts/${data.data.danbooru_id}`,
URLRegex: /(?:https?:\/\/)?danbooru\.donmai\.us\/(?:posts|post\/show)\/\d{7}/i
},
10: {
name: 'drawr',
backupURL: data => `http://drawr.net/show.php?id=${data.data.drawr_id}`,
URLRegex: /(?:http:\/\/)?(?:www\.)?drawr\.net\/show\.php\?id=\d{7}/i
},
11: {
name: 'Nijie',
backupURL: data => `http://nijie.info/view.php?id=${data.data.nijie_id}`,
URLRegex: /(?:http:\/\/)?nijie\.info\/view\.php\?id=\d{5}/i
},
12: {
name: 'Yande.re',
backupURL: data => `https://yande.re/post/show/${data.data.yandere_id}`,
URLRegex: /(?:https?:\/\/)?yande\.re\/post\/show\/\d{6}/i
},
16: {
name: 'FAKKU',
backupURL: data => `https://www.fakku.net/hentai/${data.data.source.toLowerCase().replace(' ', '-')}`,
URLRegex: /(?:https?:\/\/)?(www\.)?fakku\.net\/hentai\/[a-z-]+\d{10}/i
},
18: {
name: 'nHentai',
backupURL: data => `https://nhentai.net/g/${data.header.thumbnail.match(/nhentai\/(\d+)/)[1]}`,
URLRegex: /(?:)/i
},
19: {
name: '2D-Market',
backupURL: data => `http://2d-market.com/Comic/${data.header.thumbnail.match(/2d_market\/(\d+)/i)[1]}-${data.data.source.replace(' ', '-')}`,
URLRegex: /(?:https?:\/\/)2d-market\.com\/Comic\/\d+/i
},
20: {
name: 'MediBang',
backupURL: data => data.data.url,
URLRegex: /(?:https?:\/\/)?medibang\.com\/picture\/[a-z0-9]+/i
},
21: {
name: 'AniDB',
backupURL: data => `https://anidb.net/perl-bin/animedb.pl?show=anime&aid=${data.data.anidb_aid}`,
URLRegex: /(?:https?:\/\/)?anidb\.net\/perl-bin\/animedb\.pl\?show=.+&aid=\d{4}/i
},
25: {
name: 'Gelbooru',
backupURL: data => `https://gelbooru.com/index.php?page=post&s=view&id=${data.data.gelbooru_id}`,
URLRegex: /(?:https?:\/\/)gelbooru\.com\/index\.php\?page=post&s=view&id=\d{7}/i
},
26: {
name: 'Konachan',
backupURL: data => `https://konachan.com/post/show/${data.data.konachan_id}`,
URLRegex: /(?:http:\/\/)?konachan\.com\/post\/show\/\d{6}/i
},
27: {
name: 'Sankaku Channel',
backupURL: data => `https://chan.sankakucomplex.com/post/show/${data.data.sankaku_id}`,
URLRegex: /(?:https?:\/\/)?chan\.sankakucomplex\.com\/post\/show\/\d{7}/i
},
28: {
name: 'Anime-Pictures',
backupURL: data => `https://anime-pictures.net/pictures/view_post/${data.data['anime-pictures_id']}`,
URLRegex: /(?:https?:\/\/)?anime-pictures\.net\/pictures\/view_post\/\d{6}/i
},
29: {
name: 'e621',
backupURL: data => `https://e621.net/post/show/${data.data.e621_id}`,
URLRegex: /(?:https?:\/\/)?e621\.net\/post\/show\/\d{6}/i
},
30: {
name: 'Idol Complex',
backupURL: data => `https://idol.sankakucomplex.com/post/show/${data.data.idol_id}`,
URLRegex: /(?:https?:\/\/)?idol\.sankakucomplex\.com\/post\/show\/\d{6}/i
},
31: {
name: 'bcy.net Illust',
backupURL: data => `https://bcy.net/${data.data.bcy_type}/detail/${data.data.member_link_id}/${data.data.bcy_id}`,
URLRegex: /(?:http:\/\/)?bcy.net\/illust\/detail\/\d{5}/i
},
32: {
name: 'bcy.net Cosplay',
backupURL: data => `https://bcy.net/${data.data.bcy_type}/detail/${data.data.member_link_id}/${data.data.bcy_id}`,
URLRegex: /(?:http:\/\/)?bcy.net\/coser\/detail\/\d{5}/i
},
33: {
name: 'PortalGraphics',
backupURL: data => `http://web.archive.org/web/http://www.portalgraphics.net/pg/illust/?image_id=${data.data.pg_id}`,
URLRegex: /(?:http:\/\/)?web\.archive\.org\/web\/http:\/\/www\.portalgraphics\.net\/pg\/illust\/\?image_id=\d{5}/i
},
34: {
name: 'deviantArt',
backupURL: data => `https://deviantart.com/view/${data.data.da_id}`,
URLRegex: /(?:https:\/\/)?deviantart\.com\/view\/\d{8}/i
},
35: {
name: 'Pawoo',
backupURL: data => `https://pawoo.net/@${data.data.user_acct}/${data.data.pawoo_id}`,
URLRegex: /(?:https?:\/\/)?pawoo\.net\/@.+/i
}
};

/**
* Query handler for SauceNAO.
*
Expand Down Expand Up @@ -59,9 +173,13 @@ class SauceHandler {
else if (res.length === 1) result = res[0];
else throw new Error('No results.');

let data = resolveSauceData(result);

return {
url: data.url,
site: data.name,
index: data.id,
similarity: Number(result.header.similarity),
url: resolveSauceURL(result),
original: result
};
}).then(resolve).catch(reject);
Expand Down Expand Up @@ -94,104 +212,22 @@ function sendForm(form) {
});
}

// I could shorten this to hell and back but it'll do for now.
function resolveSauceURL(data) {
function resolveSauceData(data) {
let body = data.data;
let id = data.header.index_name.match(/^Index #(\d+):? /)[1];
let name = SITE_VALUES[id].name;
let url;

switch (data.header.index_name.match(/^Index #(\d+):? /)[1]) {
case '5':
// Pixiv
url = `https://www.pixiv.net/member_illust.php?mode=medium&illust_id=${data.data.pixiv_id}`;
break;
case '8':
// Nico Nico Seiga
url = `http://seiga.nicovideo.jp/seiga/im${data.data.seiga_id}`;
break;
case '9':
// Danbooru
url = `https://danbooru.donmai/us/posts/${data.data.danbooru_id}`;
break;
case '10':
// drawr
url = `http://drawr.net/show.php?id=${data.data.drawr_id}`;
break;
case '11':
// Nijie
url = `http://nijie.info/view.php?id=${data.data.nijie_id}`;
break;
case '12':
// Yande.re
url = `https://yande.re/post/show/${data.data.yandere_id}`;
break;
case '16':
// FAKKU
url = `https://www.fakku.net/hentai/${data.data.source.toLowerCase().replace(' ', '-')}`;
break;
case '18':
// H-Misc
url = `https://nhentai.net/g/${data.header.thumbnail.match(/nhentai\/(\d+)/)[1]}`;
break;
case '19':
// 2D-Market
url = `http://2d-market.com/Comic/${data.header.thumbnail.match(/2d_market\/(\d+)/i)[1]}-${data.data.source.replace(' ', '-')}`;
break;
case '20':
// MediBang
url = data.data.url;
break;
case '21':
// Anime
url = `https://anidb.net/perl-bin/animedb.pl?show=anime&aid=${data.data.anidb_aid}`;
break;
case '25':
// Gelbooru
url = `https://gelbooru.com/index.php?page=post&s=view&id=${data.data.gelbooru_id}`;
break;
case '26':
// Konachan
url = `https://konachan.com/post/show/${data.data.konachan_id}`;
break;
case '27':
// Sankaku Channel
// TODO: Find a reliable way to differentiate between channel and idol pics.
url = `https://chan.sankakucomplex.com/post/show/${data.data.sankaku_id}`;
break;
case '28':
// Anime-Pictures
url = `https://anime-pictures.net/pictures/view_post/${data.data['anime-pictures_id']}`;
break;
case '29':
// e621
url = `https://e621.net/post/show/${data.data.e621_id}`;
break;
case '30':
// Idol Complex
url = `https://idol.sankakucomplex.com/post/show/${data.data.sankaku_id}`;
break;
case '31':
// bcy.net Illust
url = `https://bcy.net/${data.data.bcy_type}/detail/${data.data.member_link_id}/${data.data.bcy_id}`;
break;
case '32':
// bcy.net Cosplay
url = `https://bcy.net/${data.data.bcy_type}/detail/${data.data.member_link_id}/${data.data.bcy_id}`;
break;
case '33':
// PortalGraphics
break;
case '34':
// deviantArt
url = `https://${data.data.author_url}/art/${data.data.title.replace(/=/g, '').replace(/ - /g, '-').replace(/ /g, '-')}-${data.data.da_id}`;
break;
case '35':
// Pawoo
url = `https://pawoo.net/@${data.data.user_acct}/${data.data.pawoo_id}`;
break;
default:
throw new Error(`Unsupported site index: ${data.header.index_name.match(/^Index #(\d+):? /)[1]}`);
if (body.ext_urls[0] && body.ext_urls.length > 1) {
url = body.ext_urls.filter(url => SITE_VALUES[id].URLRegex.test(url))[0];
} else if (body.ext_urls[0] && body.ext_urls.length === 1) {
url = body.ext_urls[0];
}

return url;
// Use fallback generation method.
if (!url) url = SITE_VALUES[id].backupURL(data);

return {id, url, name};
}

module.exports = SauceHandler;

0 comments on commit 41f9e90

Please sign in to comment.