Repository navigation
Expand file tree
/
Copy pathMixcloudScript.js
More file actions
4203 lines (3695 loc) · 94.9 KB
/
Copy pathMixcloudScript.js
File metadata and controls
4203 lines (3695 loc) · 94.9 KB
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
/**
* Mixcloud Plugin for Grayjay
*
* This plugin enables browsing, searching, and streaming Mixcloud content.
*/
// ====================== CONSTANTS ======================
const PLATFORM = {
name: "Mixcloud"
};
const URLS = {
GRAPHQL_URL: "https://app.mixcloud.com/graphql",
BASE_URL: "https://www.mixcloud.com",
PLUGIN_ICON_URL: "https://plugins.grayjay.app/Mixcloud/MixcloudIcon.png"
};
// Default request headers for API requests
const DEFAULT_REST_HEADERS = {
"Origin": URLS.BASE_URL,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0",
"Content-Type": "application/json"
};
// Regular expressions for URL matching
const REGEX = {
// Content URLs
LIVE_STREAM_URL: /^https:\/\/www\.mixcloud\.com\/live\/([a-zA-Z0-9_-]+)\/?$/,
CONTENT_DETAILS_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/([^\/\?]+)\/?(\?.*)?$/, // Permissive pattern to handle multiple scripts such as Mandarin, updated to allow query params
CHANNEL_URL: /^https:\/\/www\.mixcloud\.com\/([a-zA-Z0-9_-]+)\/?$/,
// Playlist URLs
PLAYLIST_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/playlists\/([^\/]+)\/?$/,
PLAYLISTS_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/playlists\/?$/,
FAVORITES_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/favorites\/?$/,
HISTORY_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/listens\/?$/,
LISTEN_LATER_URL: /^https:\/\/www\.mixcloud\.com\/listen-later\/?$/,
REPOSTS_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/reposts\/?$/,
TRACKS_URL: /^https:\/\/www\.mixcloud\.com\/([^\/]+)\/tracks\/?$/,
MIXCLOUD_INTERNAL_CREATOR_OWN_CONTENT: /[?&]isMixCloudOwnChannelContent=true(&|$)/
};
Type.Feed.Playlists = "PLAYLISTS";
// List of URLs that should not be matched on source.isChannelUrl, source.isContentDetailsUrl, source.isPlaylistUrl
// since mixcloud does not have a dedicated perfix url for channels and published content
// this way those URLs are not matched as channel or content details URL and fail gracefully
const RESERVED_URL_LIST = [
"https://www.mixcloud.com/genres/",
"https://www.mixcloud.com/pro/monetization/",
"https://www.mixcloud.com/pro/host-tagging/",
"https://www.mixcloud.com/pro/live/",
"https://www.mixcloud.com/pro/unlimited-uploads/",
"https://www.mixcloud.com/pro/posts/",
"https://www.mixcloud.com/pro/customization/",
"https://www.mixcloud.com/pro/stats/",
"https://www.mixcloud.com/pro/hq-audio/",
"https://www.mixcloud.com/premium/",
"https://www.mixcloud.com/login/",
"https://www.mixcloud.com/brand/",
"https://www.mixcloud.com/privacy/",
"https://www.mixcloud.com/terms/",
"https://www.mixcloud.com/dmca/",
"https://www.mixcloud.com/developers/",
"https://www.mixcloud.com/subscription-terms/",
"https://www.mixcloud.com/select-terms/",
"https://www.mixcloud.com/about/",
"https://www.mixcloud.com/settings/",
"https://www.mixcloud.com/settings/applications/",
"https://www.mixcloud.com/settings/notifications/",
"https://www.mixcloud.com/settings/profile/",
"https://www.mixcloud.com/settings/account/",
"https://www.mixcloud.com/pro/tips/",
"https://www.mixcloud.com/home/new-uploads/",
"https://www.mixcloud.com/settings/monetization/tips/",
"https://www.mixcloud.com/settings/payments/",
"https://www.mixcloud.com/settings/monetization/subscriptions/",
"https://www.mixcloud.com/notifications/",
"https://www.mixcloud.com/upload/",
"https://www.mixcloud.com/pro/",
"https://www.mixcloud.com/plans/",
"https://www.mixcloud.com/dashboard/posts/",
"https://www.mixcloud.com/dashboard/earnings/",
"https://www.mixcloud.com/dashboard/my-shows/",
"https://www.mixcloud.com/dashboard/my-tracks/",
"https://www.mixcloud.com/dashboard/stats/",
"https://www.mixcloud.com/home/for-you/",
"https://www.mixcloud.com/home/feed/",
"https://www.mixcloud.com/live/",
"https://www.mixcloud.com/discover/",
"https://www.mixcloud.com/playlists/",
"https://www.mixcloud.com/trending/",
"https://www.mixcloud.com/favorites/",
"https://www.mixcloud.com/history/",
"https://www.mixcloud.com/dashboard/playlists/",
"https://www.mixcloud.com/jobs/",
"https://www.mixcloud.com/press/",
"https://www.mixcloud.com/advertise/",
"https://www.mixcloud.com/faq/",
"https://www.mixcloud.com/categories/",
"https://www.mixcloud.com/signup/",
"https://www.mixcloud.com/contact/",
"https://www.mixcloud.com/apps/",
"https://www.mixcloud.com/featured/",
"https://www.mixcloud.com/popular/",
"https://www.mixcloud.com/new/",
"https://www.mixcloud.com/search/"
];
// Plugin configuration storage
let config = {};
let state = {
hasPremiumFeatures: false,
hasProFeatures: false
};
// ====================== PLUGIN INITIALIZATION ======================
source.enable = function (conf, settings, saveStateStr) {
config = conf ?? {};
if (saveStateStr) {
state = JSON.parse(saveStateStr);
}
else if (bridge.isLoggedIn()) {
const useAuth = true;
const cloudcastInfo = makeGraphQLRequest("getViewerDataQuery", {}, null, useAuth);
if (cloudcastInfo?.data?.viewer?.me) {
state.hasPremiumFeatures = cloudcastInfo?.data?.viewer?.me?.hasPremiumFeatures ?? false;
state.hasProFeatures = cloudcastInfo?.data?.viewer?.me?.hasProFeatures ?? false;
}
}
};
source.saveState = () => {
return JSON.stringify(state);
};
// ====================== PLUGIN ENTRY POINTS ======================
/**
* Gets the home feed (currently live streams)
* @returns {ContentPager} A pager for live streams
*/
source.getHome = function () {
return new LiveStreamPager();
};
/**
* Checks if a URL is a content details URL
* @param {string} url URL to check
* @returns {boolean} True if URL is a content details URL
*/
source.isContentDetailsUrl = function (url) {
// Check if query param isMixCloudOwnChannelContent=true
if (REGEX.MIXCLOUD_INTERNAL_CREATOR_OWN_CONTENT.test(url)) {
return true;
}
// Check if URL is in the reserved list
if (isPlatformReservedUrl(url)) {
return false;
}
// Check if it's a playlist-type URL (favorites, reposts, playlists, etc.)
if (source.isPlaylistUrl(url)) {
return false;
}
// Match live streams
if (urlMatches(url, REGEX.LIVE_STREAM_URL)) {
return true;
}
// Match regular mixes (format: https://www.mixcloud.com/username/mix-name/)
if (urlMatches(url, REGEX.CONTENT_DETAILS_URL)) {
return true;
}
return false;
};
/**
* Gets details for a content URL
* @param {string} url Content URL
* @returns {PlatformVideoDetails} Details about the content
*/
source.getContentDetails = function (encodedUrl) {
const url = decodeURIComponent(encodedUrl);
const isLive = urlMatches(url, REGEX.LIVE_STREAM_URL);
if (isLive) {
return getContentDetailsLiveStream(url);
} else {
return getContentDetailsCloudcast(url);
}
};
/**
* Returns chat window information for live Mixcloud streams with chat
* @param {string} url - The video URL
* @returns {Object|null} Chat window configuration or null if chat not available
*/
source.getLiveChatWindow = function (url) {
const usernameMatch = url.match(REGEX.LIVE_STREAM_URL);
if (usernameMatch && usernameMatch[1]) {
// Extract username from live stream URL using regex capture group
const username = usernameMatch[1];
return {
url: `${createMixcloudUrl(username, null, true)}/chat/`,
removeElements: [],
removeElementsInterval: []
};
}
};
/**
* Checks if a URL is a channel URL
* @param {string} url URL to check
* @returns {boolean} True if URL is a channel URL
*/
source.isChannelUrl = function (url) {
// Check if URL is in the reserved list
if (isPlatformReservedUrl(url)) {
return false;
}
// Check if it's a playlist-type URL (favorites, reposts, playlists, etc.)
if (source.isPlaylistUrl(url)) {
return false;
}
return urlMatches(url, REGEX.CHANNEL_URL);
};
/**
* Gets channel information
* @param {string} url Channel URL
* @returns {PlatformChannel} Channel information
*/
source.getChannel = function (url) {
if(url.endsWith('/')) {
url = url.slice(0, -1);
}
const usernameMatch = url.match(REGEX.CHANNEL_URL);
if (!usernameMatch || !usernameMatch[1]) {
throw new UnavailableException("Not able to access channel information");
}
// Extract username from channel URL using regex capture group
const username = usernameMatch[1];
const channelInfo = makeGraphQLRequest("ChannelInfoByUsername", {
"lookup": {
"username": username
}
});
if (!channelInfo?.data?.user) {
throw new UnavailableException("Channel not found");
}
const links = {
Mixcloud: url
};
if (Array.isArray(channelInfo?.data?.user?.socialMediaLinks)) {
channelInfo.data.user.socialMediaLinks.forEach(link => {
links[capitalizeFirstLetter(link.platform) || link.deeplink] = link.deeplink;
});
}
else {
bridge.log("socialMediaLinks is not an array");
}
if (Array.isArray(channelInfo?.data?.user?.customLinks)) {
channelInfo.data.user.customLinks.forEach(link => {
links[capitalizeFirstLetter(link.title) || link.url] = link.url;
});
} else {
bridge.log("customLinks is not an array");
}
let description = channelInfo?.data?.user?.biog ?? '';
const location = [channelInfo?.data?.user?.country, channelInfo?.data?.user?.city].filter(Boolean).join(', ');
if (location) {
description += `\n\n${location}`;
}
return new PlatformChannel({
id: new PlatformID(PLATFORM.name, channelInfo.data.user.id, config.id),
name: channelInfo.data.user.displayName,
description: description,
url: url,
thumbnail: channelInfo.data.user.picture?.urlRoot ? createThumbnailUrl(channelInfo.data.user.picture.urlRoot, 272, 272) : null,
banner: channelInfo.data.user.coverPicture?.urlRoot ? createThumbnailUrl(channelInfo.data.user.coverPicture.urlRoot, 1460, 370) : null,
subscribers: channelInfo.data.user.followers?.totalCount || 0,
links: links,
urlAlternatives: [
`https://www.mixcloud.com/${username}/`
]
});
};
/**
* Gets channel capabilities
* @returns {object} Object with content types and sorts
*/
source.getChannelCapabilities = () => {
return {
types: [Type.Feed.Mixed, Type.Feed.Playlists],
sorts: [Type.Order.Chronological]
};
};
/**
* Gets channel content
* @param {string} url Channel URL
* @param {string} type Content type
* @param {string} order Order
* @param {object} filters Filters
* @returns {ContentPager} A pager for channel content
*/
source.getChannelContents = function (url, type, order, filters) {
const usernameMatch = url.match(REGEX.CHANNEL_URL);
if (!usernameMatch || !usernameMatch[1]) {
throw new UnavailableException("Not able to access channel information");
}
// Extract username from channel URL using regex capture group
const username = usernameMatch[1];
// Handle different content type requests
if (type === Type.Feed.Playlists) {
// For playlists from a channel
return source.getChannelPlaylists(url);
} else {
// For video/mixed content
// First get the channel ID which is needed for pagination queries
const channelInfo = makeGraphQLRequest("ChannelInfoByUsername", {
"lookup": {
"username": username
}
});
if (!channelInfo?.data?.user?.id) {
throw new UnavailableException("Channel not found");
}
const initialData = [];
if (channelInfo?.data?.user?.liveStream?.streamStatus === 'LIVE') {
initialData.push(source.getContentDetails(createMixcloudUrl(username, null, true)));
}
// Return a pager that will load channel uploads with pagination
return new ChannelContentPager(channelInfo, username, initialData);
}
};
/**
* Gets channel playlists
* @param {string} url Channel URL
* @returns {PlaylistPager} A pager for channel playlists
*/
source.getChannelPlaylists = function (url) {
const usernameMatch = url.match(REGEX.CHANNEL_URL);
if (!usernameMatch || !usernameMatch[1]) {
throw new UnavailableException("Not able to access channel information");
}
const username = usernameMatch[1];
return new PlaylistsPager(username);
};
/**
* Checks if a URL is a playlist URL
* @param {string} url URL to check
* @returns {boolean} True if URL is a playlist URL
*/
source.isPlaylistUrl = function (url) {
return urlMatches(url, REGEX.PLAYLIST_URL) ||
urlMatches(url, REGEX.PLAYLISTS_URL) ||
urlMatches(url, REGEX.FAVORITES_URL) ||
urlMatches(url, REGEX.HISTORY_URL) ||
urlMatches(url, REGEX.LISTEN_LATER_URL) ||
urlMatches(url, REGEX.REPOSTS_URL) ||
urlMatches(url, REGEX.TRACKS_URL);
};
/**
* Gets playlist contents
* @param {string} url Playlist URL
* @returns {ContentPager} A pager for playlist content
*/
function getPlaylistContents(url) {
// Check if it's a favorites URL
const favoritesMatch = url.match(REGEX.FAVORITES_URL);
if (favoritesMatch && favoritesMatch[1]) {
const username = favoritesMatch[1];
return new FavoritesPager(username);
}
// Check if it's a history URL
const historyMatch = url.match(REGEX.HISTORY_URL);
if (historyMatch && historyMatch[1]) {
const username = historyMatch[1];
return new HistoryPager(username);
}
// Check if it's a reposts URL
const repostsMatch = url.match(REGEX.REPOSTS_URL);
if (repostsMatch && repostsMatch[1]) {
const username = repostsMatch[1];
return new RepostsPager(username);
}
// Check if it's a channel playlists URL
const playlistsMatch = url.match(REGEX.PLAYLISTS_URL);
if (playlistsMatch && playlistsMatch[1]) {
const username = playlistsMatch[1];
return new PlaylistsPager(username);
}
// Check if it's a tracks URL
const tracksMatch = url.match(REGEX.TRACKS_URL);
if (tracksMatch && tracksMatch[1]) {
const username = tracksMatch[1];
return new TracksPager(username);
}
// Check if it's a listen-later URL (global, no username)
const listenLaterMatch = url.match(REGEX.LISTEN_LATER_URL);
if (listenLaterMatch) {
return new ListenLaterPager();
}
// Otherwise, it's a specific playlist
const match = url.match(REGEX.PLAYLIST_URL);
if (!match || !match[1] || !match[2]) {
throw new UnavailableException("Invalid playlist URL");
}
const username = match[1];
const playlistSlug = match[2];
return new PlaylistContentPager(username, playlistSlug);
}
/**
* Gets playlist details
* @param {string} url Playlist URL
* @returns {PlatformPlaylistDetails} Playlist details
*/
source.getPlaylist = function (url) {
// Handle different playlist types
const favoritesMatch = url.match(REGEX.FAVORITES_URL);
if (favoritesMatch && favoritesMatch[1]) {
return createSpecialPlaylistDetails(favoritesMatch[1], "favorites", "Favorites");
}
const historyMatch = url.match(REGEX.HISTORY_URL);
if (historyMatch && historyMatch[1]) {
return createSpecialPlaylistDetails(historyMatch[1], "history", "Listening History");
}
const repostsMatch = url.match(REGEX.REPOSTS_URL);
if (repostsMatch && repostsMatch[1]) {
return createSpecialPlaylistDetails(repostsMatch[1], "reposts", "Reposts");
}
const tracksMatch = url.match(REGEX.TRACKS_URL);
if (tracksMatch && tracksMatch[1]) {
return createSpecialPlaylistDetails(tracksMatch[1], "tracks", "Tracks");
}
const listenLaterMatch = url.match(REGEX.LISTEN_LATER_URL);
if (listenLaterMatch) {
const userInfo = makeGraphQLRequest("getViewerDataQuery", {}, null, true);
if (!userInfo?.data?.viewer?.me) {
throw new UnavailableException("User not authenticated");
}
const user = userInfo.data.viewer.me;
return new PlatformPlaylistDetails({
id: new PlatformID(PLATFORM.name, "listen_later", config.id),
name: "Listen Later",
url: url,
thumbnail: URLS.PLUGIN_ICON_URL, // Listen Later doesn't have a specific thumbnail
author: new PlatformAuthorLink(
new PlatformID(PLATFORM.name, user.id || "me", config.id),
user.username || "Me",
createMixcloudUrl(user.username || ""),
null
),
videoCount: -1, // Unknown count for listen later
contents: getPlaylistContents(url)
});
}
// Check if it's a channel playlists URL
const playlistsMatch = url.match(REGEX.PLAYLISTS_URL);
if (playlistsMatch && playlistsMatch[1]) {
throw new UnavailableException("Cannot get details for playlists listing page");
}
// Otherwise, it's a specific playlist
const match = url.match(REGEX.PLAYLIST_URL);
if (!match || !match[1] || !match[2]) {
throw new UnavailableException("Invalid playlist URL");
}
const username = match[1];
const playlistSlug = match[2];
// Fetch playlist details
const playlistInfo = makeGraphQLRequest("PlaylistDetailsQuery", {
lookup: {
username: username,
slug: playlistSlug
}
}, "PlaylistDetailsQuery");
if (!playlistInfo?.data?.playlist) {
logError(`PlaylistDetailsQuery failed. Response: ${JSON.stringify(playlistInfo)}`);
throw new UnavailableException("Playlist not found");
}
const playlist = playlistInfo.data.playlist;
const owner = playlist.owner || { username: username, displayName: username };
return new PlatformPlaylistDetails({
id: new PlatformID(PLATFORM.name, playlist.id, config.id),
name: playlist.name,
url: url,
thumbnail: playlist.picture?.urlRoot ?
createThumbnailUrl(playlist.picture.urlRoot, 272, 272) : null,
author: new PlatformAuthorLink(
new PlatformID(PLATFORM.name, owner.id || username, config.id),
owner.displayName || username,
createMixcloudUrl(owner.username || username),
owner.picture?.urlRoot ? createThumbnailUrl(owner.picture.urlRoot, 272, 272) : null
),
videoCount: playlist.items?.totalCount || 0,
contents: getPlaylistContents(url)
});
};
/**
* Searches for content
* @param {string} query Search query
* @returns {ContentPager} A pager for search results
*/
source.search = function (query) {
return new SearchPager(query);
};
/**
* Searches for channels
* @param {string} query Search query
* @returns {ContentPager} A pager for channel search results
*/
source.searchChannels = function (query) {
return new ChannelSearchPager(query);
};
/**
* Gets comments for a content URL
* @param {string} url Content URL
* @returns {CommentPager} A pager for comments
*/
source.getComments = function (url) {
return new MixcloudCommentPager(url);
}
/**
* Get user playlists
* @returns {string[]} Array of playlist URLs
*/
source.getUserPlaylists = function () {
try {
// Get current user info to build system playlist URLs
const userResponse = makeGraphQLRequest("getViewerDataQuery", {}, null, true);
if (!userResponse?.data?.viewer?.me?.username) {
logError("Unable to get current user info for playlists");
return [];
}
const username = userResponse.data.viewer.me.username;
// System playlists for current user
const allPlaylistUrls = [
`${URLS.BASE_URL}/${username}/favorites/`,
`${URLS.BASE_URL}/${username}/listens/`, // History
`${URLS.BASE_URL}/${username}/reposts/`, // User reposts
`${URLS.BASE_URL}/listen-later/` // Global listen-later URL
];
let hasMore = true;
let cursor = null;
// Paginate through all playlists
while (hasMore) {
const variables = {};
if (cursor) {
variables.cursor = cursor;
}
const playlistsResponse = makeGraphQLRequest("PlaylistsQuery", variables, null, true);
if (!playlistsResponse?.data?.viewer?.me?.playlists) {
break;
}
const playlists = playlistsResponse.data.viewer.me.playlists;
const edges = playlists.edges || [];
const pageInfo = playlists.pageInfo || {};
// Extract playlists and map to URLs
const playlistUrls = edges.map(edge => {
const playlist = edge.node;
const owner = playlist.owner;
// Return the full playlist URL
return `${URLS.BASE_URL}/${owner.username}/playlists/${playlist.slug}/`;
});
// Add to all playlists
allPlaylistUrls.push(...playlistUrls);
// Update pagination
hasMore = pageInfo.hasNextPage || false;
cursor = pageInfo.endCursor || null;
}
return allPlaylistUrls;
} catch (error) {
logError(`Exception in getUserPlaylists: ${error.message}`);
return [];
}
}
/**
* Gets the authenticated user's subscriptions (following list)
* @returns {string[]} Array of channel URLs for subscribed channels
*/
source.getUserSubscriptions = function () {
try {
// Get current user info
const userResponse = makeGraphQLRequest("getViewerDataQuery", {}, null, true);
if (!userResponse?.data?.viewer?.me?.username) {
logError("Unable to get current user info for subscriptions");
return [];
}
const username = userResponse.data.viewer.me.username;
const allSubscriptionUrls = [];
let hasMore = true;
let cursor = null;
// Paginate through all followings
while (hasMore) {
const variables = {
lookup: { username: username },
count: 20
};
if (cursor) {
variables.cursor = cursor;
}
const followingsResponse = makeGraphQLRequest("UserFollowingsQuery", variables, null, true);
if (!followingsResponse?.data?.user?.followings) {
break;
}
const followings = followingsResponse.data.user.followings;
const edges = followings.edges || [];
const pageInfo = followings.pageInfo || {};
// Extract followings and map to channel URLs
const subscriptionUrls = edges.map(edge => {
const user = edge.node;
return `${URLS.BASE_URL}/${user.username}`;
});
// Add to all subscriptions
allSubscriptionUrls.push(...subscriptionUrls);
// Update pagination
hasMore = pageInfo.hasNextPage || false;
cursor = pageInfo.endCursor || null;
}
return allSubscriptionUrls;
} catch (error) {
logError(`Exception in getUserSubscriptions: ${error.message}`);
return [];
}
}
// ====================== HELPER FUNCTIONS ======================
/**
* Creates special playlist details for system playlists
* @param {string} username Username
* @param {string} type Playlist type
* @param {string} displayName Display name
* @returns {PlatformPlaylistDetails} Playlist details
*/
function createSpecialPlaylistDetails(username, type, displayName) {
const userInfo = makeGraphQLRequest("ChannelInfoByUsername", {
lookup: { username: username }
});
const user = userInfo?.data?.user;
if (!user) {
throw new UnavailableException("User not found");
}
let playlistThumbnail = null;
if (user.coverPicture?.urlRoot) {
playlistThumbnail = createThumbnailUrl(user.coverPicture.urlRoot, 1460, 370);
} else if (user.picture?.urlRoot) {
playlistThumbnail = createThumbnailUrl(user.picture.urlRoot, 272, 272);
}
const urlMap = {
"favorites": `${username}/favorites`,
"history": `${username}/listens`,
"reposts": `${username}/reposts`,
"tracks": `${username}/tracks`
};
return new PlatformPlaylistDetails({
id: new PlatformID(PLATFORM.name, `${username}_${type}`, config.id),
name: `${user.displayName}'s ${displayName}`,
url: createMixcloudUrl(urlMap[type]),
thumbnail: playlistThumbnail,
author: new PlatformAuthorLink(
new PlatformID(PLATFORM.name, user.id, config.id),
user.displayName,
createMixcloudUrl(username),
user.picture?.urlRoot ? createThumbnailUrl(user.picture.urlRoot, 272, 272) : null
),
videoCount: -1,
contents: getPlaylistContents(createMixcloudUrl(urlMap[type]))
});
}
// ====================== DECRYPTION FUNCTIONS ======================
/**
* Decrypts Mixcloud audio URLs using XOR cipher
* @param {string} encryptedUrl Base64 encoded encrypted URL
* @returns {string} Decrypted URL
*/
function decryptMixcloudUrl(encryptedUrl) {
const key = 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD';
try {
// Decode base64
const ciphertext = atob(encryptedUrl);
// XOR decrypt
let decrypted = '';
for (let i = 0; i < ciphertext.length; i++) {
const ch = ciphertext.charCodeAt(i);
const k = key.charCodeAt(i % key.length);
decrypted += String.fromCharCode(ch ^ k);
}
return decrypted;
} catch (e) {
log(`Failed to decrypt URL: ${e.message}`);
return null;
}
}
// ====================== CONTENT DETAILS FUNCTIONS ======================
/**
* Gets details for a regular Mixcloud mix
* @param {string} url URL of the cloudcast
* @returns {PlatformVideoDetails} Details about the cloudcast
*/
function getContentDetailsCloudcast(url) {
const mixMatch = url.match(REGEX.CONTENT_DETAILS_URL);
const isMixCloudOwnChannelContent = new URL(url)?.searchParams?.get("isMixCloudOwnChannelContent") === "true";
if (!mixMatch || !mixMatch[1] || !mixMatch[2]) {
throw new UnavailableException("Not able to access cloudcast information");
}
// Extract username and slug using regex capture groups
const username = mixMatch[1];
const slug = mixMatch[2];
const cloudcastInfo = makeGraphQLRequest("CloudcastLookup", {
"lookup": {
"username": username,
"slug": slug
}
}, null, true);
if (!cloudcastInfo?.data?.cloudcast) {
throw new UnavailableException("Cloudcast not found");
}
const cloudcast = cloudcastInfo.data.cloudcast;
const sources = [];
const isPreview = isLimitedPreview(cloudcast);
if (cloudcast?.streamInfo?.hlsUrl) {
let hlsUrl;
if (isValidURL(cloudcast?.streamInfo?.hlsUrl)) {
hlsUrl = cloudcast?.streamInfo?.hlsUrl;
} else {
const decryptedUrl = decryptMixcloudUrl(cloudcast?.streamInfo?.hlsUrl);
if (decryptedUrl) {
hlsUrl = decryptedUrl;
}
}
if (hlsUrl) {
sources.push(new HLSSource({
name: "HLS",
url: hlsUrl,
duration: cloudcast.audioLength,
language: "Unknown",
original: true
}));
}
}
else if (cloudcast?.streamInfo?.url) {
log(`Attempting to decrypt URL: ${cloudcast.streamInfo.url}`);
const decryptedUrl = decryptMixcloudUrl(cloudcast.streamInfo.url);
if (decryptedUrl) {
log(`Decrypted URL: ${decryptedUrl}`);
// Determine format from decrypted URL
if (decryptedUrl.includes('.m4a') || decryptedUrl.includes('.mp4')) {
sources.push(new AudioUrlSource({
name: "Audio",
url: decryptedUrl,
bitrate: 128000,
container: "m4a",
duration: cloudcast.audioLength
}));
} else if (decryptedUrl.includes('.mp3')) {
sources.push(new AudioUrlSource({
name: "Audio",
url: decryptedUrl,
bitrate: 128000,
container: "mp3",
duration: cloudcast.audioLength
}));
} else {
// Try as m4a by default if no extension detected
sources.push(new AudioUrlSource({
name: "Audio",
url: decryptedUrl,
bitrate: 128000,
container: "m4a",
duration: cloudcast.audioLength
}));
}
}
}
// Fallback to HLS DRM URL
else if (cloudcast?.streamInfo?.hlsDrmUrl) {
const playlistUrl = cloudcast.streamInfo.hlsDrmUrl.replace("hls_drm/", "hls/");
sources.push(new HLSSource({
name: "HLS",
url: playlistUrl,
duration: cloudcast.audioLength,
language: "Unknown",
original: true
}));
}
else if (isPreview) {
sources.push(new AudioUrlSource({
name: "Audio",
url: cloudcast.previewUrl,
container: "mp3",
duration: -1
}));
}
if (!sources.length) {
throw new UnavailableException("No sources available for cloudcast");
}
let description = cloudcast.description || "";
if (!isMixCloudOwnChannelContent) {
description += `\n\n<a href="${cloudcast.owner.url}">${cloudcast.owner.displayName}</a>`;
} else {
const creator = cloudcast?.creatorAttributions?.edges?.[0]?.node;
if (creator) {
description += `\n\n<a href="${creator.url}">${creator.displayName}</a>`;
}
}
const platformVideo = cloudcastToPlatformVideo(cloudcast);
// Extract tags from the cloudcast
// const tags = extractTags(cloudcastInfo?.data?.cloudcast?.tagList);
const platformVideoDetails = new PlatformVideoDetails({
id: platformVideo.id,
name: platformVideo.name,
description: description,
author: platformVideo.author,
url: platformVideo.url,
duration: platformVideo.duration,
thumbnails: platformVideo.thumbnails,
viewCount: platformVideo.viewCount,
datetime: platformVideo.datetime,
video: new UnMuxVideoSourceDescriptor([], sources),
isLive: false,
});
platformVideoDetails.getContentRecommendations = function () {
const moreFromOwner = makeGraphQLRequest("MoreFromOwnerQuery", {
"lookup": {
"username": username,
"slug": slug
}
});
const contentList = [];
moreFromOwner?.data?.cloudcast?.moreFromOwner?.edges.forEach(edge => {
if (edge.node.isPlayable) {
contentList.push(cloudcastToPlatformVideo(edge.node));
};
});
return new ContentPager(contentList, false);
};
if (isPreview) {
bridge.toast('Preview - Subscribe to the artist to access the full content.')
}
return platformVideoDetails;
}
/**
* Gets details for a Mixcloud live stream
* @param {string} url URL of the live stream
* @returns {PlatformVideoDetails} Details about the live stream
*/