-
Notifications
You must be signed in to change notification settings - Fork 143
/
data.js
52 lines (49 loc) · 1.28 KB
/
data.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
const axios = require('axios')
const config = require('./config/db')
const mongoose = require('mongoose')
mongoose.connect(config.mongodb)
const movieSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
original_title: String,
rating: Number,
genres: Array,
casts: Array,
directors: Array,
year: Number,
id: String,
image: String
})
const Movie = mongoose.model("Movie", movieSchema)
const jsonBird = "https://bird.ioliu.cn/v1?url="
const doubanMovie = "http://api.douban.com/v2/movie/top250?count="
function fetchData(count) {
const url = `${jsonBird}${doubanMovie}${count}`
axios.get(url)
.then(res => {
const subjects = res.data.subjects
subjects.map(subject => {
let movie = {
title: subject.title,
original_title: subject.original_title,
rating: subject.rating.average,
genres: subject.genres,
casts: subject.casts,
directors: subject.directors,
year: subject.year,
id: subject.id,
image: subject.images.small
}
Movie.create(movie, (err, movie) => {
if (err) { console.log(err) }
console.log(movie.title, '--------')
})
})
})
.catch((err) => {
throw err
})
}
fetchData(5)