-
Notifications
You must be signed in to change notification settings - Fork 1
/
plex.js
189 lines (137 loc) · 4.39 KB
/
plex.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
const {Config} = require('./utils');3
const PlexAPI = require("plex-api");
const Path = require('path');
const options = {
hostname: Config.PLEX_IP,
port: Config.PLEX_PORT,
username: Config.PLEX_USER,
password: Config.PLEX_PASSWORD,
options: {
identifier: Config.PLEX_IDENTIFIER
}
}
const Plex = new PlexAPI( options );
function PlexQuery() {
let args = Array.prototype.slice.call(arguments, 0);
return Plex.query.apply(Plex, args);
}
class PlexLibrary {
get Name() {
return this.data.title;
}
get Key() {
return this.data.key;
}
get LastScan() {
return this.data.lastScan;
}
get Type() {
return this.data.type
}
constructor(obj) {
this.data = obj;
}
recentlyAdded(start, count) {
// ?X-Plex-Container-Start=${start}&X-Plex-Container-Size=${count}
return PlexQuery(`/library/sections/${this.Key}/recentlyAdded`);
}
filterRecentlyAdded(start, count) {
return this.recentlyAdded(start, count).then( (res) => {
let MediaContainer = res.MediaContainer;
// if ( MediaContainer.librarySectionID != 7 ) return [];
let items = MediaContainer.Metadata || [];
console.log(`[${this.Name} (${this.Key})] found ${items.length} recently added`);
let remappedItems = this.remapData( items );
console.log(`[${this.Name} (${this.Key})] remapped in ${remappedItems.length} recently added`);
let sortedRemappedItems = remappedItems.sort( (item1, item2) => {
return item1.addedAt > item2.addedAt ? 1 : -1;
});
return sortedRemappedItems.filter( (item) => {
return item.addedAt > this.LastScan; // && item.key == '/library/metadata/42317';
}).sort( (item1, item2) => {
return item1.addedAt > item2.addedAt ? 1 : -1;
});
});
}
itemDetails(item) {
return PlexQuery(`${item.key}`);
}
remapData( items ) {
// if ( this.Key == '9'|| this.Key == '13' || this.Key == '27' || this.Key == '35') {
if ( this.Type == 'show' ) {
// SerieTV
let res = {};
for ( let item of items ) {
let show = res[ item.grandparentTitle ];
if ( !show ) {
show = res[ item.grandparentTitle ] = JSON.parse( JSON.stringify(item) ); // duplicate item
show.Seasons = {};
show.Media = [];
}
show.Seasons[ item.parentTitle ] = item.year
show.Media = show.Media.concat( item.Media );
show.addedAt = Math.max( show.addedAt, item.addedAt );
}
let shows = Object.values(res);
for ( let show of shows ) {
show.title = show.grandparentTitle;
show.year = Object.values(show.Seasons).sort( (y1, y2) => y1 > y2 ? 1 : -1 )[0];
}
return shows;
// } else if ( this.Key == '16' || this.Key == '25' ) {
} else if ( this.Name == 'On Stage' ) {
// Video - Videos Collection
let res = {};
for ( let item of items ) {
let filepath = null;
try {
filepath = item.Media[0].Part[0].file;
} catch (e) {
continue;
}
// let fileext = Path.extname(filepath);
// let filename = Path.basename(filepath, fileext);
let dirpath = Path.dirname(filepath);
let dirname = Path.basename(dirpath);
let collection = res[ dirname ];
if ( !collection ) {
collection = res[ dirname ] = JSON.parse( JSON.stringify(item) ); // duplicate item
collection.Media = [];
}
collection.title = dirname;
collection.Media = collection.Media.concat( item.Media );
collection.addedAt = Math.max( collection.addedAt, item.addedAt );
}
return Object.values(res);
} else {
// docu-film , film, cineteca, animation
let res = {};
for ( let item of items ) {
let year = item.year || 0;
let movie = res[ `${item.title.toLowerCase()}-${year}` ];
if ( !movie ) {
movie = res[ `${item.title.toLowerCase()}-${year}` ] = JSON.parse(JSON.stringify(item) );
movie.Media = [];
}
movie.Media = movie.Media.concat( item.Media );
}
return Object.values(res);
}
}
start() {
/**
V configure cron job
V get all recentlyAdded
V filter by lastScan
V loop each recent movies
V scrape all data
V check telgram bot
V compiute html noty
V publish to telegram
*/
}
}
module.exports = {
PlexQuery,
PlexLibrary
};