forked from Tueti/MMM-MovieListings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
87 lines (69 loc) · 2.56 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-MovieListing
*
* By Christian Jens http://christianjens.de
* MIT Licensed.
*/
// https://api.themoviedb.org/3/movie/now_playing?api_key=dbe08b1e1a7061853de90f8dd0e3408e&language=de-DE&page=1®ion=de
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-MovieListing helper started...')
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'FETCH_MOVIE_LIST') {
this.fetchMovieList(payload);
}
if (notification === 'FETCH_MOVIE_ID') {
this.fetchMovieById(payload);
}
},
fetchMovieList: function(payload) {
var self = this;
var movies = [];
request(
payload.baseUrl + '?api_key=' + payload.apiKey + '&language=' + payload.language + '&page=1' + '®ion=' + payload.region,
function (error, response, body) {
if (error) {
return self.sendSocketNotification('MOVIE_ERROR', error);
}
self.sendSocketNotification('MOVIE_LIST_DONE', JSON.parse(body))
}
);
},
fetchMovieById: function(payload) {
var self = this;
var movie = {};
request(
'https://api.themoviedb.org/3/movie/' + payload.movieId + '?api_key=' + payload.apiKey + '&language=' + payload.language,
function (error, response, body) {
if (error) {
return self.sendSocketNotification('MOVIE_ERROR', error);
}
request(
'https://api.themoviedb.org/3/movie/' + payload.movieId + '/credits?api_key=' + payload.apiKey,
function(subError, subRresponse, creditsBody) {
if (subError) {
return self.sendSocketNotification('MOVIE_ERROR', error);
}
var movieData = {
details: JSON.parse(body),
credits: JSON.parse(creditsBody)
};
// Create shortened version of plot with linebreaks so it displays nicely
var plotContent = "";
if (payload.config.maxPlotLength == 0) {
plotContent = movieData.details.overview
} else {
plotContent = movieData.details.overview.length > payload.config.maxPlotLength ? `${movieData.details.overview.substring(0, (payload.config.maxPlotLength))}…` : movieData.details.overview;
}
// Add shortened plot to payload
movieData.details.overviewShort = plotContent;
self.sendSocketNotification('MOVIE_ID_DONE', movieData);
}
);
}
);
}
});