-
Notifications
You must be signed in to change notification settings - Fork 1
/
movies_checker.js
131 lines (108 loc) · 3.64 KB
/
movies_checker.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
var slug = require('slug')
, FeedParser = require('feedparser')
, Parse = require("parse").Parse
, MongoClient = require('mongodb').MongoClient
, assert = require('assert')
, request = require('request')
, _ = require('underscore');
var req = request('https://yts.to/rss')
, feedparser = new FeedParser()
, parsedMovies = []
, PARSE_APP_KEY = process.env.PARSE_APP_KEY || ""
, PARSE_JAVASCRIPT_KEY = process.env.PARSE_JAVASCRIPT_KEY || "";
var Movie = (function () {
function Movie(item) {
this.title = item.title;
this.link = item.link;
this.pub_date = item.pubDate;
this.hash = slug(item.title, "_");
}
return Movie;
})();
function filterMovies(db, movies, callback){
var slugs = _.pluck(movies, "hash");
var collection = db.collection('movies');
// Find already inserted movies
collection.find({"hash": {$in:slugs}}).toArray(function(err, alreadyInsertedMovies) {
assert.equal(err, null);
// take the ones that are not included
var alreadyInsertedSlugs = _.pluck(alreadyInsertedMovies, "hash");
var filteredMovies = _.reject(movies, function(movie){
return _.contains(alreadyInsertedSlugs, movie.hash);
});
callback(filteredMovies);
});
}
function insertMovies(db, movies, callback){
movies = movies || [];
if(!movies.length){
return callback(movies);
}
var collection = db.collection('movies');
// Insert filtered movies
collection.insert(movies, function(err, result) {
assert.equal(err, null);
console.log("Inserted "+ movies.length +" documents into the movies collection");
callback(movies);
});
}
function sendPushNotifications(movies){
console.log("new movies:");
console.log(movies);
if(!movies.length){
return;
}
if(PARSE_APP_KEY != "" && PARSE_JAVASCRIPT_KEY != ""){
Parse.initialize(PARSE_APP_KEY, PARSE_JAVASCRIPT_KEY);
Parse.Push.send({
channels: [""], // Set the broadcast channel
data: {
alert: "New movies uploaded: \n\n" + _.pluck(movies, "title").join("\n")
}
}, {
success: function () {
// Push was successful
},
error: function (error) {
// Handle error
}
});
}
}
req.on('error', function (error) {
// handle any request errors
});
req.on('response', function (res) {
var stream = this;
if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
stream.pipe(feedparser);
});
feedparser.on('error', function(error) {
// always handle errors
console.log(error);
});
feedparser.on('readable', function() {
var stream = this
, meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
, item;
while (item = stream.read()) {
parsedMovies.push(new Movie(item));
}
});
feedparser.on('end', function(){ // This is where the action is!
if(parsedMovies.length){
// Connection URL
var url = 'mongodb://localhost:27017/yts';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
filterMovies(db, parsedMovies, function(filteredMovies){
insertMovies(db, filteredMovies, function(insertedMovies){
db.close();
sendPushNotifications(insertedMovies);
});
});
});
}
});