-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBroadcastAPI.pas
2435 lines (1940 loc) · 56.4 KB
/
BroadcastAPI.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 BroadcastAPI;
{$SCOPEDENUMS ON}
interface
uses
// Required Units
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Graphics, IOUtils, System.Generics.Collections, IdSSLOpenSSL, IdURI,
IdHTTP, IdGlobal, JSON, Vcl.Clipbrd, DateUtils, Cod.Types, Imaging.jpeg,
Cod.VarHelpers, Cod.Dialogs, Cod.SysUtils, Cod.Files, Cod.ArrayHelpers;
type
// Cardinals
TArtSize = (Small, Medium, Large);
TWorkItem = (DownloadingImage);
TWorkItems = set of TWorkItem;
// Source
TDataSource = (None, Tracks, Albums, Artists, Playlists);
TDataSources = set of TDataSource;
// Loading
TLoad = (Track, Album, Artist, PlayList);
TLoadSet = set of TLoad;
// Procs
TDataTypeUpdate = procedure(AUpdate: TDataSource) of object;
// Records
ResultType = record
Error: boolean;
LoggedIn: boolean;
ServerMessage: string;
function Success: boolean;
procedure TerminateSession;
procedure AnaliseFrom(JSON: TJSONValue);
end;
THistoryItem = record
TrackID: string;
TimeStamp: TDateTime;
end;
TLibraryStatus = record
TotalTracks: integer;
TotalPlays: integer;
TokenExpireDate: TDateTime;
LastLibraryModified: TDateTime;
UpdateTimestamp: TDateTime;
(* Loading *)
procedure LoadFrom(JSON: TJSONValue);
end;
TAccount = record
Username: string;
OneQueue: boolean;
BitRate: string;
UserID: integer;
CreationDate: TDateTime;
Verified: boolean;
BetaTester: boolean;
EmailAdress: string;
Premium: boolean;
VerificationDate: TDateTime;
(* Loading *)
procedure LoadFrom(JSON: TJSONValue);
end;
TTrackItem = record
(* Song properties in their JSON order, "?" is a unknown property *)
ID: string;
TrackNumber: cardinal;
Year: cardinal;
Title: string;
Genre: string;
LengthSeconds: cardinal;
AlbumID: string;
ArtworkID: string;
ArtistID: string;
// ??? Some ID integer
DayUploaded: TDate;
IsInTrash: boolean;
FileSize: integer;
UploadLocation: string;
// ??? empty string
Rating: cardinal;
Plays: cardinal;
StreamLocations: string;
AudioType: string;
ReplayGain: string;
UploadTime: TTime;
// ??? Tag Array
// Extra Data
CachedImage,
CachedImageLarge: TJpegImage;
Status: TWorkItems;
(* Utils *)
function GetStreamingURL: string;
(* Artwork *)
function ArtworkLoaded(Large: boolean = false): boolean;
function GetArtwork(Large: boolean = false): TJPEGImage;
(* Loading *)
procedure LoadFrom(JSONPair: TJSONPair);
end;
TAlbumItem = record
(* Album properties in their JSON order, "?" is a unknown property *)
ID: string;
AlbumName: string;
TracksID: TArray<string>;
ArtistID: string;
IsInTrash: boolean;
Rating: cardinal;
Disk: cardinal;
Year: cardinal;
// ??? - Artist_aditional
// ??? - ICatID
CachedImage: TJpegImage;
Status: TWorkItems;
(* Artwork *)
function ArtworkLoaded: boolean;
function GetArtwork: TJPEGImage;
(* Loading *)
procedure LoadFrom(JSONPair: TJSONPair);
end;
TArtistItem = record
(* Album properties in their JSON order, "?" is a unknown property *)
ID: string;
ArtistName: string;
TracksID: TArray<string>;
IsInTrash: boolean;
Rating: cardinal;
ArtworkID: string;
// ??? - ICatID
// Extra Data
HasArtwork: boolean;
CachedImage: TJpegImage;
Status: TWorkItems;
(* Artwork *)
function ArtworkLoaded: boolean;
function GetArtwork: TJPEGImage;
(* Loading *)
procedure LoadFrom(JSONPair: TJSONPair);
end;
TPlaylistItem = record
(* Album properties in their JSON order, "?" is a unknown property *)
ID: string;
Name: string;
TracksID: TArray<string>;
// ??? UID
// ??? system_created
// ??? public_id
PlaylistType: string;
Description: string;
ArtworkID: string;
// ??? SortType
// Extra Data
HasArtwork: boolean;
CachedImage: TJpegImage;
Status: TWorkItems;
(* Artwork *)
function ArtworkLoaded: boolean;
function GetArtwork: TJPEGImage;
(* Loading *)
procedure LoadFrom(JSONPair: TJSONPair);
end;
TSession = record
DeviceName: string;
Joinable: boolean;
Connected: boolean;
Client: string;
LastLogin: TDateTime;
Location: string;
(* Loading *)
procedure LoadFrom(JSON: TJSONValue);
end;
// Arrays
TArtists = TArray<TArtistItem>;
TAlbums = TArray<TAlbumItem>;
TTracks = TArray<TTrackItem>;
TPlaylists = TArray<TPlaylistItem>;
TSessions = TArray<TSession>;
// Get Data
function GetTrack(ID: string): integer;
function GetAlbum(ID: string): integer;
function GetArtist(ID: string): integer;
function GetPlaylist(ID: string): integer;
function GetPlaylistOfType(AType: string): integer; (* thumbsup, recently-played, recently-uploaded *)
// Utils
function StringToDateTime(const ADateTimeStr: string; CovertUTC: boolean = true): TDateTime;
function DateTimeToString(ADateTime: TDateTime; CovertUTC: boolean = true): string;
function DateToString(ADateTime: TDate; CovertUTC: boolean = true): string;
function Yearify(Year: cardinal): string;
// Main Request
function SendClientRequest(RequestJSON: string; Endpoint: string = ''): TJSONValue;
// User
function LoginUser: boolean;
procedure LogOff;
function IsAuthenthicated: boolean;
procedure ReturnToLogin;
// Memory
procedure APIFreeMemory;
// Artwork Store
procedure AddToArtworkStore(ID: string; Cache: TJpegImage; AType: TDataSource);
function ExistsInStore(ID: string; AType: TDataSource): boolean;
function GetArtStoreCache(ID: string; AType: TDataSource): TJpegImage;
function GetArtworkStore(AType: TDataSource = TDataSource.None): string;
procedure ClearArtworkStore;
procedure InitiateArtworkStore;
// Tracks
function UpdateTrackRating(ID: string; Rating: integer; ReloadLibrary: boolean): boolean;
function GetSongPlaylists(ID: string): TArray<string>;
function TrackRatingToLikedPlaylist(ID: string): boolean;
// Albums
function UpdateAlbumRating(ID: string; Rating: integer; ReloadLibrary: boolean): boolean;
// Artists
function UpdateArtistRating(ID: string; Rating: integer; ReloadLibrary: boolean): boolean;
// Playlist
function CreateNewPlayList(Name, Description: string; MakePublic: boolean; Tracks: TArray<string>): boolean; overload;
function CreateNewPlayList(Name, Description: string; MakePublic: boolean; Mood: string): boolean; overload;
function AppentToPlaylist(ID: string; Tracks: TArray<string>): boolean;
function PreappendToPlaylist(ID: string; Tracks: TArray<string>): boolean;
function ChangePlayList(ID: string; Tracks: TArray<string>): boolean;
function DeleteFromPlaylist(ID: string; Tracks: TArray<string>): boolean;
function TouchupPlaylist(ID: string): boolean;
function UpdatePlayList(ID: string; Name, Description: string; ReloadLibrary: boolean): boolean;
function DeletePlayList(ID: string): boolean;
function DeleteTracks(Tracks: TArray<string>): boolean;
function DeleteTrack(ID: string): boolean;
function DeleteAlbum(ID: string): boolean;
function DeleteArtist(ID: string): boolean;
function RestoreTracks(Tracks: TArray<string>): boolean;
function RestoreTrack(ID: string): boolean;
function RestoreAlbum(ID: string): boolean;
function RestoreArtist(ID: string): boolean;
function EmptyTrash(Tracks: TArray<string>): boolean;
function CompleteEmptyTrash: boolean;
// History
function PushHistory(Items: TArray<THistoryItem>): boolean;
// Library
procedure LoadStatus;
procedure LoadLibrary;
procedure LoadLibraryAdvanced(LoadSet: TLoadSet);
// Additional Data
function GetSongArtwork(ID: string; Size: TArtSize = TArtSize.Small): TJpegImage;
function SongArtCollage(ID1, ID2, ID3, ID4: string): TJpegImage;
// Status
procedure SetWorkStatus(Status: string);
procedure SetDataWorkStatus(Status: string);
procedure ResetWork;
var
// Formattable Strings
WELCOME_STRING: string = 'Welcome, %S';
const
// Formattable Strings
DEVICE_NAME_CONST = '%S' + ' iBroadcast for Windows';
WELCOME_STRING_SPECIAL = 'Happy holidays, %S';
// Login Constants
CLIENT_NAME = 'Cods iBroadcast';
API_ENDPOINT = 'https://api.ibroadcast.com/';
LIBRARY_ENDPOINT = 'https://library.ibroadcast.com/';
ARTWORK_ENDPOINT = 'https://artwork.ibroadcast.com/artwork/%S-%U';
STREAMING_ENDPOINT = 'https://streaming.ibroadcast.com';
API_VERSION = '1.0.0';
// Artwork Store
ART_EXT = '.jpeg';
// Templates
REQUEST_HEADER = '{'
+ '"user_id": %U,'
+ '"token": "%S",'
+ '"version": "' + API_VERSION + '"';
// Request Formats
REQUEST_LOGIN = '{'
+ '"login_token": "%S",'
+ '"device_name": "%S",'
+ '"client": "%S",'
+ '"version": "' + API_VERSION + '",'
+ '"app_id": "%S",'
+ '"type": "account",'
+ '"mode": "login_token"'
+ '}';
REQUEST_LOGOFF = REQUEST_HEADER + ','
+ '"mode": "logout"'
+ '}';
// Data
REQUEST_EMPTY = REQUEST_HEADER + ','
+ '}';
REQUEST_DATA = REQUEST_HEADER + ','
+ '"mode": "%S"'
+ '}';
// Playlist
REQUEST_LIST_TEMPLATE = REQUEST_HEADER + ','
+ '"mode": "createplaylist",'
+ '"name": "%S",'
+ '"description": "%S",'
+ '"make_public": %S';
REQUEST_LIST_CREATETRACKS = REQUEST_LIST_TEMPLATE + ','
+ '"tracks": [%S]'
+ '}';
REQUEST_LIST_CREATEMOOD = REQUEST_LIST_TEMPLATE + ','
+ '"mood": "%S"'
+ '}';
REQUEST_LIST_DELETE = REQUEST_HEADER + ','
+ '"mode": "deleteplaylist",'
+ '"playlist": %S'
+ '}';
REQUEST_LIST_ADD = REQUEST_HEADER + ','
+ '"mode": "appendplaylist",'
+ '"playlist": %S,'
+ '"tracks": [%S]'
+ '}';
REQUEST_LIST_SET = REQUEST_HEADER + ','
+ '"mode": "updateplaylist",'
+ '"playlist": %S,'
+ '"tracks": [%S]'
+ '}';
REQUEST_LIST_UPDATE = REQUEST_HEADER + ','
+ '"mode": "updateplaylist",'
+ '"playlist": %S,'
+ '"name": "%S",'
+ 'supported_types: false,'
+ '"description": "%S"'
+ '}';
// Track
REQUEST_TRACK_DELETE = REQUEST_HEADER + ','
+ '"mode": "trash",'
+ '"tracks": [%S]'
+ '}';
REQUEST_TRACK_RESTORE = REQUEST_HEADER + ','
+ '"mode": "restore",'
+ '"tracks": [%S]'
+ '}';
REQUEST_TRACK_EMPTYTRASH = REQUEST_HEADER + ','
+ '"mode": "empty_trash",'
+ '"tracks": [%S]'
+ '}';
// Rating
REQUEST_RATE_TRACK = REQUEST_HEADER + ','
+ '"mode": "ratetrack",'
+ '"track_id": %S,'
+ '"rating": %D'
+ '}';
REQUEST_RATE_ALBUM = REQUEST_HEADER + ','
+ '"mode": "ratealbum",'
+ '"album_id": %S,'
+ '"rating": %D'
+ '}';
REQUEST_RATE_ARTIST = REQUEST_HEADER + ','
+ '"mode": "rateartist",'
+ '"artist_id": %S,'
+ '"rating": %D'
+ '}';
// History
(* Will be build on runtime *)
// Library
REQUEST_LIBRARY = REQUEST_HEADER
+ '}';
var
// App Device token
LOGIN_TOKEN: string;
// Notify
OnWorkStatusChange: procedure(Status: string);
OnDataWorkStatusChange: procedure(Status: string);
// Post export
ExportPost: boolean = false;
// Cover Settings
DefaultArtSize: TArtSize = TArtSize.Medium;
// Login Information
DEVICE_NAME: string;
// Verbose Loggins
WORK_STATUS: string;
DATA_WORK_STATUS: string;
// Work
WorkCount: int64;
TotalWorkCount: int64;
// Setings
ValueRatingMode: boolean = false; // use rating stars
// Notify Events
OnUpdateType: TDataTypeUpdate;
// Artwork Store
ArtworkStore: boolean = true;
MediaStoreLocation: string;
// Server Login Output
TOKEN: string;
USER_ID: integer;
APPLICATION_ID: string = '1078';
// Library
LibraryStatus: TLibraryStatus;
Account: TAccount;
Sessions: TSessions;
Tracks: TTracks;
Albums: TAlbums;
Artists: TArtists;
Playlists: TPlaylists;
DefaultPicture: TJPEGImage;
implementation
uses
MainUI;
function SendClientRequest(RequestJSON, Endpoint: string): TJSONValue;
var
AFolder, APath: string;
HTTP: TIdHTTP;
SSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
ResponseStream, RequestStream: TStringStream;
I: integer;
begin
// Endpoint
if Endpoint = '' then
Endpoint := API_ENDPOINT;
// Create HTTP and SSLIOHandler components
HTTP := TIdHTTP.Create(nil);
SSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
RequestStream := TStringStream.Create(RequestJSON, TEncoding.UTF8);
ResponseStream := TStringStream.Create(RequestJSON, TEncoding.UTF8);
try
// Set SSL/TLS options
SSLIOHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
HTTP.IOHandler := SSLIOHandler;
// Set headers
HTTP.Request.ContentType := 'application/json; charset=utf-8';
// Send request and receive response
HTTP.Post(Endpoint, RequestStream, ResponseStream);
// POST Exporter
if ExportPost then
begin
AFolder := ReplaceWinPath('shell:desktop\POST Export\');
if not TDirectory.Exists(AFolder) then
TDirectory.CreateDirectory(AFolder);
I := 0;
repeat
APath := AFolder + 'apirequest' + i.ToString + '.json';
Inc(I);
until not TFile.Exists(APath);
TFile.WriteAllText(APath, ResponseStream.DataString);
end;
// Parse response and extract numbers
Result := TJSONObject.ParseJSONValue(ResponseStream.DataString);
finally
// Free
HTTP.Free;
RequestStream.Free;
ResponseStream.Free;
end;
end;
function LoginUser: boolean;
var
Request: string;
SResult: ResultType;
JSONValue: TJSONValue;
JSONUser: TJSONObject;
begin
// Prepare request string
Request := Format(REQUEST_LOGIN, [LOGIN_TOKEN, DEVICE_NAME, CLIENT_NAME, APPLICATION_ID]);
// Parse response and extract numbers
JSONValue := SendClientRequest(Request);
try
SResult.AnaliseFrom(JSONValue);
Result := SResult.Success;
// Success
if SResult.Success then
begin
// Get "user" category
JSONUser := JSONValue.GetValue<TJSONObject>('user');
// Get User ID
USER_ID := JSONUser.GetValue<TJSONString>('id').Value.ToInteger;
TOKEN := JSONUser.GetValue<TJSONString>('token').Value;;
end
else
begin
//raise Exception.Create(SResult.ServerMessage);
end;
finally
JSONValue.Free;
end;
end;
procedure LogOff;
var
Request: string;
SResult: ResultType;
JSONValue: TJSONValue;
begin
// Prepare request string
Request := Format(REQUEST_LOGOFF, [USER_ID, TOKEN]);
// Parse response and extract numbers
JSONValue := SendClientRequest(Request);
try
SResult.AnaliseFrom(JSONValue);
ReturnToLogin;
finally
JSONValue.Free;
end;
end;
function IsAuthenthicated: boolean;
var
Request: string;
SResult: ResultType;
JSONValue: TJSONValue;
begin
if (USER_ID = 0) or (TOKEN = '') then
Exit(false);
// Prepare request string
Request := Format(REQUEST_EMPTY, [USER_ID, TOKEN]);
// Parse response and extract numbers
JSONValue := SendClientRequest(Request);
try
SResult.AnaliseFrom(JSONValue);
Result := SResult.LoggedIn;
finally
JSONValue.Free;
end;
end;
procedure ReturnToLogin;
begin
UIForm.PrepareForLogin;
end;
procedure APIFreeMemory;
var
I: Integer;
begin
for I := 0 to High(Tracks) do
begin
if Tracks[I].CachedImage <> nil then
Tracks[I].CachedImage.Free;
if Tracks[I].CachedImageLarge <> nil then
Tracks[I].CachedImageLarge.Free;
end;
end;
procedure AddToArtworkStore(ID: string; Cache: TJpegImage; AType: TDataSource);
var
Path: string;
begin
Path := GetArtworkStore(AType) + ID + ART_EXT;
Cache.SaveToFile(Path);
end;
function ExistsInStore(ID: string; AType: TDataSource): boolean;
var
Path: string;
begin
if not ArtworkStore then
Exit(false);
Path := GetArtworkStore(AType) + ID + ART_EXT;
Result := TFile.Exists( Path );
end;
function GetArtStoreCache(ID: string; AType: TDataSource): TJpegImage;
var
Path: string;
begin
Path := GetArtworkStore(AType) + ID + ART_EXT;
Result := TJpegImage.Create;
Result.LoadFromFile(Path);
end;
function GetArtworkStore(AType: TDataSource): string;
begin
Result := IncludeTrailingPathDelimiter(MediaStoreLocation);
case AType of
TDataSource.Tracks: Result := Result + 'Tracks';
TDataSource.Albums: Result := Result + 'Albums';
TDataSource.Artists: Result := Result + 'Artists';
TDataSource.Playlists: Result := Result + 'Playlists';
end;
Result := IncludeTrailingPathDelimiter(Result);
end;
procedure ClearArtworkStore;
var
Path: string;
begin
Path := GetArtworkStore;
if TDirectory.Exists(Path) then
TDirectory.Delete(Path, true);
end;
procedure InitiateArtworkStore;
var
ArtRoot: string;
begin
if not ArtworkStore then
Exit;
ArtRoot := GetArtworkStore;
if not TDirectory.Exists(ArtRoot) then
TDirectory.CreateDirectory(ArtRoot);
TDirectory.CreateDirectory(GetArtworkStore(TDataSource.Tracks));
TDirectory.CreateDirectory(GetArtworkStore(TDataSource.Albums));
TDirectory.CreateDirectory(GetArtworkStore(TDataSource.Artists));
TDirectory.CreateDirectory(GetArtworkStore(TDataSource.Playlists));
end;
function UpdateTrackRating(ID: string; Rating: integer; ReloadLibrary: boolean): boolean;
var
Request: string;
JResult: ResultType;
JSONValue: TJSONValue;
begin
// Prepare request string
Request := Format(REQUEST_RATE_TRACK, [USER_ID, TOKEN, ID, Rating]);
// Parse response and extract numbers
SetWorkStatus('Updating track rating');
JSONValue := SendClientRequest(Request);
try
// Error
JResult.AnaliseFrom(JSONVALUE);
Result := JResult.Success;
finally
JSONValue.Free;
end;
// Re-load playlists
if ReloadLibrary then
LoadLibraryAdvanced([TLoad.Track]);
end;
function GetSongPlaylists(ID: string): TArray<string>;
var
I: Integer;
begin
// Search
Result := [];
for I := 0 to High(Playlists) do
if Playlists[I].TracksID.Find(ID) <> -1 then
Result.AddValue(Playlists[I].ID);
end;
function TrackRatingToLikedPlaylist(ID: string): boolean;
var
Index, SongIndex: integer;
begin
Result := false;
SongIndex := GetTrack(ID);
Index := GetPlaylistOfType('thumbsup');
if (Index <> -1) and (SongIndex <> -1) then
begin
const Fav = Playlists[Index].TracksID.Find(ID) <> -1;
var IsFav: boolean;
if ValueRatingMode then
IsFav := Tracks[SongIndex].Rating = 10
else
IsFav := Tracks[SongIndex].Rating in [10, 5];
if IsFav <> Fav then
begin
if IsFav then
Result := PreappendToPlaylist(Playlists[Index].ID, [ID])
else
Result := DeleteFromPlaylist(Playlists[Index].ID, [ID]);
end;
end;
end;
function UpdateAlbumRating(ID: string; Rating: integer; ReloadLibrary: boolean): boolean;
var
Request: string;
JResult: ResultType;
JSONValue: TJSONValue;
begin
// Prepare request string
Request := Format(REQUEST_RATE_ALBUM, [USER_ID, TOKEN, ID, Rating]);
// Parse response and extract numbers
SetWorkStatus('Updating album rating');
JSONValue := SendClientRequest(Request);
try
// Error
JResult.AnaliseFrom(JSONVALUE);
Result := JResult.Success;
finally
JSONValue.Free;
end;
// Re-load playlists
if ReloadLibrary then
LoadLibraryAdvanced([TLoad.Album]);
end;
function UpdateArtistRating(ID: string; Rating: integer; ReloadLibrary: boolean): boolean;
var
Request: string;
JResult: ResultType;
JSONValue: TJSONValue;
begin
// Prepare request string
Request := Format(REQUEST_RATE_ARTIST, [USER_ID, TOKEN, ID, Rating]);
// Parse response and extract numbers
SetWorkStatus('Updating artist rating');
JSONValue := SendClientRequest(Request);
try
// Error
JResult.AnaliseFrom(JSONVALUE);
Result := JResult.Success;
finally
JSONValue.Free;
end;
// Re-load playlists
if ReloadLibrary then
LoadLibraryAdvanced([TLoad.Artist]);
end;
function CreateNewPlayList(Name, Description: string; MakePublic: boolean; Tracks: TArray<string>): boolean;
var
Request: string;
JResult: ResultType;
ATracks: string;
ATotal: integer;
JSONValue: TJSONValue;
I: Integer;
begin
// Get Tracks
ATracks := '';
ATotal := High(Tracks);
for I := 0 to ATotal do
begin
ATracks := ATracks + Tracks[I];
if I < ATotal then
ATracks := Concat(ATracks, ',');
end;
// Prepare request string
Request := Format(REQUEST_LIST_CREATETRACKS, [USER_ID, TOKEN,
Name, Description, booleantostring(MakePublic), ATracks]);
// Parse response and extract numbers
SetWorkStatus('Creating Playlist by Songs');
JSONValue := SendClientRequest(Request);
try
// Error
JResult.AnaliseFrom(JSONVALUE);
Result := JResult.Success;
finally
JSONValue.Free;
end;
// Re-load playlists
LoadLibraryAdvanced([TLoad.PlayList]);
end;
function CreateNewPlayList(Name, Description: string; MakePublic: boolean; Mood: string): boolean;
var
Request: string;
JResult: ResultType;
JSONValue: TJSONValue;
begin
// Prepare request string
Request := Format(REQUEST_LIST_CREATEMOOD, [USER_ID, TOKEN,
Name, Description, booleantostring(MakePublic), Mood]);
// Parse response and extract numbers
SetWorkStatus('Creating Playlist by Mood');
JSONValue := SendClientRequest(Request);
try
// Error
JResult.AnaliseFrom(JSONVALUE);
Result := JResult.Success;
finally
JSONValue.Free;
end;
// Re-load playlists
LoadLibraryAdvanced([TLoad.PlayList]);
end;
function AppentToPlaylist(ID: string; Tracks: TArray<string>): boolean;
var
Request: string;
JResult: ResultType;
ATracks: string;
ATotal: integer;
JSONValue: TJSONValue;
I: Integer;
begin
// Get Tracks
ATracks := '';
ATotal := High(Tracks);
for I := 0 to ATotal do
begin
ATracks := ATracks + Tracks[I];
if I < ATotal then
ATracks := Concat(ATracks, ',');
end;
// Prepare request string
Request := Format(REQUEST_LIST_ADD, [USER_ID, TOKEN,
ID, ATracks]);
// Parse response and extract numbers
SetWorkStatus('Adding songs to playlist');
JSONValue := SendClientRequest(Request);
try
// Error
JResult.AnaliseFrom(JSONVALUE);
Result := JResult.Success;
finally
JSONValue.Free;
end;
// Re-load playlists
LoadLibraryAdvanced([TLoad.PlayList]);
end;
function PreappendToPlaylist(ID: string; Tracks: TArray<string>): boolean;
var
AllTracks: TArray<string>;
I: Integer;
begin
// Get Tracks
AllTracks := Playlists[GetPlaylist(ID)].TracksID;
// Insert
for I := 0 to High(Tracks) do
AllTracks.Insert(0, Tracks[I]);
// Change ex
Result := ChangePlayList(ID, AllTracks);
end;
function ChangePlayList(ID: string; Tracks: TArray<string>): boolean;
var
Request: string;
JResult: ResultType;
AllTracks: TArray<string>;
ATracks: string;
ATotal: integer;
JSONValue: TJSONValue;
I: Integer;
begin
// Delete Tracks