This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
208 lines (151 loc) · 5.05 KB
/
update.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//////////////////////////////
// krushaTV episode updater //
//////////////////////////////
// set environment variable
var env = process.env.NODE_ENV || "development";
// DB Models
var models = require( './models' );
var path = require( 'path' );
// LOGGING
var bunyan = require( 'bunyan' );
var log_options = {
name: 'krushaTV_update',
streams: [ {
path: path.join( __dirname, './log/krushaTV_update.log' ),
level: 'error'
} ]
};
if ( env !== 'production' ) {
log_options.streams.push( {
level: 'info',
stream: process.stdout
});
}
var log = bunyan.createLogger( log_options );
// Promise library
var Promise = require( 'bluebird' );
// Sanitizing
var sanitizeHtml = require( 'sanitize-html' );
// File IO
var fs = Promise.promisifyAll( require( 'fs' ) );
// TVDb api
var tvdb_api_data = require( __dirname + '/config/thetvdb' );
var tvdb_temp = require( __dirname + '/app/modules/tvdb' );
var TVDb = new tvdb_temp( tvdb_api_data.key, tvdb_api_data.username, tvdb_api_data.password, {
language: 'en'
} );
// Time library
var moment = require( 'moment' );
var last_updated = null;
var now = null;
var last_updated_file = __dirname + '/config/last_updated.json';
function update_episodes( show, page ) {
return TVDb.SeriesEpisodes( show.thetvdb_id, page )
.then( function( data ) {
if ( !data.data ) {
console.log( show.name );
console.log( page );
return;
}
return Promise.map( data.data, function( episode ) {
var season = parseInt( episode.airedSeason );
var episode_nr = parseInt( episode.airedEpisodeNumber );
var airdate = episode.firstAired ? new Date( episode.firstAired ) : null;
if ( isNaN( season ) || season < 1 || isNaN( episode_nr ) || episode_nr < 1 ) {
return;
}
var result;
return models.Episodes.findOrCreate( {
where: {
thetvdb_id: episode.id
},
defaults: {
seriesid: show.id,
season: season,
episode: episode_nr,
title: episode.episodeName,
airdate: airdate,
thetvdb_id: episode.id,
overview: episode.overview
}
})
.spread( function( _result, created ) {
result = _result;
if ( !created ) {
result.season = season;
result.episode = episode_nr;
result.title = episode.episodeName;
result.airdate = airdate;
result.overview = episode.overview;
return result.save();
}
})
.catch( function ( error ) {
if ( error.name === 'SequelizeUniqueConstraintError' ) {
return result.destroy();
}
else {
throw error;
}
});
}, {
concurrency: 5
})
.then( function() {
var next_page = parseInt( data.links.next );
if ( !isNaN( next_page ) && next_page > 0 ) {
return update_episodes( show, next_page );
}
});
});
}
fs.readFileAsync( last_updated_file, {
encoding: 'utf8'
} )
.then( function( last_updated_file_content ) {
if ( last_updated_file_content.length > 0 ) {
return JSON.parse( last_updated_file_content );
}
} )
.then( function( _last_updated ) {
if ( _last_updated ) {
last_updated = moment( _last_updated ).toDate();
}
})
.then( function() {
return TVDb.login();
} )
.then( function() {
now = moment().toDate();
return TVDb.UpdatedQuery( last_updated )
})
.then( function( updated_shows ) {
var shows_id_array = [];
if ( updated_shows.data ) {
updated_shows.data.forEach( function( show ) {
shows_id_array.push( show.id );
});
}
return models.Series.findAll( {
where: {
thetvdb_id: {
in: shows_id_array
}
}
});
})
.each( update_episodes )
.then( function() {
return fs.writeFileAsync( last_updated_file, JSON.stringify( now ), {
encoding: 'utf8'
} );
})
.then( function() {
console.log( 'done' );
})
.catch( function( error ) {
log.error( error );
})
.finally( function() {
process.exit( 0 );
});