-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.js
1206 lines (1042 loc) · 49.3 KB
/
stats.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var servers;
var sections = {};
var active_server;
var active_section;
var last_updated_string;
var resolution_mappings = {"4k" : "4K",
"1080" : "1080p",
"720" : "720p",
"480" : "480p",
"576" : "576p",
"sd" : "SD"
};
function formattedDateString(timestamp) {
var date = new Date(timestamp);
var formatted_date = date.toLocaleTimeString() + " " + date.toDateString();
return formatted_date;
}
function showHeadings() {
var movies_visible = document.getElementById("movies-container").style.display === "block";
var shows_visible = document.getElementById("shows-container").style.display === "block";
var music_visible = document.getElementById("music-container").style.display === "block";
// there is probably a better way to do this
if (movies_visible && shows_visible && music_visible) {
document.getElementById("movies-heading").style.display = "block";
document.getElementById("shows-heading").style.display = "block";
document.getElementById("music-heading").style.display = "block";
}
else if (movies_visible && shows_visible) {
document.getElementById("movies-heading").style.display = "block";
document.getElementById("shows-heading").style.display = "block";
}
else if (movies_visible && music_visible) {
document.getElementById("movies-heading").style.display = "block";
document.getElementById("music-heading").style.display = "block";
}
else if (music_visible && shows_visible) {
document.getElementById("music-heading").style.display = "block";
document.getElementById("shows-heading").style.display = "block";
}
}
function showDisplay(type) {
document.getElementById("server-error-indicator").style.display = "none";
document.getElementById("loading-indicator").style.display = "none";
// show movies section if active_section type is movie or forced by passing type to showDisplay
if (active_section || type) {
if ((active_section && active_section["type"] === "movie") || type === "movies") {
document.getElementById("movies-container").style.display = "block";
}
else if ((active_section && active_section["type"] === "show") || type === "shows") {
document.getElementById("shows-container").style.display = "block";
}
else if ((active_section && active_section["type"] === "artist") || type === "music") {
document.getElementById("music-container").style.display = "block";
}
}
showHeadings();
}
function hideDisplay() {
document.getElementById("movies-container").style.display = "none";
document.getElementById("shows-container").style.display = "none";
document.getElementById("music-container").style.display = "none";
document.getElementById("movies-heading").style.display = "none";
document.getElementById("shows-heading").style.display = "none";
document.getElementById("music-heading").style.display = "none";
document.getElementById("server-error-indicator").style.display = "none";
document.getElementById("server-updated").style.display = "none";
document.getElementById("loading-indicator").style.display = "block";
}
function getServerAddresses(callback) {
utils.background_storage_get("server_addresses", function(response) {
callback(response["value"]);
});
}
function getSections(uri, plex_token, callback) {
var library_sections_url = uri + "/library/sections?X-Plex-Token=" + plex_token;
utils.getXML(library_sections_url, function(sections_xml) {
callback(sections_xml);
});
}
function processLibrarySections(sections_xml) {
var directories = sections_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
var dir_metadata = {};
for (var i = 0; i < directories.length; i++) {
var title = directories[i].getAttribute("title");
var type = directories[i].getAttribute("type");
var scanner = directories[i].getAttribute("scanner");
var key = directories[i].getAttribute("key");
// only return movie or tv show libraries
if (type === "movie" || type === "show" || type === "artist") {
dir_metadata[key] = {"type": type, "title": title};
}
}
return dir_metadata;
}
function getAllMovies(uri, plex_token, section_key, callback) {
var library_section_url = uri + "/library/sections/" + section_key + "/all?X-Plex-Token=" + plex_token;
utils.getXML(library_section_url, function(section_xml) {
var movies_xml = section_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Video");
var movies = [];
for (var i = 0; i < movies_xml.length; i++) {
var movie_data = {};
movie_data["content_rating"] = movies_xml[i].getAttribute("contentRating");
movie_data["rating"] = movies_xml[i].getAttribute("rating");
movie_data["year"] = movies_xml[i].getAttribute("year");
movie_data["added_at"] = movies_xml[i].getAttribute("addedAt");
var metadata_xml = movies_xml[i].getElementsByTagName("Media")[0];
movie_data["video_resolution"] = metadata_xml.getAttribute("videoResolution");
movies.push(movie_data);
}
callback(movies);
});
}
function getAllShows(uri, plex_token, section_key, callback) {
var library_section_url = uri + "/library/sections/" + section_key + "/all?X-Plex-Token=" + plex_token;
utils.getXML(library_section_url, function(section_xml) {
var shows_xml = section_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
var shows = [];
for (var i = 0; i < shows_xml.length; i++) {
var show_data = {};
show_data["content_rating"] = shows_xml[i].getAttribute("contentRating");
show_data["rating"] = shows_xml[i].getAttribute("rating");
show_data["year"] = shows_xml[i].getAttribute("year");
show_data["duration"] = shows_xml[i].getAttribute("duration");
shows.push(show_data);
}
callback(shows);
});
}
function getAllEpisodes(uri, plex_token, section, callback) {
var library_section_episodes_url = uri + "/library/sections/" + section + "/all?type=4&X-Plex-Token=" + plex_token;
utils.getXML(library_section_episodes_url, function(section_xml) {
var episodes_xml = section_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Video");
var episodes = [];
for (var i = 0; i < episodes_xml.length; i++) {
var episode_data = {};
episode_data["added_at"] = episodes_xml[i].getAttribute("addedAt");
var metadata_xml = episodes_xml[i].getElementsByTagName("Media")[0];
episode_data["video_resolution"] = metadata_xml.getAttribute("videoResolution");
episodes.push(episode_data);
}
callback(episodes);
});
}
function getAllSongs(uri, plex_token, section, callback) {
var library_section_songs_url = uri + "/library/sections/" + section + "/all?type=10&X-Plex-Token=" + plex_token;
var container_size = 10000;
var container_start = 0;
var songs = [];
var paged_request = function() {
var paged_library_section_songs_url = library_section_songs_url + "&sort=titleSort:asc&X-Plex-Container-Size=" + container_size + "&X-Plex-Container-Start=" + container_start;
var p = new promise.Promise();
utils.getXML(paged_library_section_songs_url, function(section_xml) {
var songs_xml = section_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Track");
if (songs_xml.length === 0) {
return callback(songs);
}
container_start += container_size;
for (var i = 0; i < songs_xml.length; i++) {
var song_data = {};
song_data["added_at"] = songs_xml[i].getAttribute("addedAt");
var metadata_xml = songs_xml[i].getElementsByTagName("Media")[0];
song_data["bitrate"] = metadata_xml.getAttribute("bitrate");
songs.push(song_data);
}
return p.then(paged_request());
});
return p;
}
paged_request();
}
function getAllAlbums(uri, plex_token, section, callback) {
var library_section_albums_url = uri + "/library/sections/" + section + "/all?type=9&X-Plex-Token=" + plex_token;
utils.getXML(library_section_albums_url, function(section_xml) {
var albums_xml = section_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
var albums = [];
for (var i = 0; i < albums_xml.length; i++) {
var album_data = {};
album_data["year"] = albums_xml[i].getAttribute("year");
album_data["added_at"] = albums_xml[i].getAttribute("addedAt");
albums.push(album_data);
}
callback(albums);
});
}
function getSectionGenres(uri, plex_token, section_key, callback) {
var library_section_genres_url = uri + "/library/sections/" + section_key + "/genre?X-Plex-Token=" + plex_token;
utils.getXML(library_section_genres_url, function(genres_xml) {
var genre_nodes = genres_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
var genres = {};
for (var i = 0; i < genre_nodes.length; i++) {
var genre_key = genre_nodes[i].getAttribute("key");
var genre_title = genre_nodes[i].getAttribute("title");
genres[genre_key] = genre_title;
}
callback(genres);
});
}
function getMoviesByGenre(uri, plex_token, section_key, genre_key, callback) {
var filtered_movies_url = uri + "/library/sections/" + section_key + "/all?genre=" + genre_key + "&X-Plex-Token=" + plex_token;
utils.getXML(filtered_movies_url, function(movies_xml) {
var movies = movies_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Video");
callback(movies);
});
}
function getShowsByGenre(uri, plex_token, section_key, genre_key, callback) {
var filtered_shows_url = uri + "/library/sections/" + section_key + "/all?genre=" + genre_key + "&X-Plex-Token=" + plex_token;
utils.getXML(filtered_shows_url, function(shows_xml) {
var shows = shows_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
callback(shows);
});
}
function getAlbumsByGenre(uri, plex_token, section_key, genre_key, callback) {
var filtered_albums_url = uri + "/library/sections/" + section_key + "/all?genre=" + genre_key + "&X-Plex-Token=" + plex_token;
utils.getXML(filtered_albums_url, function(albums_xml) {
var albums = albums_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
callback(albums);
});
}
function generateMovieStats(movies, genre_count) {
var content_rating_count = {};
var movie_rating_count = {};
var resolution_count = {};
var year_count = {};
var dates_added = [];
for (var i = 0; i < movies.length; i++) {
// content rating count
var content_rating = movies[i]["content_rating"];
if (content_rating_count[content_rating]) {
content_rating_count[content_rating]++;
}
else {
content_rating_count[content_rating] = 1;
}
// movie ratings partioning
// round down movie rating so that ratings 4.0-4.9 = 4 etc
var movie_rating = parseInt(movies[i]["rating"]);
if (movie_rating_count[movie_rating]) {
movie_rating_count[movie_rating]++;
}
else {
movie_rating_count[movie_rating] = 1;
}
// resolutions count
var resolution = movies[i]["video_resolution"];
if (resolution_count[resolution]) {
resolution_count[resolution]++;
}
else {
resolution_count[resolution] = 1;
}
// years count
var year = parseInt(movies[i]["year"]);
if (year_count[year]) {
year_count[year]++;
}
else {
year_count[year] = 1;
}
// movies added over time
// set date time to beginning of day to make it easy to work with
var added_at = new Date(parseInt(movies[i]["added_at"]) * 1000).setHours(0, 0, 0, 0);
dates_added.push(added_at);
}
// clean up, remove invalid data
if (content_rating_count[null]) {
content_rating_count["Unknown"] = content_rating_count[null];
delete content_rating_count[null];
}
if (resolution_count[null]) {
resolution_count["Unknown"] = resolution_count[null];
delete resolution_count[null];
}
delete year_count[NaN];
// add missing years
var sorted_years = Object.keys(year_count).sort();
for (var j = sorted_years[0]; j < sorted_years[sorted_years.length - 1]; j++) {
if (!year_count[j]) {
year_count[j] = 0;
}
}
// collate movies added over time data
var sorted_dates = dates_added.sort(function(a, b) {return a - b;});
var today = new Date(Date.now());
var start_date = new Date(sorted_dates[0]);
var date_added_count = {};
var total_count = 0;
// iterate over dates from first movie added date added to today
for (var d = start_date; d <= today; d.setDate(d.getDate() + 1)) {
var current_timestamp = d.getTime();
var day_count = 0;
for (var i = 0; i < sorted_dates.length; i++) {
if (sorted_dates[i] === current_timestamp) {
day_count += 1;
}
}
// only add date to array if movies were added that day
if (day_count > 0) {
total_count += day_count;
var date_string = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
date_added_count[date_string] = total_count;
}
}
// format movie ratings data
for (var rating in movie_rating_count) {
if (isNaN(rating)) {
movie_rating_count["No Rating"] = movie_rating_count[NaN];
}
else {
var formatted_rating = rating + ".0 - " + rating + ".9";
movie_rating_count[formatted_rating] = movie_rating_count[rating];
}
delete movie_rating_count[rating];
}
// group 9.0-10.0 ratings together
if (movie_rating_count["9.0 - 9.9"] || movie_rating_count["10.0 - 10.9"]) {
movie_rating_count["9.0 - 10.0"] = (movie_rating_count["9.0 - 9.9"] || 0) + (movie_rating_count["10.0 - 10.9"] || 0);
delete movie_rating_count["9.0 - 9.9"];
delete movie_rating_count["10.0 - 10.9"];
}
// format movie resolutions data
for (var resolution in resolution_count) {
if (resolution_mappings[resolution]) {
resolution_count[resolution_mappings[resolution]] = resolution_count[resolution];
delete resolution_count[resolution];
}
}
return {
"content_rating_count": content_rating_count,
"movie_rating_count": movie_rating_count,
"resolution_count": resolution_count,
"year_count": year_count,
"genre_count": genre_count,
"date_added_count": date_added_count
};
}
function generateShowStats(shows, episodes, genre_count) {
var content_rating_count = {};
var show_rating_count = {};
var resolution_count = {};
var year_count = {};
var episodes_dates_added = [];
for (var i = 0; i < shows.length; i++) {
// content rating count
var content_rating = shows[i]["content_rating"];
if (content_rating_count[content_rating]) {
content_rating_count[content_rating]++;
}
else {
content_rating_count[content_rating] = 1;
}
// show ratings partioning
// round down show rating so that ratings 4.0-4.9 = 4 etc
var movie_rating = parseInt(shows[i]["rating"]);
if (show_rating_count[movie_rating]) {
show_rating_count[movie_rating]++;
}
else {
show_rating_count[movie_rating] = 1;
}
// years count
var year = parseInt(shows[i]["year"]);
if (year_count[year]) {
year_count[year]++;
}
else {
year_count[year] = 1;
}
}
// iterate over all episodes
for (var j = 0; j < episodes.length; j++) {
// resolutions count
var resolution = episodes[j]["video_resolution"];
if (resolution_count[resolution]) {
resolution_count[resolution]++;
}
else {
resolution_count[resolution] = 1;
}
// episodes added over time
// set date time to beginning of day to make it easy to work with
var added_at = new Date(parseInt(episodes[j]["added_at"]) * 1000).setHours(0, 0, 0, 0);
episodes_dates_added.push(added_at);
}
// clean up, remove invalid data
if (content_rating_count[null]) {
content_rating_count["Unknown"] = content_rating_count[null];
delete content_rating_count[null];
}
if (resolution_count[null]) {
resolution_count["Unknown"] = resolution_count[null];
delete resolution_count[null];
}
delete year_count[NaN];
// add missing years
var sorted_years = Object.keys(year_count).sort();
for (var j = sorted_years[0]; j < sorted_years[sorted_years.length - 1]; j++) {
if (!year_count[j]) {
year_count[j] = 0;
}
}
// collate episodes added over time data
var sorted_dates = episodes_dates_added.sort(function(a, b) {return a - b;});
var today = new Date(Date.now());
var start_date = new Date(sorted_dates[0]);
var episodes_date_added_count = {};
var total_count = 0;
// iterate over dates from first movie added date added to today
for (var d = start_date; d <= today; d.setDate(d.getDate() + 1)) {
var current_timestamp = d.getTime();
var day_count = 0;
for (var i = 0; i < sorted_dates.length; i++) {
if (sorted_dates[i] === current_timestamp) {
day_count += 1;
}
}
// only add date to array if shows were added that day
if (day_count > 0) {
total_count += day_count
var date_string = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
episodes_date_added_count[date_string] = total_count;
}
}
// format show ratings data
for (var rating in show_rating_count) {
if (isNaN(rating)) {
show_rating_count["No Rating"] = show_rating_count[NaN];
}
else {
var formatted_rating = rating + ".0 - " + rating + ".9";
show_rating_count[formatted_rating] = show_rating_count[rating];
}
delete show_rating_count[rating];
}
// group 9.0-10.0 ratings together
if (show_rating_count["9.0 - 9.9"] || show_rating_count["10.0 - 10.9"]) {
show_rating_count["9.0 - 10.0"] = (show_rating_count["9.0 - 9.9"] || 0) + (show_rating_count["10.0 - 10.9"] || 0);
delete show_rating_count["9.0 - 9.9"];
delete show_rating_count["10.0 - 10.9"];
}
// format movie resolutions data
for (var resolution in resolution_count) {
if (resolution_mappings[resolution]) {
resolution_count[resolution_mappings[resolution]] = resolution_count[resolution];
delete resolution_count[resolution];
}
}
return {
"content_rating_count": content_rating_count,
"show_rating_count": show_rating_count,
"resolution_count": resolution_count,
"year_count": year_count,
"genre_count": genre_count,
"episodes_date_added_count": episodes_date_added_count
};
}
function generateMusicStats(songs, albums, genre_count) {
var bitrate_count = {};
var year_count = {};
var songs_dates_added = [];
var albums_dates_added = [];
for (var i = 0; i < songs.length; i++) {
// bitrates count
var bitrate = songs[i]["bitrate"];
if (bitrate_count[bitrate]) {
bitrate_count[bitrate]++;
}
else {
bitrate_count[bitrate] = 1;
}
// songs added over time
// set date time to beginning of day to make it easy to work with
var song_added_at = new Date(parseInt(songs[i]["added_at"]) * 1000).setHours(0, 0, 0, 0);
songs_dates_added.push(song_added_at);
}
for (var i = 0; i < albums.length; i++) {
// years count
var year = parseInt(albums[i]["year"]);
if (year_count[year]) {
year_count[year]++;
}
else {
year_count[year] = 1;
}
// albums added over time
// set date time to beginning of day to make it easy to work with
var album_added_at = new Date(parseInt(albums[i]["added_at"]) * 1000).setHours(0, 0, 0, 0);
albums_dates_added.push(album_added_at);
}
// clean up, remove invalid data
delete bitrate_count[null];
delete year_count[NaN];
// add missing years
var sorted_years = Object.keys(year_count).sort();
for (var j = sorted_years[0]; j < sorted_years[sorted_years.length - 1]; j++) {
if (!year_count[j]) {
year_count[j] = 0;
}
}
// collate songs added over time data
var sorted_song_dates = songs_dates_added.sort(function(a, b) {return a - b;});
var today = new Date(Date.now());
var start_date = new Date(sorted_song_dates[0]);
var songs_date_added_count = {};
var total_count = 0;
// iterate over dates from first song added date added to today
for (var d = start_date; d <= today; d.setDate(d.getDate() + 1)) {
var current_timestamp = d.getTime();
var day_count = 0;
for (var i = 0; i < sorted_song_dates.length; i++) {
if (sorted_song_dates[i] === current_timestamp) {
day_count += 1;
}
}
// only add date to array if songs were added that day
if (day_count > 0) {
total_count += day_count;
var date_string = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
songs_date_added_count[date_string] = total_count;
}
}
// collate albums added over time data
var sorted_album_dates = albums_dates_added.sort(function(a, b) {return a - b;});
var today = new Date(Date.now());
var start_date = new Date(sorted_album_dates[0]);
var albums_date_added_count = {};
var total_count = 0;
// iterate over dates from first album added date added to today
for (var d = start_date; d <= today; d.setDate(d.getDate() + 1)) {
var current_timestamp = d.getTime();
var day_count = 0;
for (var i = 0; i < sorted_album_dates.length; i++) {
if (sorted_album_dates[i] === current_timestamp) {
day_count += 1;
}
}
// only add date to array if albums were added that day
if (day_count > 0) {
total_count += day_count;
var date_string = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
albums_date_added_count[date_string] = total_count;
}
}
return {
"bitrate_count": bitrate_count,
"year_count": year_count,
"genre_count": genre_count,
"date_added_count": {"songs": songs_date_added_count, "albums": albums_date_added_count}
};
}
function generateStats(uri, plex_token, callback) {
utils.debug("Generating stats for " + uri);
var all_movies = [];
var all_shows = [];
var all_episodes = [];
var all_songs = [];
var all_albums = [];
var movie_genres_count = {};
var show_genres_count = {};
var album_genres_count = {};
var section_movies = {};
var section_shows = {};
var section_episodes = {};
var section_songs = {};
var section_albums = {};
var section_movie_genres_count = {};
var section_show_genres_count = {};
var section_album_genres_count = {};
getSections(uri, plex_token, function(sections_xml) {
// check if no response from server
if (!sections_xml) {
callback(null);
return;
}
var processed_sections = processLibrarySections(sections_xml);
// set up counter to keep track of running tasks
var task_counter = 0;
var task_completed = function() {
utils.debug("Data task finished");
task_counter--;
// check if all async tasks are finished
if (task_counter === 0) {
utils.debug("All data tasks finished");
var movie_stats = generateMovieStats(all_movies, movie_genres_count);
var show_stats = generateShowStats(all_shows, all_episodes, show_genres_count);
var music_stats = generateMusicStats(all_songs, all_albums, album_genres_count);
var per_section_movie_stats = {};
for (var section_key in section_movies) {
var section_movie_stats = generateMovieStats(section_movies[section_key], section_movie_genres_count[section_key]);
per_section_movie_stats[section_key] = section_movie_stats;
}
var per_section_show_stats = {};
for (var section_key in section_shows) {
var section_show_stats = generateShowStats(section_shows[section_key], section_episodes[section_key], section_show_genres_count[section_key]);
per_section_show_stats[section_key] = section_show_stats;
}
var per_section_music_stats = {};
for (var section_key in section_albums) {
var section_music_stats = generateMusicStats(section_songs[section_key], section_albums[section_key], section_album_genres_count[section_key]);
per_section_music_stats[section_key] = section_music_stats;
}
callback(movie_stats, per_section_movie_stats, show_stats, per_section_show_stats, music_stats, per_section_music_stats);
}
};
for (var section_key in processed_sections) {
// use closures because of scoping issues
(function (section_key) {
if (processed_sections[section_key]["type"] === "movie") {
task_counter++;
section_movie_genres_count[section_key] = {};
// get all movies for section
getAllMovies(uri, plex_token, section_key, function(movies) {
all_movies = all_movies.concat(movies);
section_movies[section_key] = movies;
// because the plex web api calls for library sections only returns the first two genres
// of each movie we need to get all the genre mappings first and count the number of movies
// returned by the api with that genre filtered out
getSectionGenres(uri, plex_token, section_key, function(genres) {
task_counter += Object.keys(genres).length;
for (var genre_key in genres) {
(function (genre_key) {
var genre_title = genres[genre_key];
getMoviesByGenre(uri, plex_token, section_key, genre_key, function(genre_movies) {
if (movie_genres_count[genre_title]) {
movie_genres_count[genre_title] += genre_movies.length;
}
else {
movie_genres_count[genre_title] = genre_movies.length;
}
if (section_movie_genres_count[section_key][genre_title]) {
section_movie_genres_count[section_key][genre_title] += genre_movies.length;
}
else {
section_movie_genres_count[section_key][genre_title] = genre_movies.length;
}
task_completed();
});
}(genre_key));
}
task_completed();
});
});
}
else if (processed_sections[section_key]["type"] === "show") {
task_counter++;
section_show_genres_count[section_key] = {};
section_episodes[section_key] = [];
// get all tv shows for section
getAllShows(uri, plex_token, section_key, function(shows) {
all_shows = all_shows.concat(shows);
section_shows[section_key] = shows;
// because the plex web api calls for library sections only returns the first two genres
// of each show we need to get all the genre mappings first and count the number of shows
// returned by the api with that genre filtered out
getSectionGenres(uri, plex_token, section_key, function(genres) {
task_counter += Object.keys(genres).length;
for (var genre_key in genres) {
(function (genre_key) {
var genre_title = genres[genre_key];
getShowsByGenre(uri, plex_token, section_key, genre_key, function(genre_shows) {
if (show_genres_count[genre_title]) {
show_genres_count[genre_title] += genre_shows.length;
}
else {
show_genres_count[genre_title] = genre_shows.length;
}
if (section_show_genres_count[section_key][genre_title]) {
section_show_genres_count[section_key][genre_title] += genre_shows.length;
}
else {
section_show_genres_count[section_key][genre_title] = genre_shows.length;
}
task_completed();
});
}(genre_key));
}
task_completed();
});
});
// get all tv show episodes for section
task_counter++;
getAllEpisodes(uri, plex_token, section_key, function(episodes) {
all_episodes = all_episodes.concat(episodes);
section_episodes[section_key] = section_episodes[section_key].concat(episodes);
task_completed();
});
}
else if (processed_sections[section_key]["type"] === "artist") {
task_counter++;
section_album_genres_count[section_key] = {};
// get all songs for section
getAllSongs(uri, plex_token, section_key, function(songs) {
all_songs = all_songs.concat(songs);
section_songs[section_key] = songs;
// because the plex web api calls for library sections only returns the first two genres
// of each album we need to get all the genre mappings first and count the number of albums
// returned by the api with that genre filtered out
getSectionGenres(uri, plex_token, section_key, function(genres) {
task_counter += Object.keys(genres).length;
for (var genre_key in genres) {
(function (genre_key) {
var genre_title = genres[genre_key];
getAlbumsByGenre(uri, plex_token, section_key, genre_key, function(genre_albums) {
if (album_genres_count[genre_title]) {
album_genres_count[genre_title] += genre_albums.length;
}
else {
album_genres_count[genre_title] = genre_albums.length;
}
if (section_album_genres_count[section_key][genre_title]) {
section_album_genres_count[section_key][genre_title] += genre_albums.length;
}
else {
section_album_genres_count[section_key][genre_title] = genre_albums.length;
}
task_completed();
});
}(genre_key));
}
task_completed();
});
});
task_counter++;
getAllAlbums(uri, plex_token, section_key, function(albums) {
all_albums = all_albums.concat(albums);
section_albums[section_key] = albums;
task_completed();
});
}
}(section_key));
}
});
}
function getStats(server, section, force, callback) {
var machine_identifier = server["machine_identifier"];
var name = server["name"];
var uri = server["uri"];
var plex_token = server["access_token"];
var cache_key;
if (section) {
// get section stats
cache_key = "cache-stats-" + machine_identifier + "-" + section;
}
else {
// get server stats
cache_key = "cache-stats-" + machine_identifier;
}
utils.local_storage_get(cache_key, function(data) {
// if force is true then we are recalculating stats
if (data && !force) {
utils.debug("Cache hit for " + cache_key);
var timestamp = data["timestamp"];
if (section) {
var stats = data["stats"];
callback(stats, timestamp);
}
else {
var movie_stats = data["movie_stats"];
var show_stats = data["show_stats"];
var music_stats = data["music_stats"];
callback({"movie_stats": movie_stats, "show_stats": show_stats, "music_stats": music_stats}, timestamp);
}
}
else {
utils.debug("Cache miss for " + cache_key);
generateStats(uri, plex_token, function(movie_stats, movie_section_stats, show_stats, show_section_stats, music_stats, music_section_stats) {
if (movie_stats === null) {
// couldn't reach server to get data
utils.debug("Couldn't reach server " + uri + " to get stat data");
callback(null);
return;
}
var timestamp = new Date().getTime();
var hash = {"name": name, "movie_stats": movie_stats, "show_stats": show_stats, "music_stats": music_stats, "timestamp": timestamp};
utils.local_storage_set(cache_key, hash);
for (var section_key in movie_section_stats) {
var section_hash = {"stats": movie_section_stats[section_key], "timestamp": timestamp};
utils.local_storage_set("cache-stats-" + machine_identifier + "-" + section_key, section_hash);
}
for (var section_key in show_section_stats) {
var section_hash = {"stats": show_section_stats[section_key], "timestamp": timestamp};
utils.local_storage_set("cache-stats-" + machine_identifier + "-" + section_key, section_hash);
}
for (var section_key in music_section_stats) {
var section_hash = {"stats": music_section_stats[section_key], "timestamp": timestamp};
utils.local_storage_set("cache-stats-" + machine_identifier + "-" + section_key, section_hash);
}
callback({"movie_stats": movie_stats, "show_stats": show_stats, "music_stats": music_stats}, timestamp);
});
}
});
}
function recalculateServerStats() {
hideDisplay();
switchToServer(servers[active_server], null, true);
}
function updateNav() {
// set active server name on nav bar
var server_name_element = document.getElementById("active-server-name");
// clear what's already there
while (server_name_element.firstChild) {
server_name_element.removeChild(server_name_element.firstChild);
}
var server_name_text_node = document.createTextNode(servers[active_server]["name"]);
server_name_element.appendChild(server_name_text_node);
if (active_section) {
var section_name = active_section["title"];
var section_name_span = document.createElement("span");
section_name_span.setAttribute("id", "active-section-name");
var section_name_text_node = document.createTextNode("(" + section_name+ ")");
section_name_span.appendChild(section_name_text_node);
server_name_element.appendChild(section_name_span);
}
}
function setServerSelections() {
var server_list_element = document.getElementById("server-choices");
// add all server choices
for (var server in servers) {
var li = document.createElement("li");
var server_element = document.createElement("a");
server_element.setAttribute("href", "#");
server_element.setAttribute("class", "server-choice");
server_element.setAttribute("data-machine_identifier", servers[server]["machine_identifier"]);
var text_node = document.createTextNode(servers[server]["name"]);
server_element.appendChild(text_node);
li.appendChild(server_element);
server_list_element.appendChild(li);
}
}
function addSectionSelections() {
for (var server in servers) {
(function (server) {
sections[server] = {};
getSections(servers[server]["uri"], servers[server]["access_token"], function(sections_xml) {
var server_picker;
var server_choices = document.getElementsByClassName("server-choice");
for (var i = 0; i < server_choices.length; i++) {
if (server_choices[i].getAttribute("data-machine_identifier") === servers[server]["machine_identifier"]) {
server_picker = server_choices[i].parentNode;
break;
}
}
var ul = document.createElement("ul");
server_picker.appendChild(ul);
// create All option, to select collated server stats
var li = document.createElement("li");
var all_section_element = document.createElement("a");
all_section_element.setAttribute("href", "#");
all_section_element.setAttribute("class", "section-choice");
all_section_element.setAttribute("id", "server-all-choice");
all_section_element.setAttribute("data-machine_identifier", servers[server]["machine_identifier"]);
// no section_key data attribute so when clicked getStats() returns collated server stats
var text_node = document.createTextNode("All");
all_section_element.appendChild(text_node);
li.appendChild(all_section_element);
ul.appendChild(li);
// add event handler
all_section_element.addEventListener("click", switchSection, false);
if (!sections_xml) {
// couldn't reach server to get sections data
utils.debug("Couldn't reach server " + servers[server]["name"] + " at " + servers[server]["uri"]);
utils.debug("Skipping sections for this server");
return;
}
var processed_sections = processLibrarySections(sections_xml);
// sort section keys by section type
var sorted_keys = Object.keys(processed_sections).sort(function(a, b) {
return processed_sections[a]["type"] > processed_sections[b]["type"];