Untitled diff

Creado El diff nunca expira
0 eliminaciones
176 líneas
12 adiciones
184 líneas
'use strict';
'use strict';


var _ = require('lodash');
var _ = require('lodash');
var Generic = require('butter-provider');
var Generic = require('butter-provider');
var inherits = require('util').inherits;
var inherits = require('util').inherits;
var Q = require('q');
var Q = require('q');
var querystring = require('querystring');
var querystring = require('querystring');
var request = require('request');
var request = require('request');
var sanitize = require('butter-sanitize');
var sanitize = require('butter-sanitize');


var AnimeApi = function (args) {
var AnimeApi = function (args) {
var that = this;
var that = this;


AnimeApi.super_.call(this);
AnimeApi.super_.call(this);


if (args.apiURL) {
if (args.apiURL) {
this.apiURL = args.apiURL.split(',');
this.apiURL = args.apiURL.split(',');
}
}
};
};


inherits(AnimeApi, Generic);
inherits(AnimeApi, Generic);


AnimeApi.prototype.config = {
AnimeApi.prototype.config = {
name: 'AnimeApi',
name: 'AnimeApi',
uniqueId: 'mal_id',
uniqueId: 'mal_id',
tabName: 'AnimeApi',
tabName: 'AnimeApi',
type: 'anime',
type: 'anime',
metadata: 'trakttv:anime-metadata'
metadata: 'trakttv:anime-metadata'
};
};


function formatFetch(animes) {
function formatFetch(animes) {
var results = _.map(animes, function (anime) {
var results = _.map(animes, function (anime) {
return {
return {
images: anime.images,
// images: anime.images,
images: {
poster: 'https://media.kitsu.io/anime/poster_images/' + anime._id + '/large.jpg',
fanart: 'https://media.kitsu.io/anime/cover_images/' + anime._id + '/original.jpg',
},
mal_id: anime._id,
mal_id: anime._id,
haru_id: anime._id,
haru_id: anime._id,
tvdb_id: 'mal-' + anime._id,
tvdb_id: 'mal-' + anime._id,
imdb_id: anime._id,
imdb_id: anime._id,
slug: anime.slug,
slug: anime.slug,
title: anime.title,
title: anime.title,
year: anime.year,
year: anime.year,
type: anime.type,
type: anime.type,
item_data: anime.type,
item_data: anime.type,
rating: anime.rating
rating: anime.rating
};
};
});
});


return {results: sanitize(results), hasMore: true};
return {results: sanitize(results), hasMore: true};
};
};


function formatDetail(anime) {
function formatDetail(anime) {
var result = {
var result = {
mal_id: anime._id,
mal_id: anime._id,
haru_id: anime._id,
haru_id: anime._id,
tvdb_id: 'mal-' + anime._id,
tvdb_id: 'mal-' + anime._id,
imdb_id: anime._id,
imdb_id: anime._id,
slug: anime.slug,
slug: anime.slug,
title: anime.title,
title: anime.title,
item_data: anime.type,
item_data: anime.type,
country: 'Japan',
country: 'Japan',
genre: anime.genres,
genre: anime.genres,
genres: anime.genres,
genres: anime.genres,
num_seasons: 1,
num_seasons: 1,
runtime: anime.runtime,
runtime: anime.runtime,
status: anime.status,
status: anime.status,
synopsis: anime.synopsis,
synopsis: anime.synopsis,
network: [], //FIXME
network: [], //FIXME
rating: anime.rating,
rating: anime.rating,
images: anime.images,
// images: anime.images,
images: {
poster: 'https://media.kitsu.io/anime/poster_images/' + anime._id + '/large.jpg',
fanart: 'https://media.kitsu.io/anime/cover_images/' + anime._id + '/original.jpg',
},
backdrop: anime.images.fanart,
backdrop: anime.images.fanart,
poster: anime.images.poster,
poster: anime.images.poster,
year: anime.year,
year: anime.year,
type: anime.type
type: anime.type
};
};


if (anime.type === 'show') {
if (anime.type === 'show') {
result = _.extend(result, {episodes: anime.episodes});
result = _.extend(result, {episodes: anime.episodes});
} else {
} else {
// ret = _.extend(ret, {
// ret = _.extend(ret, {
// cover: img,
// cover: img,
// rating: item.score,
// rating: item.score,
// subtitle: undefined,
// subtitle: undefined,
// torrents: movieTorrents(item.id, item.episodes)
// torrents: movieTorrents(item.id, item.episodes)
// });
// });
}
}


return sanitize(result);
return sanitize(result);
};
};


function processCloudFlareHack(options, url) {
function processCloudFlareHack(options, url) {
var req = options;
var req = options;
var match = url.match(/^cloudflare\+(.*):\/\/(.*)/);
var match = url.match(/^cloudflare\+(.*):\/\/(.*)/);
if (match) {
if (match) {
req = _.extend(req, {
req = _.extend(req, {
uri: match[1] + '://cloudflare.com/',
uri: match[1] + '://cloudflare.com/',
headers: {
headers: {
'Host': match[2],
'Host': match[2],
'User-Agent': 'Mozilla/5.0 (Linux) AppleWebkit/534.30 (KHTML, like Gecko) PT/3.8.0'
'User-Agent': 'Mozilla/5.0 (Linux) AppleWebkit/534.30 (KHTML, like Gecko) PT/3.8.0'
}
}
});
});
}
}
return req;
return req;
};
};


function get(index, url, that) {
function get(index, url, that) {
var deferred = Q.defer();
var deferred = Q.defer();


var options = {
var options = {
url: url,
url: url,
json: true
json: true
};
};


var req = processCloudFlareHack(options, that.apiURL[index]);
var req = processCloudFlareHack(options, that.apiURL[index]);
console.info('Request to AnimeApi', req.url);
console.info('Request to AnimeApi', req.url);
request(req, function (err, res, data) {
request(req, function (err, res, data) {
if (err || res.statusCode >= 400) {
if (err || res.statusCode >= 400) {
console.warn('AnimeAPI endpoint \'%s\' failed.', that.apiURL[index]);
console.warn('AnimeAPI endpoint \'%s\' failed.', that.apiURL[index]);
if (index + 1 >= that.apiURL.length) {
if (index + 1 >= that.apiURL.length) {
return deferred.reject(err || 'Status Code is above 400');
return deferred.reject(err || 'Status Code is above 400');
} else {
} else {
return get(index + 1, url, that);
return get(index + 1, url, that);
}
}
} else if (!data || data.error) {
} else if (!data || data.error) {
err = data ? data.status_message : 'No data returned';
err = data ? data.status_message : 'No data returned';
console.error('API error:', err);
console.error('API error:', err);
return deferred.reject(err);
return deferred.reject(err);
} else {
} else {
return deferred.resolve(data);
return deferred.resolve(data);
}
}
});
});


return deferred.promise;
return deferred.promise;
};
};


AnimeApi.prototype.extractIds = function (items) {
AnimeApi.prototype.extractIds = function (items) {
return _.map(items.results, 'mal_id');
return _.map(items.results, 'mal_id');
};
};


AnimeApi.prototype.fetch = function (filters) {
AnimeApi.prototype.fetch = function (filters) {
var that = this;
var that = this;


var params = {};
var params = {};
params.sort = 'seeds';
params.sort = 'seeds';
params.limit = '50';
params.limit = '50';


if (filters.keywords) {
if (filters.keywords) {
params.keywords = filters.keywords.replace(/\s/g, '% ');
params.keywords = filters.keywords.replace(/\s/g, '% ');
}
}


if (filters.genre) {
if (filters.genre) {
params.genre = filters.genre;
params.genre = filters.genre;
}
}


if (filters.order) {
if (filters.order) {
params.order = filters.order;
params.order = filters.order;
}
}


if (filters.sorter && filters.sorter !== 'popularity') {
if (filters.sorter && filters.sorter !== 'popularity') {
params.sort = filters.sorter;
params.sort = filters.sorter;
}
}


var index = 0;
var index = 0;
var url = that.apiURL[index] + 'animes/' + filters.page + '?' + querystring.stringify(params).replace(/%25%20/g, '%20');
var url = that.apiURL[index] + 'animes/' + filters.page + '?' + querystring.stringify(params).replace(/%25%20/g, '%20');
return get(index, url, that).then(formatFetch);
return get(index, url, that).then(formatFetch);
};
};


AnimeApi.prototype.detail = function (torrent_id, old_data, debug) {
AnimeApi.prototype.detail = function (torrent_id, old_data, debug) {
var that = this;
var that = this;


var index = 0;
var index = 0;
var url = that.apiURL[index] + "anime/" + torrent_id;
var url = that.apiURL[index] + "anime/" + torrent_id;
return get(index, url, that).then(formatDetail);
return get(index, url, that).then(formatDetail);
};
};


module.exports = AnimeApi;
module.exports = AnimeApi;