Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Commit

Permalink
add support of TINAMI (#117)
Browse files Browse the repository at this point in the history
* add support of TINAMI

[TINAMI](https://tinami.com) is one of famous Japanese illustration community for fans of anime and comics like Pixiv.

* fix some bad JavaScript and modify an image URL to use HTTPS
  • Loading branch information
shuuji3 authored and eramdam committed Feb 6, 2017
1 parent 0b775e9 commit d814e9b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -68,6 +68,7 @@
"run-sequence": ">=1.1.2",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": ">=1.1.0",
"vinyl-transform": "^1.0.0"
"vinyl-transform": "^1.0.0",
"xml-js": "^1.0.0"
}
}
1 change: 1 addition & 0 deletions src/js/util/providers/index.js
Expand Up @@ -25,3 +25,4 @@ export { default as yfrog } from './yfrog';
export { default as youtube } from './youtu_be';
export { default as twipple } from './twipple';
export { default as pixiv } from './pixiv';
export { default as tinami } from './tinami';
38 changes: 38 additions & 0 deletions src/js/util/providers/tinami.js
@@ -0,0 +1,38 @@
import convert from 'xml-js';

export default function ($) {
return {
name: 'TINAMI',
setting: 'tinami',
re: /(?:www.tinami.com\/view|tinami.jp\/)/,
default: true,
callback: url => {
let imageId = new URL(url).pathname.split('/').pop();
// If short url, encode ID with base 36
if (url.includes('tinami.jp')) {
imageId = parseInt(imageId, 36);
}
return fetch($.getSafeURL(
`${$.getEnpointFor('tinami')}cont_id=${imageId}&api_key=${$.getKeyFor('tinami')}`))
.then($.statusAndText)
.then(xml => convert.xml2js(xml, {compact: true}))
.then(json => {
// Quit if there is non-public image or no image
if (json.rsp._attributes.stat !== 'ok' ||
json.rsp.content._attributes.type == 'novel') {
return;
}
const image = json.rsp.content.image ||
json.rsp.content.images.image[0] ||
json.rsp.content.images.image;
const imgUrl = $.getSafeURL(image.url._text.replace(
'http://api.tinami.com/', 'https://www.tinami.com/api/'));
return Promise.resolve({
type: 'image',
thumbnail_url: imgUrl,
url: imgUrl,
});
});
},
};
}
14 changes: 13 additions & 1 deletion src/js/util/thumbnails.js
Expand Up @@ -17,6 +17,7 @@ const endpoints = {
twitch: 'https://api.twitch.tv/kraken/',
giphy: 'https://giphy.com/services/oembed?url=',
pixiv: 'http://embed.pixiv.net/embed_json.php?callback=callback&size=medium&id=',
tinami: 'https://www.tinami.com/api/content/info?',
};

let providersSettings;
Expand Down Expand Up @@ -67,6 +68,16 @@ const statusAndJson = res => {
return Promise.reject(new Error(res.statusText));
};

/**
* Function to use in promise that will return the text output of a request
*/
const statusAndText = res => {
if (res.status >= 200 && res.status < 300) {
return res.text();
}
return Promise.reject(new Error(res.statusText));
}

/**
* Returns a promise with image data from noembed
*/
Expand Down Expand Up @@ -103,7 +114,7 @@ const noEmbedVideoCB = url => {
};

// We export a few useful functions for providers
const util = { getKeyFor, statusAndJson, getEnpointFor, getSafeURL, noEmbedVideoCB, noEmbedImgCB };
const util = { getKeyFor, statusAndJson, statusAndText, getEnpointFor, getSafeURL, noEmbedVideoCB, noEmbedImgCB };

const schemeWhitelist = [
Providers.fivehundredpx(util),
Expand Down Expand Up @@ -133,6 +144,7 @@ const schemeWhitelist = [
Providers.universal(util),
Providers.twipple(util),
Providers.pixiv(util),
Providers.tinami(util),
];

const validateUrl = (url) => {
Expand Down

0 comments on commit d814e9b

Please sign in to comment.