-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnit2.pas
3709 lines (3206 loc) · 122 KB
/
Unit2.pas
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
unit Unit2;
interface
uses
Winapi.Windows,
Winapi.Messages,
Winapi.shellapi,
PsAPI,
TlHelp32,
System.SysUtils,
System.Variants,
System.Math,
System.Classes,
System.JSON,
System.Generics.Collections,
System.NetEncoding,
System.DateUtils,
System.StrUtils,
System.IOUTils,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Vcl.ExtCtrls,
idURI,
IdGlobalProtocols,
IdStack,
IdGlobal,
IdBaseComponent,
IdComponent,
IdTCPConnection,
IdTCPClient,
IdHTTP,
IdMessageClient,
IdMessage,
IdMessageBuilder,
IdAttachment,
IdMessageParts,
IdEMailAddress,
IdAttachmentFile,
IdSMTPBase,
IdSMTP,
IdAttachmentMemory,
System.Net.URLClient,
System.Net.HttpClientComponent,
System.Net.HttpClient,
Vcl.WinXPickers,
Vcl.ComCtrls,
Unit1, Vcl.Imaging.pngimage;
type
TMainForm = class(TForm)
sparqlBirthDays: TMemo;
CacheTimer: TTimer;
StartTimer: TTimer;
sparqlDeathDays: TMemo;
sparqlReleaseDays: TMemo;
Panel1: TPanel;
btStart: TButton;
btStop: TButton;
btSwagger: TButton;
edSecret: TEdit;
lbSecret: TLabel;
Label1: TLabel;
lbTMDbAPI: TLabel;
edSecretBase64: TEdit;
edTMDbAPI: TEdit;
btTimer: TButton;
btRecentProgress: TButton;
btClear: TButton;
progDay: TEdit;
progMonth: TEdit;
DateTimePickerBirthday: TDateTimePicker;
DateTimePickerDeathDay: TDateTimePicker;
DateTimePickerReleaseDay: TDateTimePicker;
btTop1000: TButton;
btTop5000: TButton;
btAll: TButton;
edtClientVersion: TEdit;
btUpdateVersion: TButton;
tmrVersionCheck: TTimer;
tmrTopUpdate: TTimer;
sparqlRelatives: TMemo;
tmrWaiting: TTimer;
btInternal: TButton;
tmrProgress: TTimer;
mmStats: TMemo;
btClean: TButton;
ckRegenerate: TCheckBox;
btRedoc: TButton;
mmInfo: TMemo;
btEmail: TButton;
lblBirthDays: TLabel;
lblDeathDays: TLabel;
lblReleaseDays: TLabel;
lblSearchPeople: TLabel;
lblSearchMovies: TLabel;
lblSearchTVShow: TLabel;
Shape1: TShape;
PanelA: TPanel;
ProgressDetailA: TLabel;
CurrentProgressA: TLabel;
shapeProgressFGA: TShape;
shapeProgressBG: TShape;
ProgressStepA: TLabel;
PanelB: TPanel;
Shape2: TShape;
shapeProgressFGB: TShape;
ProgressStepB: TLabel;
ProgressDetailB: TLabel;
CurrentProgressB: TLabel;
PanelC: TPanel;
Shape4: TShape;
shapeProgressFGC: TShape;
ProgressStepC: TLabel;
ProgressDetailC: TLabel;
CurrentProgressC: TLabel;
PanelD: TPanel;
Shape6: TShape;
shapeProgressFGD: TShape;
ProgressStepD: TLabel;
ProgressDetailD: TLabel;
CurrentProgressD: TLabel;
PanelE: TPanel;
Shape8: TShape;
shapeProgressFGE: TShape;
ProgressStepE: TLabel;
ProgressDetailE: TLabel;
CurrentProgressE: TLabel;
PanelF: TPanel;
Shape10: TShape;
shapeProgressFGF: TShape;
ProgressStepF: TLabel;
ProgressDetailF: TLabel;
CurrentProgressF: TLabel;
Label2: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
procedure GetAppVersionString;
procedure btStartClick(ASender: TObject);
procedure btStopClick(ASender: TObject);
procedure FormCreate(ASender: TObject);
procedure edSecretChange(Sender: TObject);
procedure CacheTimerTimer(Sender: TObject);
procedure btRecentProgressClick(Sender: TObject);
procedure NetHTTPClient1RequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);
procedure ManualRequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);
procedure ManualRequestError(const Sender: TObject; const AError: string);
procedure NetHTTPClient1RequestError(const Sender: TObject; const AError: string);
procedure btSwaggerClick(Sender: TObject);
procedure StartTimerTimer(Sender: TObject);
procedure btTimerClick(Sender: TObject);
procedure btClearClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure DateTimePickerBirthdayCloseUp(Sender: TObject);
procedure DateTimePickerDeathDayCloseUp(Sender: TObject);
procedure btTop1000Click(Sender: TObject);
procedure GetTopResults(TopURL: String; TopURLName: String);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
function GetProcessThreadCount(ProcessId: Cardinal): Integer;
procedure btTop5000Click(Sender: TObject);
procedure btAllClick(Sender: TObject);
procedure edtClientVersionChange(Sender: TObject);
procedure btUpdateVersionClick(Sender: TObject);
procedure UpdateHomeAssistant;
procedure tmrVersionCheckTimer(Sender: TObject);
procedure tmrTopUpdateTimer(Sender: TObject);
procedure DateTimePickerReleaseDayCloseUp(Sender: TObject);
procedure tmrWaitingTimer(Sender: TObject);
procedure btInternalClick(Sender: TObject);
procedure tmrProgressTimer(Sender: TObject);
procedure btCleanClick(Sender: TObject);
procedure btRedocClick(Sender: TObject);
procedure LogEvent(Details: String);
procedure LogException(Source: String; EClass: String; EMessage: String; Data: String);
procedure SendActivityLog(Subject: String);
procedure btEmailClick(Sender: TObject);
procedure GetAppParameters(List: TStringList);
function SearchPeople(ActorID: Integer):Integer;
function SearchMovies(MovieID: Integer):Integer;
procedure UpdateSearch(SearchIndex: Integer; SearchData: String);
function LocalSearchPeople(SearchTerm: String; Adult: Boolean):String;
function LocalSearchMovies(SearchTerm: String; Adult: Boolean):String;
procedure lblSearchPeopleClick(Sender: TObject);
function Occurrences(const Substring, Text: string): integer;
procedure SaveSearchIndexes;
procedure LoadSearchIndexes;
procedure UpdateProgress(PNum: Integer; PInfo: String; PDetail:String; PStep:String; PReport: String);
procedure lblSearchMoviesClick(Sender: TObject);
procedure lblSearchTVShowClick(Sender: TObject);
public
Progress: TStringList;
WaitingMessage: String;
LastURL: String;
PersonCacheRequests,
PersonCacheHit,
PersonCacheForce,
PersonCacheMiss,
PersonCacheAge: Integer;
MovieCacheRequests,
MovieCacheHit,
MovieCacheForce,
MovieCacheMiss,
MovieCacheAge: Integer;
TVShowCacheRequests,
TVShowCacheHit,
TVShowCacheForce,
TVShowCacheMiss,
TVShowCacheAge: Integer;
CleanRequests,
CleanSmall,
CleanPeople,
CleanDays,
CleanMovies,
CleanTVShows: Integer;
CleanSize: Int64;
CleanFiles: Integer;
BirthDaysCount: String;
AppStartup: TDateTime;
AppParameters: TStringList;
AppConfiguration: TJSONObject;
AppBaseURL: String;
AppURL: String;
AppSwagger: String;
AppRedoc: String;
AppHAURL: String;
AppHAToken: String;
AppCacheDir: String;
AppCacheSkips: Integer;
LastException: TDateTime;
MailServerAvailable: Boolean;
MailServerHost: String;
MailServerPort: Integer;
MailServerUser: String;
MailServerPass: String;
MailServerFrom: String;
MailServerName: String;
LookupCache: TDictionary<String,String>;
CacheFiles: String;
CacheSize: String;
strict private
procedure UpdateGUI;
end;
TFancyNetHTTPClient = class(TNetHTTPClient)
private
local_Description: String;
local_CacheFile: String;
local_URL: String;
public
property Description: String read local_Description write local_Description;
property CacheFile: String read local_CacheFile write local_CacheFile;
property URL: String read local_URL write local_URL;
end;
var
MainForm: TMainForm;
AppVersionString: String;
AppVersion: String;
AppRelease: String;
MemoryUsage: String;
MemoryUsageNice: String;
IndexPeople: Array of String;
IndexMovies: Array of String;
IndexTVShow: Array of String;
IndexPeopleSize: Integer;
IndexMoviesSize: Integer;
IndexTVShowSize: Integer;
implementation
{$R *.dfm}
resourcestring
SServerStopped = 'Server stopped';
SServerStartedAt = 'Server started at ';
{ TMainForm }
function TMainForm.Occurrences(const Substring, Text: string): integer;
var
offset: integer;
begin
result := 0;
offset := PosEx(Substring, Text, 1);
while offset <> 0 do
begin
inc(result);
offset := PosEx(Substring, Text, offset + length(Substring));
end;
end;
procedure TMainForm.GetAppParameters(List: TStringList);
var
i: Integer;
begin
i := 1;
while i <= ParamCount do
begin
List.Add('"'+ParamStr(i)+'"');
i := i + 1;
end;
end;
procedure TMainForm.btTimerClick(Sender: TObject);
begin
if btTimer.Tag = 0 then
begin
btTimer.Tag := 1;
btTimer.Caption := 'Disable Timer';
tmrTopUpdate.Enabled := True;
tmrVersionCheck.Enabled := True;
end
else
begin
btTimer.Tag := 0;
btTimer.Caption := 'Enable Timer';
tmrTopUpdate.Enabled := False;
tmrVersionCheck.Enabled := False;
end;
end;
procedure TMainForm.btTop1000Click(Sender: TObject);
var
URL: String;
Client: TFancyNetHTTPClient;
begin
LogEvent('');
LogEvent('Regenerating Top1000 Data');
CurrentProgressA.Caption := 'ManReGenTop1000: '+TGUID.NewGUID.ToString;
CacheTimer.Enabled := False;
URL := AppURL+'/ActorInfoService/TopOneThousand';
// Setup the Request
URL := URL+'?Secret='+edSecret.Text;
URL := URL+'&Segment=A';
URL := URL+'&Progress='+CurrentProgressA.Caption;
// Submit the request (asynchronously)
Client := TFancyNetHTTPClient.Create(nil);
Client.Tag := DateTimeToUnix(Now);
Client.Description := 'Top 1000';
Client.Asynchronous := True;
Client.ConnectionTimeout := 5400000; // 90 minutes
Client.ResponseTimeout := 5400000; // 90 minutes
Client.onRequestCompleted := ManualRequestCompleted;
Client.onRequestError := ManualRequestError;
Client.URL := TidURI.URLEncode(URL);
if Pos('https', URL) > 0
then Client.SecureProtocols := [THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS12];
try
Client.Get(Client.URL);
except on E: Exception do
begin
LogException('Regen Top1000', E.ClassName, E.Message, Client.URL);
end;
end;
end;
procedure TMainForm.GetTopResults(TopURL: String; TopURLName: String);
var
URL: String;
// Client: TFancyNetHTTPClient;
begin
LogEvent('Caching '+TopURLName+' ['+TopURL+']');
CurrentProgressA.Caption := 'ManReGen'+TopURLName+': '+TGUID.NewGUID.ToString;
CacheTimer.Enabled := False;
URL := TopURL;
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
//
// // Submit the request (asynchronously)
// Client := TFancyNetHTTPClient.Create(nil);
// Client.Tag := DateTimeToUnix(Now);
// Client.Description := TopURLName;
// Client.Asynchronous := True;
// Client.ConnectionTimeout := 30000; // 5 minutes
// Client.ResponseTimeout := 30000; // 5 minutes
// Client.onRequestCompleted := ManualRequestCompleted;
// Client.onRequestError := ManualRequestError;
// Client.URL := TidURI.URLEncode(URL);
// if Pos('https', URL) > 0
// then Client.SecureProtocols := [THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS12];
// try
// Client.Get(Client.URL);
// except on E: Exception do
// begin
// LogException('Get '+TopURLName, E.ClassName, E.Message, Client.URL);
// end;
// end;
end;
procedure TMainForm.btTop5000Click(Sender: TObject);
var
URL: String;
Client: TFancyNetHTTPClient;
begin
LogEvent('');
LogEvent('Regenerating Top5000 Data');
CurrentProgressA.Caption := 'ManReGenTop5000: '+TGUID.NewGUID.ToString;
CacheTimer.Enabled := False;
URL := AppURL+'/ActorInfoService/TopFiveThousand';
// Setup the Request
URL := URL+'?Secret='+edSecret.Text;
URL := URL+'&Segment=A';
URL := URL+'&Progress='+CurrentProgressA.Caption;
// Submit the request (asynchronously)
Client := TFancyNetHTTPClient.Create(nil);
Client.Tag := DateTimeToUnix(Now);
Client.Description := 'Top 5000';
client.Asynchronous := True;
Client.ConnectionTimeout := 5400000; // 90 minutes
Client.ResponseTimeout := 5400000; // 90 minutes
Client.onRequestCompleted := ManualRequestCompleted;
Client.onRequestError := ManualRequestError;
Client.URL := TidURI.URLEncode(URL);
if Pos('https', URL) > 0
then Client.SecureProtocols := [THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS12];
try
Client.Get(Client.URL);
except on E: Exception do
begin
LogException('Regen Top5000', E.ClassName, E.Message, Client.URL);
end;
end;
end;
procedure TMainForm.btUpdateVersionClick(Sender: TObject);
var
URL: String;
Client: TFancyNetHTTPClient;
Response: String;
NewVersion: String;
begin
URL := 'https://www.actorious.com/ActoriousClient.version';
// Submit the request (asynchronously)
Client := TFancyNetHTTPClient.Create(nil);
Client.SecureProtocols := [THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS12];
try
Response := Client.Get(TidURI.URLEncode(URL)).ContentAsString;
except on E: Exception do
begin
LogException('Version Check', E.ClassName, E.Message, URL);
end;
end;
if (Pos('ActoriousClient', Response) > 0) then
begin
// Just want the last digits
Response := Trim(Response);
NewVersion := Copy(Response,Pos('ActoriousClient_',Response)+16,length(Response));
NewVersion := Copy(NewVersion,1,Pos('";',NewVersion)-1);
NewVersion := StringReplace(NewVersion,'_','.',[rfReplaceAll]);
if (edtClientVersion.Text <> NewVersion) then
begin
edtClientVersion.Text := NewVersion;
LogEvent('- Actorious Client Version Updated: '+NewVersion);
end;
end;
Client.Free;
UpdateHomeAssistant;
end;
procedure TMainForm.UpdateHomeAssistant;
var
Client: TNetHTTPClient;
URL: String;
Token: String;
Endpoint: String;
Data: TStringStream;
Response: String;
begin
// NOTE: AppStartup, MemoryUsage, AppVersion and AppRelease are Form Variables defined elsewhere
// Decide if you're going to use a Home Assistant Internal vs. External URL
// And that they might differ in whether SSL is used
URL := AppHAURL;
Token := AppHAToken;
if AppHAURL <> '' then
begin
// Setup the Main Request
Client := TNetHTTPClient.Create(nil);
if Pos('https', URL) > 0
then Client.SecureProtocols := [THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS12];
Client.ContentType := 'application/json';
Client.CustomHeaders['Authorization'] := 'Bearer '+Token;
try
Endpoint := '/api/states/sensor.actorious_server_start';
Data := TStringStream.Create('{"state": "'+FormatDateTime('mmm dd (ddd) hh:nn', AppStartup)+'" }');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_runtime';
Data := TStringStream.Create('{"state": "'+IntToStr(DaysBetween(Now, AppStartup))+'d '+FormatDateTime('h"h "n"m"', Now-AppStartup)+'" }');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_version';
Data := TStringStream.Create('{"state": "'+AppVersion+'" }');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_release';
Data := TStringStream.Create('{"state": "'+AppRelease+'" }');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_memory';
Data := TStringStream.Create('{"state":'+MemoryUsage+', "attributes":{"unit_of_measurement":"MB"}}');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_memory_nice';
Data := TStringStream.Create('{"state":"'+MemoryUsageNice+'", "attributes":{"unit_of_measurement":"MB"}}');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_working_date';
Data := TStringStream.Create('{"state":"'+ProgMonth.text+' '+ProgDay.Text+'"}');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_cache_files';
Data := TStringStream.Create('{"state":"'+CacheFiles+'", "attributes":{"unit_of_measurement":"Files"}}');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_cache_size';
Data := TStringStream.Create('{"state":"'+CacheSize+'", "attributes":{"unit_of_measurement":"GB"}}');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Endpoint := '/api/states/sensor.actorious_server_statistics';
Data := TStringStream.Create('{"state":"'+BirthDaysCount+' / '+lblBirthDays.Caption+' / '+lblDeathDays.Caption+' / '+lblReleaseDays.Caption+'"}');
Response := Client.Post(URL+Endpoint, Data).ContentAsString;
if Pos('"entity_id"', Response) = 0 then LogEvent(Response);
Data.Free();
Client.Free;
except on E: Exception do
begin
LogException('Update Home Assistant', E.ClassName, E.Message, URL+Endpoint);
end;
end;
end;
end;
procedure TMainForm.UpdateSearch(SearchIndex: Integer; SearchData: String);
var
SearchFormat: String;
begin
SearchFormat := Uppercase(SearchData);
SearchFormat := StringReplace(SearchFormat,' ','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'''','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'"','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'/','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'-','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,',','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'.','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'(','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,')','',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'HERSELFGUEST:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'HIMSELFGUEST:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'HERSELF:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'HIMSELF:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'SELFGUEST:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'SELF:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'GUEST:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'VOICE:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'VOICES:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'UNCREDITED:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'ARCHIVALFOOTAGE:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'ARCHIVEFOOTAGE:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'FOOTAGE:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,':HERSELF:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,':HIMSELF:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,':SELF:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,':VOICE:',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'::',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'::',':',[rfReplaceAll]);
SearchFormat := StringReplace(SearchFormat,'::',':',[rfReplaceAll]);
if (Copy(SearchData,1,1) = 'Y') or (Copy(SearchData,1,1) = 'N')
then IndexPeople[SearchIndex] := SearchFormat;
if (Copy(SearchData,1,1) = 'X') or (Copy(SearchData,1,1) = 'M')
then IndexMovies[SearchIndex] := SearchFormat;
if (Copy(SearchData,1,1) = 'W') or (Copy(SearchData,1,1) = 'L')
then IndexTVShow[SearchIndex] := SearchFormat;
end;
procedure TMainForm.btAllClick(Sender: TObject);
var
i: integer;
d: TJSONObject;
d1,
d2,
d3: String;
begin
// What are the last 20 progress items recorded?
// if Progress.Count > 0 then
// begin
mmInfo.DisableAlign;
mmInfo.Lines.BeginUpdate;
LogEvent('______________________________________________________');
LogEvent('EXTERNAL PROGRESS INFORMATION');
// What are the last 50 progress items recorded?
for i := Progress.Count -1 downto 0 do
begin
try
d := TJSONObject.ParseJSONValue(Progress[i]) as TJSONObject;
if ((Pos('::1' ,(d.getValue('IP') as TJSONString).Value) = 0) and
(Pos('24.86.168.176' ,(d.getValue('IP') as TJSONString).Value) = 0) and
(Pos('23.111.75.19' ,(d.getValue('IP') as TJSONString).Value) = 0)) then
begin
d1 := (d.getValue('ST') as TJSONString).Value;
d2 := FormatDateTime('HH:nn:ss.zzz',(d.getValue('TP') as TJSONNumber).AsDouble-(d.getValue('TS') as TJSONNumber).AsDouble);
d3 := '';
if (d.getValue('DY') <> nil)
then d3 := D3 +'DY:'+(d.getValue('DY') as TJSONString).Value.PadLeft(4)+' ';
if (d.getValue('DT') <> nil)
then d3 := D3 +'DT:'+(d.getValue('DT') as TJSONString).Value.PadLeft(6)+' ';
LogEvent(
d1+' '+
d2+' '+
(d.getValue('IP') as TJSONString).Value.PadRight(17)+
(d.getValue('RQ') as TJSONString).Value.PadRight(16)+' '+
d3.PadRight(20)+
(d.getValue('PR') as TJSONString).Value
);
end;
d.Free;
except on E: Exception do
begin
LogException('External Progress', E.ClassName, E.Message, Progress[i]);
end;
end;
end;
LogEvent('');
mmInfo.Lines.EndUpdate;
mmInfo.EnableAlign;
// end;
// mmInfo.scrolltoBottom;
// mmInfo.Repaint;
end;
procedure TMainForm.btRecentProgressClick(Sender: TObject);
var
i: Integer;
ProgressSize: Integer;
st: TMemoryManagerState;
sb: TSmallBlockTypeState;
mem1: UInt64;
mem2: UInt64;
begin
mmStats.Lines.BeginUpdate;
mmStats.Clear;
mmStats.Lines.Add('');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add(' SERVER INFORMATION ');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add('');
mmStats.Lines.Add(' '+AppVersionString);
mmStats.Lines.Add(' Running on '+GetEnvironmentVariable('COMPUTERNAME'));
mmStats.Lines.Add('');
mmStats.Lines.Add(' Started: '+FormatDateTime('yyyy-mm-dd HH:nn:ss', AppStartup));
mmStats.Lines.Add(' RunTime: '+IntToStr(DaysBetween(Now, AppStartup))+'d '+FormatDateTime('HH:nn:ss', Now-AppStartup));
mmStats.Lines.Add('');
{$WARN SYMBOL_PLATFORM OFF}
GetMemoryManagerState(st);
mem1 := 0;
for sb in st.SmallBlockTypeStates do
mem1 := mem1 + (sb.UseableBlockSize * sb.AllocatedBlockCount);
mem2 := mem1 + st.TotalAllocatedMediumBlockSize + st.TotalAllocatedLargeBlockSize;
{$WARN SYMBOL_PLATFORM ON}
mmStats.Lines.Add(' Memory (Large ): '+FloatToStrF(st.TotalAllocatedLargeBlockSize/1024/1024,ffNumber,10,3).PadLeft(9)+' MB');
mmStats.Lines.Add(' Memory (Medium ): '+FloatToStrF(st.TotalAllocatedMediumBlockSize/1024/1024,ffNumber,10,3).PadLeft(9)+' MB');
mmStats.Lines.Add(' Memory (Small ): '+FloatToStrF(mem1/1024/1024,ffNumber,10,3).PadLeft(9)+' MB');
mmStats.Lines.Add(' Memory (Total ): '+FloatToStrF(mem2/1024/1024,ffNumber,10,3).PadLeft(9)+' MB');
mmStats.Lines.Add('');
mmStats.Lines.Add(' Cache (Files ): '+CacheFiles.PadLeft(9)+' Files');
mmStats.Lines.Add(' Cache (Size ): '+CacheSize.PadLeft(9)+' GB');
mmStats.Lines.Add('');
mmStats.Lines.Add(' Threads: '+IntToStr(GetProcessThreadCount(GetCurrentProcessId)));
MemoryUsage := FloatToStrF(mem2/1024/1024,ffFixed,10,3);
MemoryUsageNice := FloatToStrF(mem2/1024/1024,ffNumber,10,1);
// Whoa - something has fallen off the rails, so abort and try again: Limit set at 5 GB
if ((mem2/1024/1024) > ( 5 * 1024)) then
begin
SendActivityLog('Resource Limit Restart');
Halt;
end;
// How much memory is our progress data taking up?
ProgressSize := 0;
for i := 0 to Progress.Count-1 do
ProgressSize := ProgressSize + Length(Progress[i]);
mmStats.Lines.Add(' History: '+IntToStr(Progress.Count)+' entries / '+FloatToStrF(ProgressSize/1024,ffNumber,10,1)+' KB');
mmStats.Lines.Add('');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add(' PERSON CACHE INFORMATION');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add('');
mmStats.Lines.Add(' Person Cache Requests:'+FloatToStrF(PersonCacheRequests,ffNumber,9,0).PadLeft(9));
if (PersonCacheRequests > 0) then
begin
mmStats.Lines.Add(' Person Cache Hits: '+FloatToStrF(PersonCacheHit,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*PersonCacheHit/PersonCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' Person Cache Misses: '+FloatToStrF(PersonCacheMiss,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*PersonCacheMiss/PersonCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' Person Cache Aged: '+FloatToStrF(PersonCacheAge,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*PersonCacheAge/PersonCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' Person Cache Force: '+FloatToStrF(PersonCacheForce,ffNumber,9,0).PadLeft(9)+FloatToStrF(100.0*PersonCacheForce/PersonCacheRequests,ffNumber,6,1).Padleft(6)+' %');
end;
mmStats.Lines.Add('');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add(' MOVIE CACHE INFORMATION');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add('');
mmStats.Lines.Add(' Movie Cache Requests: '+FloatToStrF(MovieCacheRequests,ffNumber,9,0).PadLeft(9));
if (MovieCacheRequests > 0) then
begin
mmStats.Lines.Add(' Movie Cache Hits: '+FloatToStrF(MovieCacheHit,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*MovieCacheHit/MovieCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' Movie Cache Misses: '+FloatToStrF(MovieCacheMiss,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*MovieCacheMiss/MovieCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' Movie Cache Aged: '+FloatToStrF(MovieCacheAge,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*MovieCacheAge/MovieCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' Movie Cache Force: '+FloatToStrF(MovieCacheForce,ffNumber,9,0).PadLeft(9)+FloatToStrF(100.0*MovieCacheForce/MovieCacheRequests,ffNumber,6,1).Padleft(6)+' %');
end;
mmStats.Lines.Add('');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add(' TVSHOW CACHE INFORMATION');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add('');
mmStats.Lines.Add(' TVShow Cache Requests:'+FloatToStrF(TVShowCacheRequests,ffNumber,9,0).PadLeft(9));
if (TVShowCacheRequests > 0) then
begin
mmStats.Lines.Add(' TVShow Cache Hits: '+FloatToStrF(TVShowCacheHit,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*TVShowCacheHit/TVShowCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' TVShow Cache Misses: '+FloatToStrF(TVShowCacheMiss,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*TVShowCacheMiss/TVShowCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' TVShow Cache Aged: '+FloatToStrF(TVShowCacheAge,ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*TVShowCacheAge/TVShowCacheRequests,ffNumber,6,1).PadLeft(6) +' %');
mmStats.Lines.Add(' TVShow Cache Force: '+FloatToStrF(TVShowCacheForce,ffNumber,9,0).PadLeft(9)+FloatToStrF(100.0*TVShowCacheForce/TVShowCacheRequests,ffNumber,6,1).Padleft(6)+' %');
end;
mmStats.Lines.Add('');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add(' SEARCH INDEX INFORMATION');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add('');
mmStats.Lines.Add(' Person Index Size: '+FloatToStrF(IndexPeopleSize/1024/1024,ffNumber,9,3).PadLeft(9)+' MB');
mmStats.Lines.Add(' Movies Index Size: '+FloatToStrF(IndexMoviesSize/1024/1024,ffNumber,9,3).PadLeft(9)+' MB');
mmStats.Lines.Add(' TVShow Index Size: '+FloatToStrF(IndexTVShowSize/1024/1024,ffNumber,9,3).PadLeft(9)+' MB');
mmStats.Lines.Add('');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add(' CACHE CLEANING INFORMATION');
mmStats.Lines.Add(' ========================================');
mmStats.Lines.Add('');
mmStats.Lines.Add(' Cache Clean Requests: '+FloatToStrF(CleanRequests,ffNumber,9,0).PadLeft(9));
if (CleanRequests > 0) then
begin
mmStats.Lines.Add(' Cache Clean Days: '+FloatToStrF(CleanDays, ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*CleanDays /CleanRequests,ffNumber,6,1).PadLeft(6)+' %');
mmStats.Lines.Add(' Cache Clean People: '+FloatToStrF(CleanPeople, ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*CleanPeople /CleanRequests,ffNumber,6,1).PadLeft(6)+' %');
mmStats.Lines.Add(' Cache Clean Movies: '+FloatToStrF(CleanMovies, ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*CleanMovies /CleanRequests,ffNumber,6,1).PadLeft(6)+' %');
mmStats.Lines.Add(' Cache Clean TVShows: '+FloatToStrF(CleanTVShows, ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*CleanTVShows /CleanRequests,ffNumber,6,1).PadLeft(6)+' %');
mmStats.Lines.Add(' Cache Clean Files: '+FloatToStrF(CleanFiles, ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*CleanFiles /CleanRequests,ffNumber,6,1).PadLeft(6)+' %');
mmStats.Lines.Add(' Cache Clean Small: '+FloatToStrF(CleanSmall, ffNumber,9,0).PadLeft(9) +FloatToStrF(100.0*CleanSmall /CleanRequests,ffNumber,6,1).PadLeft(6)+' %');
mmStats.Lines.Add(' Cache Clean Size: '+FloatToStrF(CleanSize/(1024*1024),ffNumber,9,0).PadLeft(9) +' MB');
end;
mmStats.Lines.EndUpdate;
end;
procedure TMainForm.btRedocClick(Sender: TObject);
var
URL: String;
begin
URL := AppRedoc;
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
procedure TMainForm.btStartClick(ASender: TObject);
begin
ServerContainer.SparkleHttpSysDispatcher.Start;
UpdateGUI;
end;
procedure TMainForm.btStopClick(ASender: TObject);
begin
ServerContainer.SparkleHttpSysDispatcher.Stop;
UpdateGUI;
end;
procedure TMainForm.btSwaggerClick(Sender: TObject);
var
URL: String;
begin
URL := AppSwagger;
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
procedure TMainForm.GetAppVersionString;
var
verblock:PVSFIXEDFILEINFO;
versionMS,versionLS:cardinal;
verlen:cardinal;
rs:TResourceStream;
m:TMemoryStream;
p:pointer;
s:cardinal;
ReleaseDate: TDateTime;
begin
// Lot of work to just get the Application Version Information
m:=TMemoryStream.Create;
try
rs:=TResourceStream.CreateFromID(HInstance,1,RT_VERSION);
try
m.CopyFrom(rs,rs.Size);
finally
rs.Free;
end;
m.Position:=0;
if VerQueryValue(m.Memory,'\',pointer(verblock),verlen) then
begin
VersionMS:=verblock.dwFileVersionMS;
VersionLS:=verblock.dwFileVersionLS;
// AppVersionString:=Application.Title+' '+
AppVersion :=
IntToStr(versionMS shr 16)+'.'+
IntToStr(versionMS and $FFFF)+'.'+
IntToStr(VersionLS shr 16)+'.'+
IntToStr(VersionLS and $FFFF);
end;
if VerQueryValue(m.Memory,PChar('\\StringFileInfo\\'+
IntToHex(GetThreadLocale,4)+IntToHex(GetACP,4)+'\\FileDescription'),p,s) or
VerQueryValue(m.Memory,'\\StringFileInfo\\040904E4\\FileDescription',p,s) then //en-us
AppVersionString:=PChar(p)+' v'+AppVersion;
finally
m.Free;
end;
Application.Title := AppVersionString;
FileAge(ParamStr(0), ReleaseDate);
AppRelease := FormatDateTime('yyyy-MMM-dd', ReleaseDate);
Caption := 'Actorious XData Server Ver '+AppVersion+' Rel '+AppRelease;
end;
function TMainForm.GetProcessThreadCount(ProcessId: Cardinal): Integer;
var
SnapshotHandle: THandle;
ProcessEntry32: TProcessEntry32;
CanContinue: Boolean;
begin
Result := 0;
SnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SnapshotHandle <> INVALID_HANDLE_VALUE then
try
ProcessEntry32.dwSize := SizeOf(ProcessEntry32);
CanContinue := Process32First(SnapshotHandle, ProcessEntry32);
while CanContinue do
begin
if ProcessEntry32.th32ProcessID = ProcessId then
begin
Result := ProcessEntry32.cntThreads;
Exit;
end;
CanContinue := Process32Next(SnapshotHandle, ProcessEntry32);
end;
finally
CloseHandle(SnapshotHandle);
end;
end;
procedure TMainForm.lblSearchMoviesClick(Sender: TObject);
var
i,j : Integer;
begin
// Shows the most recent additions to the Movies search index
btClearClick(Sender);
i := Length(IndexMovies) - 1;
j := 0;
while i >= 0 do
begin
if Length(IndexMovies[i]) >= 150
then LogEvent(RightStr('00000'+IntToStr(i),5)+' '+LeftStr(IndexMovies[i],100)+' ... '+RightStr(IndexMovies[i],50))
else LogEvent(RightStr('00000'+IntToStr(i),5)+' '+IndexMovies[i]);
i := i - 1;
// Only want the most recent 50
j := j + 1;
if j = 50 then i := -1;
end;
SaveSearchIndexes;
end;
procedure TMainForm.lblSearchPeopleClick(Sender: TObject);
var
i,j : Integer;
begin
// Shows the most recent additions to the People search index
btClearClick(Sender);
i := Length(IndexPeople) - 1;
j := 0;
while i >= 0 do
begin
if Length(IndexPeople[i]) >= 150
then LogEvent(RightStr('00000'+IntToStr(i),5)+' '+LeftStr(IndexPeople[i],100)+' ... '+RightStr(IndexPeople[i],50))
else LogEvent(RightStr('00000'+IntToStr(i),5)+' '+IndexPeople[i]);
i := i - 1;
// Only want the most recent 50
j := j + 1;
if j = 50 then i := -1;
end;
SaveSearchIndexes;
end;
procedure TMainForm.lblSearchTVShowClick(Sender: TObject);
var
i,j : Integer;
begin
// Shows the most recent additions to the TVShow search index
btClearClick(Sender);
i := Length(IndexTVShow) - 1;
j := 0;
while i >= 0 do
begin
if Length(IndexTVShow[i]) >= 150
then LogEvent(RightStr('00000'+IntToStr(i),5)+' '+LeftStr(IndexTVShow[i],100)+' ... '+RightStr(IndexTVShow[i],50))
else LogEvent(RightStr('00000'+IntToStr(i),5)+' '+IndexTVShow[i]);
i := i - 1;
// Only want the most recent 50
j := j + 1;
if j = 50 then i := -1;
end;