-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneed.js
136 lines (126 loc) · 5.01 KB
/
need.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var axios = require('axios');
var opencage = require('opencage-api-client');
require('dotenv').config();
var Review = require('./models/review');
var User = require('./models/user');
var need = {};
var tvGenres = {
10759: "Action & Adventure",
16: "Animation",
35: "Comedy",
80: "Crime",
99: "Documentary",
18: "Drama",
10751: "Family",
10762: "Kids",
9648: "Mystery",
10763: "News",
10764: "Reality",
10765: "Sci-Fi & Fantasy",
10766: "Soap",
10767: "Talk",
10768: "War & Politics",
37: "Western"
};
var movieGenres = {
28: "Action",
12: "Adventure",
16: "Animation",
35: "Comedy",
80: "Crime",
99: "Documentary",
18: "Drama",
10751: "Family",
14: "Fantasy",
36: "History",
27: "Horror",
10402: "Music",
9648: "Mystery",
10749: "Romance",
878: "Science Fiction",
10770: "TV Movie",
53: "Thriller",
10752: "War",
37: "Western"
};
axios.get(`https://api.themoviedb.org/3/trending/movie/day?api_key=${process.env.MOVIE_API_KEY}`)
.then(data => {
Movies = data.data.results;
Movies.forEach(movie => {
for (var i = 0; i < movie.genre_ids.length; i++) {
movie.genre_ids[i] = movieGenres[movie.genre_ids[i]];
};
movie.trailer_link = null ;
axios.get(`https://api.themoviedb.org/3/movie/${movie.id}/videos?api_key=a4cfca64067fb7977096ad4e3a1f1f8b&language=en-US`)
.then(data => {
var Videos = data.data.results;
if (Videos.length > 0) {
for (var i = 0; i < Videos.length; i++) {
if (movie.trailer_link) break;
var video = Videos[i];
if (video.type === "Trailer") {
if (video.site === "YouTube")
movie.trailer_link = `https://www.youtube.com/embed/${video.key}?autoplay=0&controls=1&loop=1&picture-in-picture=1`;
if (video.site === "Vimeo")
movie.trailer_link = `https://player.vimeo.com/video/${video.key}`;
}
};
}
need.treMovies = Movies;
})
.catch( err => console.log(err) )
});
})
.catch(err => console.log(err) );
axios.get(`https://api.themoviedb.org/3/trending/tv/day?api_key=${process.env.MOVIE_API_KEY}`)
.then(data => {
Shows = data.data.results;
Shows.forEach(show => {
for (var i = 0; i < show.genre_ids.length; i++) {
show.genre_ids[i] = tvGenres[show.genre_ids[i]]
};
show.trailer_link = null;
axios.get(`https://api.themoviedb.org/3/tv/${show.id}/videos?api_key=a4cfca64067fb7977096ad4e3a1f1f8b&language=en-US`)
.then(data => {
var Videos = data.data.results;
if (Videos.length > 0) {
for (var i = 0; i < Videos.length; i++) {
if (show.trailer_link) break;
var video = Videos[i];
if (video.type === "Trailer") {
if (video.site === "YouTube")
show.trailer_link = `https://www.youtube.com/embed/${video.key}?autoplay=0&controls=1&loop=1&picture-in-picture=1`;;
if (video.site === "Vimeo")
show.trailer_link = `https://player.vimeo.com/video/${video.key}`;
}
};
}
need.treShows = Shows;
}).catch( err => console.log(err) )
});
}).catch( err => console.log(err) );
need.geo_based_news = (userId) => {
return new Promise((resolve, reject) => {
if (userId) {
User.findById(userId)
.then(user => {
axios.get(`http://newsapi.org/v2/top-headlines?country=${user.region}&category=entertainment&apiKey=${process.env.NEWS_API_KEY}`)
.then(news => {
resolve(news.data.articles);
}).catch(err => console.log(err) );
}).catch(err => {
axios.get(`http://newsapi.org/v2/top-headlines?country=in&category=entertainment&apiKey=${process.env.NEWS_API_KEY}`)
.then(news => {
resolve(news.data.articles);
}).catch(err => console.log(err) );
console.log(err);
})
} else {
axios.get(`http://newsapi.org/v2/top-headlines?country=in&category=entertainment&apiKey=${process.env.NEWS_API_KEY}`)
.then(news => {
resolve(news.data.articles);
}).catch(err => console.log(err) );
}
});
}
module.exports = need;