Added catalog movies/tv to API

This commit is contained in:
zeldaroot 2021-06-04 03:20:21 +03:00
parent 70ed66898c
commit 58d7a9ac15
3 changed files with 167 additions and 48 deletions

View File

@ -143,6 +143,85 @@ function movieApi(query, ip, callback) {
});
}
/**
* Get movies.
*
* @return {Object}
*/
function moviesApi(query, ip, callback) {
var hash = md5(JSON.stringify(query));
if (CP_cache.getSync(hash)) {
return callback(null, CP_cache.getSync(hash));
}
var page = query['page'] || 1;
var limit = query['limit'] || 20;
var max_matches = page * limit;
if (limit > 100) {
return callback('Max 100 movies per page');
}
if (max_matches > 1000) {
return callback('Max 1 000 movies');
}
delete query['page'];
delete query['limit'];
var connection = sphinx.createConnection({});
connection.connect(function(err) {
if (err) {
if (typeof connection !== 'undefined' && connection) {
connection.end();
}
console.error(err);
return callback('Error connection.');
}
connection.query(
'SELECT *, 1 AS movie, custom.lastmod AS lastmod FROM rt_' +
config.domain.replace(/[^a-z0-9]/g, '_') +
' WHERE ' +
Object.keys(Object.assign(query, { movie: '1' }))
.map(function(key) {
if (!query[key]) return false;
return '`' + key + '` = ' + query[key] + '';
})
.filter(Boolean)
.join(' AND ') +
' ORDER BY lastmod DESC' +
' LIMIT ' +
(page * limit - limit) +
',' +
limit +
' OPTION max_matches = ' +
max_matches +
'; SHOW META',
function(err, results) {
if (typeof connection !== 'undefined' && connection) {
connection.end();
}
if (err) {
console.error(err);
return callback('Error query.');
}
if (!results || !results[0] || !results[0][0] || !results[0][0].id) {
return callback('Error result.');
}
var time = results[1] && results[1][2];
var api_result = results[0].map(function(movie) {
var m = structureMovieApi(movie);
delete m.players;
return m;
});
var data = {
status: 'success',
time: time.Value,
result: api_result
};
CP_cache.setSync(hash, data);
callback(null, data);
}
);
});
}
/**
* Generate secret iframe link.
*
@ -328,6 +407,31 @@ function structureMovieApi(movie) {
});
}
});
players =
players && players.length
? players
.sort(function(a, b) {
if (
typeof a.season === 'undefined' ||
typeof a.episode === 'undefined'
) {
return -1;
}
return parseFloat(a.season) - parseFloat(b.season);
})
.sort(function(a, b) {
if (
typeof a.season === 'undefined' ||
typeof a.episode === 'undefined'
) {
return -1;
}
if (b.season === a.season) {
return parseFloat(a.episode) - parseFloat(b.episode);
}
return 0;
})
: null;
var data = {
id: movie.id,
imdb_id: (custom && custom.imdb_id) || null,
@ -390,39 +494,14 @@ function structureMovieApi(movie) {
})
: null,
trailer: (custom && custom.trailer) || null,
embed:
players && players.length
? (config.language === 'ru' && config.ru.subdomain && config.ru.domain
? config.protocol + config.ru.subdomain + config.ru.domain
: config.protocol + config.subdomain + config.domain) +
'/embed/' +
movie.id
: null,
players:
players && players.length
? players
.sort(function(a, b) {
if (
typeof a.season === 'undefined' ||
typeof a.episode === 'undefined'
) {
return -1;
}
return parseFloat(a.season) - parseFloat(b.season);
})
.sort(function(a, b) {
if (
typeof a.season === 'undefined' ||
typeof a.episode === 'undefined'
) {
return -1;
}
if (b.season === a.season) {
return parseFloat(a.episode) - parseFloat(b.episode);
}
return 0;
})
: null,
embed: players
? (config.language === 'ru' && config.ru.subdomain && config.ru.domain
? config.protocol + config.ru.subdomain + config.ru.domain
: config.protocol + config.subdomain + config.domain) +
'/embed/' +
movie.id
: null,
players: players,
imdb: {
rating: movie.imdb_rating,
votes: movie.imdb_vote
@ -434,15 +513,19 @@ function structureMovieApi(movie) {
web: {
rating: movie.rating,
votes: movie.vote
},
lastmod: (custom && custom.lastmod) || null
}
};
if (players && players[0] && players[0].season && players[0].episode) {
data.season = players[0].season;
data.episode = players[0].episode;
}
if (movie.quality) {
data.quality = movie.quality;
}
if (movie.translate) {
data.sound = movie.translate;
}
data.lastmod = (custom && custom.lastmod) || null;
return data;
}
@ -505,5 +588,6 @@ function createImgUrl(type, size, id) {
}
module.exports = {
movie: movieApi
movie: movieApi,
movies: moviesApi
};

View File

@ -597,18 +597,21 @@ router.get('/', function(req, res) {
} else {
ips.set(ip, tokens.get(token).ip);
}
var movie = false;
var q = {};
['imdb_id', 'tmdb_id', 'douban_id', 'wa_id', 'tvmaze_id', 'movie_id'].forEach(
function(key) {
if (req.query[key] && req.query[key].replace(/[^0-9]/g, '')) {
q[key] =
"'" + decodeURIComponent(req.query[key].replace(/[^0-9]/g, '')) + "'";
movie = true;
}
}
);
['id', 'kp_id'].forEach(function(key) {
if (req.query[key] && req.query[key].replace(/[^0-9]/g, '')) {
q[key] = decodeURIComponent(req.query[key].replace(/[^0-9]/g, ''));
movie = true;
}
});
if (req.query['type'] && req.query['type'].replace(/[^0-9]/g, '')) {
@ -619,18 +622,38 @@ router.get('/', function(req, res) {
) {
q['type'] = req.query['type'] === 'tv' ? 1 : 0;
}
if (Object.keys(q).length <= 0) {
console.error('[ERROR PARAMS]', 'TOKEN:', token, 'IP:', ip);
return res
.status(404)
.json({ status: 'error', message: 'Your request is empty.' });
}
CP_api.movie(q, ip, function(err, result) {
if (err) {
return res.status(404).json({ status: 'error', message: err });
if (
req.query['limit'] &&
req.query['limit'].replace(/[^0-9]/g, '') &&
parseInt(req.query['limit'].replace(/[^0-9]/g, ''))
) {
q['limit'] = parseInt(req.query['limit'].replace(/[^0-9]/g, ''));
if (q['limit'] <= 0 || q['limit'] > 100) {
q['limit'] = 100;
}
return res.json(result);
});
}
if (
req.query['page'] &&
req.query['page'].replace(/[^0-9]/g, '') &&
parseInt(req.query['page'].replace(/[^0-9]/g, ''))
) {
q['page'] = parseInt(req.query['page'].replace(/[^0-9]/g, ''));
}
if (movie) {
CP_api.movie(q, ip, function(err, result) {
if (err) {
return res.status(404).json({ status: 'error', message: err });
}
return res.json(result);
});
} else {
CP_api.movies(q, ip, function(err, result) {
if (err) {
return res.status(404).json({ status: 'error', message: err });
}
return res.json(result);
});
}
});
router.all('/', function(req, res) {

View File

@ -210,7 +210,7 @@
&nbsp;&nbsp;<span class="id_imdb">imdb_id</span> - ID imdb.com
</code><br>
<code class="text-muted" style="margin:0">
&nbsp;&nbsp;<span class="id_tmdb">tmdb_id</span> - ID tmdb.org
&nbsp;&nbsp;<span class="id_tmdb">tmdb_id</span> - ID tmdb.org (type=movie|tv)
</code><br>
<code class="text-muted" style="margin:0">
&nbsp;&nbsp;<span class="id_douban">douban_id</span> - ID movie.douban.com
@ -227,6 +227,18 @@
<code class="text-muted" style="margin:0 0 1px 0">
&nbsp;&nbsp;type - movie or tv
</code><br>
<code class="text-muted">
&nbsp;&nbsp;<%- config.protocol %><%- config.subdomain %><%- config.domain %>/api?token=9b24679ee2abc8ca012ca4b07223739f&<span class="id_kp">type</span>=tv
</code><br>
<code class="text-muted" style="margin:0 0 1px 0">
&nbsp;&nbsp;type - movie or tv
</code><br>
<code class="text-muted" style="margin:0 0 1px 0">
&nbsp;&nbsp;limit - movies per page (max 100)
</code><br>
<code class="text-muted" style="margin:0 0 1px 0">
&nbsp;&nbsp;page - number page (max 1000 movies)
</code><br>
</div>
</div>
</div>