forked from OstlerDev/PopcornTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanartGenerator.js
70 lines (67 loc) · 2.88 KB
/
fanartGenerator.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
var images = require("ptv-images");
var logger = require("./logger");
function generateFanart(imdb, resolution, callback) {
logger.Debug("=== Generating Fanart ===");
var width = 1920;
var height = 1080;
if (resolution == '720') {
width = 1280;
height = 720;
}
var fs = require('fs');
if (fs.existsSync(__dirname + '/assets/cache/' + imdb + '-' + resolution + '.jpg')) {
logger.Debug('Fanart already downloaded, serving.');
callback('cache/' + imdb + '-' + resolution + '.jpg');
} else {
var API = require('./MoviesAPI');
var movies = API.getFanart(imdb, function(url) {
if (url == null) {
callback('thumbnails/Background_blank_' + resolution + '.jpg');
} else {
var http = require('https');
var file = fs.createWriteStream(__dirname + "/tmp.jpg");
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
images(__dirname + '/tmp.jpg').resize(width, height).draw(images(__dirname + '/assets/thumbnails/gradient_' + resolution + '.png'), 0, 0).save(__dirname + '/assets/cache/' + imdb + '-' + resolution + '.jpg');
callback('cache/' + imdb + '-' + resolution + '.jpg');
fs.unlink(__dirname + '/tmp.jpg');
});
}).on('error', function(e){
logger.error(e);
});
}
});
}
}
function generateFanartTV(url, imdb, resolution, callback) {
logger.Debug("=== Generating Fanart for TV ===");
var width = 1920;
var height = 1080;
if (resolution == '720') {
width = 1280;
height = 720;
}
var fs = require('fs');
if (fs.existsSync(__dirname + '/assets/cache/' + imdb + '-' + resolution + '.jpg')) {
logger.Debug('Fanart already downloaded, serving.');
callback('cache/' + imdb + '-' + resolution + '.jpg');
} else {
var http = require('https');
if (url == null) {
callback('thumbnails/Background_blank_' + resolution + '.jpg');
} else {
var file = fs.createWriteStream(__dirname + "/tmp.jpg");
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
images(__dirname + '/tmp.jpg').resize(width, height).draw(images(__dirname + '/assets/thumbnails/gradient_' + resolution + '.png'), 0, 0).save(__dirname + '/assets/cache/' + imdb + '-' + resolution + '.jpg');
callback('cache/' + imdb + '-' + resolution + '.jpg');
fs.unlink(__dirname + '/tmp.jpg');
});
});
}
}
}
exports.generateFanart = generateFanart;
exports.generateFanartTV = generateFanartTV;