-
Notifications
You must be signed in to change notification settings - Fork 0
/
plex-webhook-recently-added.js
executable file
·93 lines (80 loc) · 2.98 KB
/
plex-webhook-recently-added.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
#!/usr/bin/env node
require('dotenv').load();
const info = require('./package.json');
const request = require('request');
const moment = require('moment');
const debug = process.env.NODE_ENV === 'production' ? () => {} : console.log; // eslint-disable-line no-console
const signInUrl = process.env.SIGN_IN_URL || 'https://plex.tv/users/sign_in.json';
if (!process.env.WEBHOOKS || process.env.WEBHOOKS.length < 1) {
throw new Error('No webhook(s) provided! Add them in .env');
}
if (!process.env.PLEX_URL) {
throw new Error('No Plex URL provided! Add it in .env');
}
if (!process.env.USERNAME) {
throw new Error('No Plex username provided! Add it in .env');
}
if (!process.env.PASSWORD) {
throw new Error('No Plex password provided! Add it in .env');
}
debug(`${new Date()}`, `Requesting ${signInUrl}`);
request.post({
url: signInUrl,
form: {
'user[login]': process.env.USERNAME,
'user[password]': process.env.PASSWORD,
},
headers: {
'X-Plex-Client-Identifier': process.env.UUID || '48a766bc-45ac-40e0-a0f6-c62a6bce42d4',
'X-Plex-Product': info.name,
'X-Plex-Version': info.version,
'X-Plex-Device-Name': process.env.NAME || info.name,
Accept: 'application/json',
},
}, (loginError, loginResult, login) => {
if (loginError) {
return debug(`${new Date()}`, `Error retrieving token, status ${loginResult && loginResult.statusCode} - `, loginError);
}
const user = JSON.parse(login).user;
const token = user.authentication_token;
const port = process.env.PLEX_PORT || 32400;
const url = `${process.env.PLEX_URL}:${port}/library/recentlyAdded`;
debug(`${new Date()}`, `Requesting ${url}`);
return request({
url,
headers: {
'X-Plex-Token': token,
Accept: 'application/json',
},
}, (recentlyAddedError, recentlyAddedResult, recentlyAdded) => {
if (recentlyAddedError) {
return debug(`${new Date()}`, `Error getting recently added, status ${recentlyAddedResult && recentlyAddedResult.statusCode} - `, recentlyAddedError);
}
const items = JSON.parse(recentlyAdded).MediaContainer.Metadata;
const oneDayAgo = moment().subtract(process.env.UPDATED_AT_FILTER_VALUE || 1, process.env.UPDATED_AT_FILTER_UNIT || 'days');
const addedWithinLastDay = items.filter(item => moment.unix(item.addedAt).isAfter(oneDayAgo));
const formattedItems = addedWithinLastDay.map((item) => {
let name;
if (item.type === 'movie') {
name = item.title;
} else if (item.type === 'season') {
name = `${item.parentTitle} ${item.title}`;
} else {
name = `${item.title} (${item.type})`;
}
return name;
});
if (process.env.SILENT === '1') {
return console.log(`${new Date()}`, 'Body: ', formattedItems);
}
return JSON.parse(process.env.WEBHOOKS).forEach((webhook) => {
debug(`${new Date()}`, `Running webhook ${webhook}`);
request.post({
url: webhook,
timeout: 10000,
body: formattedItems,
json: true,
});
});
});
});