1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
var showSchema = new mongoose.Schema({
_id: Number,
tmdb_id: Number,
title: String,
original_title: String,
release_date: Date,
year: Date,
plot: String,
poster: String,
background: String,
runtime: Number,
number_of_seasons: Number,
number_of_episodes: Number,
tmdb_rating: Number,
tmdb_popularity: Number,
in_production: Boolean,
last_air_date: Date,
fully_scraped: Boolean,
allow_update: Boolean,
in_production: Boolean,
seasons: [{type: mongoose.Schema.Types.ObjectId, ref: 'Season'}]
});
var seasonSchema = new mongoose.Schema({
_id: Number,
release_date: Date,
episode_count: Number,
tmdb_id: Number,
poster: String,
season_number: Number,
fully_scraped: Boolean,
allow_update: Boolean,
_show: {type: Number, ref: 'Show'}
});
app.get('/api/shows/:id', function(req, res, next) {
async.waterfall([
function(callback) {
Show.findById(req.params.id, function(err, show) {
if(err) return next(err);
if(show.fully_scraped) {res.send(show); return;}
callback(err, show);
});
},
function(show, callback) {
MovieDB.tvInfo({id: show._id, language: 'fr'}, function(err, data) {
show.plot = data.overview;
show.background = 'http://image.tmdb.org/t/p/w780' + data.backdrop_path;
show.episode_count = data.number_of_episodes;
show.tmdb_id = data.id;
show.poster = 'http://image.tmdb.org/t/p/w342' + data.poster_path;
show.season_number = data.number_of_seasons;
show.fully_scraped = true;
show.in_production = data.in_production;
show.last_air_date = data.last_air_date;
show.save(function(err) {
if (err)
console.log('error');
else
console.log('success');
});
console.log('info');
callback(err, show, data);
});
},
function(show, data) {
var seasons = data.seasons;
var seasonsArray = [];
_.each(seasons, function(season) {
if(season.season_number != 0) {
var s = new Season({
_id: season.id,
release_date: season.air_date,
episode_count: season.episode_count,
tmdb_id: season.id,
poster: 'http://image.tmdb.org/t/p/w342' + season.poster_path,
season_number: season.season_number,
fully_scraped: true,
allow_update: false,
_show: show._id // Ici mon Number est bien enregistré
});
s.save(function(err) {
if (err)
console.log(err)
else {
console.log('season saved');
show.seasons.push(s._id); // J'ai une erreur avec le push : Cast to ObjectId failed for value "3247" at path "seasons"
}
});
}
});
}
]);
}); |
Partager