-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
142 lines (119 loc) · 3.88 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
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
137
138
139
140
141
142
// import expree
const express = require('express');
const Twitter = require('twitter');
const config = require('./config/twitter');
const tmdbconfig = require('./config/tmdb');
const fetch = require('node-fetch');
const Movie = require('./movie');
const axios = require('axios');
const sheduler = require('node-schedule');
// initialize express
var app = express();
let listOfMovies; // this will keep the list of movies
let numOfPages; // holds the value of result pages
let currentPage; // hold the value of current page
var twitter = new Twitter(config); // initialize twitter
var schedule = require('node-schedule');
schedule.scheduleJob('1 9 * * *', ()=> { // call get method at 09.0H for every day (Time is on UTC)
getMovies();
})
// further filter recent movies from api result
async function getRecentReleases(movieList) {
for(let x= 0; x<movieList.length; x++) {
let date = new Date();
let month = date.getMonth() +1;
if (month <10){
month = '0' + month;
}
date = date.getFullYear() + '-' + month + '-'+ (date.getDate()+1);
//console.log(date.valueOf());
//console.log(movieList[x]['release_date']);
let trailerUrl;
if(movieList[x]['release_date'] == date ) {
trailerUrl= await getTrailer(movieList[x]['id']);
if(trailerUrl) {
let mv = {};
mv.title = movieList[x]['title'];
mv.trailler = trailerUrl;
//console.log(movieList[x]['title'] + ' ' +trailerUrl);
tweet(mv);
} else {
console.log(movieList[x]['title'] + ' I dont have trailer');
}
} else{
//console.log(date);
}
}
}
// get list of movies
function getMovies() {
axios.get(genarateUrl(1))
.then(response => {
listOfMovies = response.data.results;
numOfPages = response.data.total_pages;
getRecentReleases(listOfMovies);
for(let y = 1; y<=numOfPages; y++) {
console.log('page'+y);
getMoviesByPage(y)
}
// console.log(response.data.results);
})
.catch(err => {
console.log(err.msg);
});
}
// get list of movies of given page
function getMoviesByPage(page) {
axios.get(genarateUrl(page))
.then( response => {
listOfMovies = response.data.results;
numOfPages = response.data.total_pages;
getRecentReleases(listOfMovies);
})
.catch(err => {
console.log(err.msg);
})
}
// genarate url from current date
function genarateUrl(page) {
let date = new Date();
let minDate = date.getFullYear() + '-' + (date.getMonth()+1) + '-'+ (date.getDate()+1);
let maxDate = date.getFullYear() + '-' + (date.getMonth()+1) + '-'+ (date.getDate()+2);
let url = `https://api.themoviedb.org/3/discover/movie?api_key=${tmdbconfig}&release_date.gte=`+ minDate +'&release_date.lte='+ maxDate + '&page=' + page;
//console.log(url);
return url;
}
// get trailler url for given movie
async function getTrailer(movie_id) {
let url2 = `https://api.themoviedb.org/3/movie/${movie_id}/videos?api_key=${tmdbconfig}&language=en-US`;
let videoList;
let key = '';
const response = await fetch(url2);
const data = await response.json();
videoList = await data.results;
if(videoList) {
for(let x = 0; x < videoList.length; x++) {
if(videoList[x]['type'] == 'Trailer' && videoList[x]['site'] == 'YouTube') {
key = await videoList[x]['key'];
break;
}
}
return key;
}
}
// tweet on given movie
function tweet(movie) {
//making the status
let title = movie.title.replace(/ /g,"_");
var status = {
status: `${movie.title}\n #${title} #upMovie #new_release #2k18 #codezilla2k18\nhttps://www.youtube.com/watch?v=${movie.trailler}`
}
// post on twitter
twitter.post('statuses/update',status, function(error, tweet, response) {
if(!error) {
console.log(tweet);
} else {
console.log(error);
}
});
}