-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
56 lines (50 loc) · 1.81 KB
/
index.js
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
const express = require('express');
const request = require('request');
const async = require('async');
import mongoose from 'mongoose'
import Shows from './models/shows'
const app = express();
const numTvShows = 1000
mongoose.connect('mongodb://localhost/shows');
//fetch tv shows from tvmaze and save into mongoDB
app.get('/all', (req, res, next) => {
async.timesLimit(numTvShows, 2, (i, callback) => {
setTimeout(function(){
var options = {
url: 'http://api.tvmaze.com/shows/'+(i+1)+"?embed[]=episodes&embed[]=cast",
}
request(options, (error, response, body) => {
var result = JSON.parse(body)
var data = {
id: result.id ,
name: result.name ,
image: result.image ? result.image.original : null ,
details: {
genres: result.genres ,
year: result.premiered ,
description: result.summary ,
cast: result._embedded ? result._embedded.cast : null ,
episodes: result._embedded ? result._embedded.episodes : null
}
};
console.log(i)
callback(null, data)
});
}, 1000)
}, (err, results) => {
results.map((items) => {
const show = new Shows(items)
show.save()
})
});
return next()
})
//get all shows that id is equal to genre
app.get('/shows/:id', (req, res, next) => {
Shows.find({'details.genres': {$in: [req.params.id]}},function(err, shows){
res.json(shows)
})
})
app.listen('8010', () => {
console.log('Listening on port 8010');
})