Newer
Older
//* Constants
const API_URL = 'https://api-v2.soundcloud.com/'
const APP_LOCALE = 'en'
const PLATFORM = 'Soundcloud'
const USER_AGENT_DESKTOP = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
const USER_AGENT_MOBILE = 'Mozilla/5.0 (Linux; Android 10; Pixel 6a) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
let CLIENT_ID = 'iZIs9mchVcX5lhVRyQGGAYlNPVldzAoX' // correct as of June 2023, enable changes this to get the latest
const URL_ADDITIVE = `&app_version=${SOUNDCLOUD_APP_VERSION}&app_locale=${APP_LOCALE}`
const REGEX_PLAYLISTS_CHANNEL = /^https?:\/\/(www\.)?soundcloud\.com\/([a-zA-Z0-9_-]+)\/(likes|popular-tracks|tracks|reposts|sets\/[a-zA-Z0-9_-]+)$/;
const REGEX_CHANNEL = /^https?:\/\/(www\.)?soundcloud\.com\/([a-zA-Z0-9_-]+)\/?$/;
const systemPlaylistsMaps = {
likes: {
path: 'likes',
apiPath: 'likes',
playlistTitle: 'Likes'
},
tracks: {
path: 'tracks',
apiPath: 'tracks',
playlistTitle: 'Tracks'
},
"popular-tracks": {
path: "popular-tracks",
apiPath: 'toptracks',
playlistTitle: 'Popular Tracks'
},
"reposts": {
path: "reposts",
apiPath: 'reposts',
apiBasePath: 'https://api-v2.soundcloud.com/stream/users',
playlistTitle: 'Reposts'
},
}
let config = {}
let state = {
channel: {}
}
source.enable = function (conf, settings, saveStateStr) {
try {
if (saveStateStr) {
state = JSON.parse(saveStateStr);
}
} catch (ex) {
log('Failed to parse saveState:' + ex);
}
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
return CLIENT_ID
}
source.getHome = function () {
return new QueryPager({ page: 1, page_size: 20 })
}
source.searchSuggestions = function (query) {
const url = `${API_URL}search/queries?q=${query}&client_id=${CLIENT_ID}&limit=10&offset=0&linked_partitioning=1${URL_ADDITIVE}`
const resp = callUrl(url)
/** @type {import("./types").SearchAutofillResponse} */
const json = JSON.parse(resp.body)
if (!json['collection']) {
throw new ScriptException('Could not find collection')
}
/** @type {{output: string; query: string}[]} */
const collection = json['collection']
return collection.map((item) => item['query'])
}
source.getSearchCapabilities = () => {
return {
types: [Type.Feed.Mixed], // can also do albums, playlists, channels those do not have types yet
sorts: [],
filters: [], // filters depend on type
}
}
source.search = function (query, type, order, filters) {
return new SearchPagerVideos({ q: query, page: 1, page_size: 20, get_all: false })
}
source.getSearchChannelContentsCapabilities = function () {
return {
types: [Type.Feed.Mixed],
sorts: [Type.Order.Chronological],
filters: [],
}
}
source.searchChannelContent = function (channelUrl, query, type, order, filters) {
return []
}
source.searchChannels = function (query) {
return new SearchPagerChannels({ q: query, page: 1, page_size: 20 })
}
source.isChannelUrl = function (url) {
// see if it matches https://soundcloud.com/nfrealmusic
return /soundcloud\.com\/[a-zA-Z0-9-_]+\/?/.test(url)
}
source.getChannel = function (url) {
if(state.channel[url]) {
return state.channel[url];
}
const resp = callUrl(url)
const html = resp.body
const matched = html.match(/window\.__sc_hydration = (.+);/)
if (!matched) {
throw new ScriptException('Could not find channel info')
}
/** @type {import("./types").SCHydration[]} */
const json = JSON.parse(matched[1])
for (let object of json) {
if (object.hydratable === 'user') {
state.channel[url] = soundcloudUserToPlatformChannel(object.data);
return state.channel[url];
}
}
throw new ScriptException('Could not find channel info')
}
source.getChannelContents = function (url) {
return new ChannelVideoPager({ url: url, page_size: 20, offset_date: 0 })
}
source.getChannelPlaylists = (url) => {
const channelSlug = extractSoundCloudId(url);
const channel = source.getChannel(`${URL_BASE}/${channelSlug}`);
const author = new PlatformAuthorLink(
new PlatformID(PLATFORM,channel.id.value.toString(),config.id,PLATFORM_CLAIMTYPE),
channel.name,
channel.url,
channel.thumbnail,
);
class ChannelPlaylistsPager extends ContentPager {
constructor({
results = [],
hasMore = true,
context = {withNext: []},
}) {
super(results, hasMore, context);
}
nextPage() {
let withNext = this.context.withNext ?? [];
let batch = http.batch();
withNext.forEach(url => {
batch.GET(url, {});
});
const responses = batch.execute();
withNext = [];
for(var ct = 0; ct < responses.length; ct++) {
const res = responses[ct];
if(res.isOk) {
const body = JSON.parse(res.body);
if(body.next_href) {
withNext.push(`${body.next_href}$?client_id=${CLIENT_ID}`);
}
const currentCollection = body.collection.map(v => {
return new PlatformPlaylist({
id: new PlatformID(PLATFORM, v.id.toString(), config.id, PLATFORM_CLAIMTYPE),
name: v.title,
thumbnail: v.artwork_url,
videoCount: v?.track_count ?? 0,
datetime: dateToUnixSeconds(v.display_date),
url: v.permalink_url,
})
}
}
const hasMore = !!withNext.length;
return new ChannelPlaylistsPager({results: all, hasMore, context: { withNext }});
}
}
let withNext = [
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
`albums`,
`playlists_without_albums`
].map((path) => `https://api-v2.soundcloud.com/users/${channel.id.value}/${path}?client_id=${CLIENT_ID}&limit=10&offset=0&linked_partitioning=1&app_version=${SOUNDCLOUD_APP_VERSION}&app_locale=en`);
// system playlists
let results = [
'likes',
'popular-tracks',
'tracks',
'reposts'
].map(path => {
const info = systemPlaylistsMaps[path];
const name = info?.playlistTitle ?? path;
const playlistPath = info?.path ?? path;
return new PlatformPlaylist({
id: new PlatformID(PLATFORM, '', config.id, PLATFORM_CLAIMTYPE),
author: author,
name: name,
thumbnail: channel.banner || channel.thumbnail || '',
videoCount: 0,
// datetime: dateToUnixSeconds(v.display_date),
url: `https://soundcloud.com/${channelSlug}/${playlistPath}`,
})
})
return new ChannelPlaylistsPager({ results, context: { withNext, firstPage: true } }).nextPage();
source.getChannelTemplateByClaimMap = () => {
return {
//SoundCloud
17: {
0: URL_BASE + "/{{CLAIMVALUE}}"
//Unused! 1: https://api.soundcloud.com/users/{{CLAIMVALUE}}
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
source.isContentDetailsUrl = function (url) {
// https://soundcloud.com/toosii2x/toosii-favorite-song
return /soundcloud\.com\/[a-zA-Z0-9-_]+\/[a-zA-Z0-9-_]+/.test(url)
}
source.getContentDetails = function (url) {
const resp = callUrl(url)
const html = resp.body
const matched = html.match(/window\.__sc_hydration = (.+);/)
if (!matched) {
if(IS_TESTING)
console.log(html);
throw new ScriptException('Could not find video info')
}
/** @type {SCHydration[]} */
const json = JSON.parse(matched[1])
/** @type {import("./types").SoundcloudTrack} */
let data
/** @type {import("./types").SoundcloudTrack} */
let sct
for (let object of json) {
if (object.hydratable === 'sound') {
data = object.data
sct = soundcloudTrackToPlatformVideo(data)
break
}
}
// for performance reasons, select just the mpeg transcoding if it exists; otherwise, select the first transcoding
if (data.media.transcodings?.length === 0) throw new ScriptException('Could not find transcodings')
const transcoding = data.media.transcodings.find((transcoding) => (transcoding.format.mime_type = 'audio/mpeg')) ?? data.media.transcodings[0]
const authorization = data.track_authorization
const generated_url = transcoding.url + `?client_id=${CLIENT_ID}&track_authorization=${authorization}`
const hls_resp = callUrl(generated_url)
const hls_url = JSON.parse(hls_resp.body).url
const sources = [
new HLSSource({
name: `${transcoding.format.mime_type}`,
duration: transcoding.duration,
url: hls_url,
language: "Unknown"
}),
]
sct.video = new UnMuxVideoSourceDescriptor([], sources)
sct.description = data.description
const likesCount = Number.isFinite(data?.likes_count) ? data.likes_count : 0;
sct.rating = new RatingLikes(likesCount);
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
return new PlatformVideoDetails(sct)
}
source.getComments = function (url) {
return new ExtendableCommentPager({ url: url, page: 1, page_size: 20 })
}
// not in Soundcloud
source.getSubComments = function (comment) {
return new CommentPager([], false, {})
}
source.getUserSubscriptions = function () {
const following_resp = callUrl('https://soundcloud.com/you/following', true)
const html = following_resp.body
if(IS_TESTING)
console.log(html)
const matched = html.match(/window\.__sc_hydration = (.+);/)
if (!matched) throw new ScriptException('Could not find user info')
/** @type {SCHydration[]} */
const following_json = JSON.parse(matched[1])
let id
for (let object of following_json) {
if (object.hydratable === 'meUser') {
id = object.data.id
break
}
}
if (!id) throw new ScriptException('Could not find user info')
const resp = callUrl(`${API_URL}users/${id}/followings?client_id=${CLIENT_ID}&limit=12&offset=0&linked_partitioning=1${URL_ADDITIVE}}`, true)
const json = JSON.parse(resp.body)
/** @type {import("./types.d.ts").SoundcloudUser[]} */
const users = json.collection
return users.map((user) => user.permalink_url)
}
source.getUserPlaylists = function () {
const url = `${API_URL}me/library/all?client_id=${CLIENT_ID}&limit=10&offset=0&linked_partioning=1${URL_ADDITIVE}`
const resp = callUrl(url, true)
if(IS_TESTING) {
console.log(url)
console.log(resp.body)
}
const json = JSON.parse(resp.body)
if(IS_TESTING)
console.log(json)
/** @type {import("./types.d.ts").PlaylistWrapper[]} */
const playlists = json.collection
return playlists.map((playlist) => {
if ('playlist' in playlist) {
return playlist.playlist.permalink_url
} else if ('system_playlist' in playlist) {
return playlist.system_playlist.permalink_url
}
})
}
source.isPlaylistUrl = function (url) {
source.getPlaylist = function (url) {
if(isSoundCloudDefaultPlaylist(url)){
return standardPlaylistPager(url);
}
const resp = callUrl(url, true)
const html = resp.body
const matched = html.match(/window\.__sc_hydration = (.+);/)
if (!matched) {
throw new ScriptException('Could not find playlist info')
}
/** @type {SCHydration[]} */
const json = JSON.parse(matched[1])
let playlistTitle = '';
let playlistId = '';
for (let object of json) {
if (object.hydratable === 'systemPlaylist') {
break
} else if (object.hydratable === 'playlist') {
ids = object.data.tracks.map((track) => track.id)
let user = json.find(object => object.hydratable === 'user');
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
let author;
if(user) {
author = new PlatformAuthorLink(
new PlatformID(
PLATFORM,
user.data.id.toString(),
config.id,
PLATFORM_CLAIMTYPE,
),
user.data.username,
user.data.permalink_url,
user.data.avatar_url,
);
} else {
author = new PlatformAuthorLink(
new PlatformID(
PLATFORM,
'',
config.id,
PLATFORM_CLAIMTYPE,
),
'',
'',
'',
);
}
/** @type {import("./types.d.ts").SoundcloudTrack[]} */
let tracks = []
// split ids into chunks of 50
for (let i = 0; i < ids.length; i += 50) {
const chunk = ids.slice(i, i + 50)
const generated_url = `${API_URL}tracks?ids=${chunk.join(',')}&client_id=${CLIENT_ID}${URL_ADDITIVE}`
const chunk_resp = callUrl(generated_url, true)
const found_tracks = JSON.parse(chunk_resp.body)
tracks = tracks.concat(found_tracks)
}
const content = tracks.map(soundcloudTrackToPlatformVideo);
return new PlatformPlaylistDetails({
url: url,
id: new PlatformID(PLATFORM, playlistId, config.id),
author: author,
name: playlistTitle,
videoCount: content?.length ?? 0,
contents: new VideoPager(content)
});
source.saveState = () => {
return JSON.stringify(state);
};
//* Internals
/**
* Gets the URL with correct headers
* @param {string} url
* @param {boolean} is_authenticated
* @param {boolean} use_mobile
* @returns {HTTPResponse}
*/
function callUrl(url, is_authenticated = false, use_mobile = false) {
if(!use_mobile) {
url = removeMobilePrefix(url);
}
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
let headers = {
'User-Agent': use_mobile ? USER_AGENT_MOBILE : USER_AGENT_DESKTOP,
DNT: '1',
Connection: 'keep-alive',
Origin: 'https://soundcloud.com',
Referer: 'https://soundcloud.com/',
}
let accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
if (url.includes('api-v2.soundcloud.com')) {
accept = 'application/json, text/javascript, */*; q=0.01'
headers['Host'] = 'api-v2.soundcloud.com'
headers['SEC-FETCH-DEST'] = 'empty'
headers['SEC-FETCH-MODE'] = 'cors'
headers['SEC-FETCH-SITE'] = 'same-site'
} else {
headers['SEC-FETCH-DEST'] = 'document'
headers['SEC-FETCH-MODE'] = 'navigate'
headers['SEC-FETCH-SITE'] = 'none'
}
headers['Accept'] = accept
return http.GET(url, headers, is_authenticated)
}
/**
* Gets the client_id from the Soundcloud home page
* @returns {string} returns the client_id
*/
function getClientId() {
// request soundcloud.com to find the url of the js file that contains 50-_____.js
const resp = callUrl('https://soundcloud.com/discover', false, true)
const html = resp.body
// find "clientId":"iZIs9mchVcX5lhVRyQGGAYlNPVldzAoX"
const matched = html.match(/"clientId":"([a-zA-Z0-9-_]+)"/)
if (!matched) {
throw new ScriptException('Could not find client_id')
}
const clientId = matched[1]
return clientId
}
/**
* Gets the Soundcloud homepage content
* @param {import("./types").HomeContext} context the search context
* @returns {PlatformVideo[]} returns the homepage content
*/
function getHomepageContent(context) {
const limit = context.page_size
const offset = (context.page - 1) * limit
const url = `${API_URL}featured_tracks/top/all-music?client_id=${CLIENT_ID}&limit=${limit}&offset=${offset}&linked_partitioning=1${URL_ADDITIVE}`
const resp = callUrl(url)
/** @type {import("./types").HomepageResponse} */
const json = JSON.parse(resp.body)
/** @type {import("./types").SoundcloudTrack[]} */
const tracks = json['collection']
const results = ensureUniqueByProperty(tracks, 'id')
.map((track) => {
});
const hasMore = json?.['next_href'] !== null
return { results, hasMore }
}
//* Pagers
class QueryPager extends VideoPager {
/**
* @param {import("./types.d.ts").HomeContext} context the query params
*/
constructor(context) {
const data = getHomepageContent(context);
super(data.results, data.hasMore, context)
}
nextPage() {
this.context.page = this.context.page + 1
const data = getHomepageContent(this.context)
this.results = data.results;
this.hasMore = data.hasMore;
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
return this
}
}
class SearchPagerVideos extends VideoPager {
/**
* @param {import("./types").SearchContext} context the query params
*/
constructor(context) {
if (context.get_all) {
// https://api-v2.soundcloud.com/search?q=search%20and%20destroy%20drake&client_id=VDJ3iu7ZYtUMibDTM2XcUbRijDa3L6ug&limit=20&offset=0&linked_partitioning=1&app_version=1683798046&app_locale=en
const limit = context.page_size
const offset = (context.page - 1) * limit
const url = `${API_URL}search?q=${encodeURIComponent(
context.q
)}&client_id=${CLIENT_ID}&limit=${limit}&offset=${offset}&linked_partitioning=1${URL_ADDITIVE}`
const resp = callUrl(url)
/** @type {import("./types").AnySearchResponse} */
const json = JSON.parse(resp.body)
if (json['collection'] === undefined) {
if(IS_TESTING)
console.log('Soundcloud search response: ' + resp.body)
throw new ScriptException('Could not find collection')
}
/** @type {(PlatformVideo | PlatformChannel | PlatformPlaylist)[]} */
const results = []
for (const result of json['collection']) {
if (result['kind'] === 'track') {
results.push(soundcloudTrackToPlatformVideo(result))
} else if (result['kind'] === 'user') {
continue
results.push(soundcloudUserToPlatformChannel(result))
} else if (result['kind'] === 'playlist' || result['kind'] === 'album') {
// results.push(soundcloudPlaylistToPlatformPlaylist(result))
} else {
if(IS_TESTING)
console.log('Soundcloud search result: ' + JSON.stringify(result))
throw new ScriptException('Unknown kind: ' + result['kind'])
}
}
super(results, results.length >= context.page_size, context)
} else {
const limit = context.page_size
const offset = (context.page - 1) * limit
const url = `${API_URL}search/tracks?limit=${limit}&offset=${offset}&q=${context.q}&client_id=${CLIENT_ID}${URL_ADDITIVE}`
const resp = callUrl(url)
/** @type {SearchResponse} */
const json = JSON.parse(resp.body)
/** @type {SoundcloudTrack[]} */
const tracks = json['collection']
const results = tracks.map((track) => soundcloudTrackToPlatformVideo(track))
super(results, results.length >= context.page_size, context)
}
}
nextPage() {
this.context.page = this.context.page + 1
return new SearchPagerVideos(this.context)
}
}
class ChannelVideoPager extends VideoPager {
/**
* @param {import("./types.d.ts").ChannelVideoPagerContext} context
*/
constructor(context) {
if (!context.id) {
const resp = callUrl(context.url)
const matched = resp.body.match(/window\.__sc_hydration = (.+);/)
if (!matched) {
throw new ScriptException('Could not find channel info')
}
/** @type {import("./types").SCHydration[]} */
const json = JSON.parse(matched[1])
for (let object of json) {
if (object.hydratable === 'user') {
/** @type {import("./types").SoundcloudUser} */
const data = object.data
context.id = data.id
break
}
}
}
const url = `${API_URL}users/${context.id}/tracks?representation=&client_id=${CLIENT_ID}&limit=${context.page_size}&offset=${context.offset_date}&linked_partitioning=1${URL_ADDITIVE}`
if(IS_TESTING)
console.log('Soundcloud channel url: ' + url)
const resp = callUrl(url)
const parsed = JSON.parse(resp.body)
/** @type {import("./types").SoundcloudTrack[]} */
const tracks = parsed['collection']
const videos = tracks.map((track) => soundcloudTrackToPlatformVideo(track))
context['offset_date'] = tracks[tracks.length - 1]?.created_at
super(videos, tracks.length > 0, context)
}
nextPage() {
this.context.page = this.context.page + 1
return new ChannelVideoPager(this.context)
}
}
class SearchPagerChannels extends ChannelPager {
/**
* @param {import("./types").SearchContext} context the query params
*/
constructor(context) {
const limit = context.page_size
const offset = (context.page - 1) * limit
const url = `${API_URL}search/users?q=${context.q}&client_id=${CLIENT_ID}&limit=${limit}&offset=${offset}&linked_partitioning=1${URL_ADDITIVE}`
const resp = callUrl(url)
/** @type {SearchResponse} */
const json = JSON.parse(resp.body)
/** @type {SoundcloudUser[]} */
const users = json['collection']
const results = users.map((user) => soundcloudUserToPlatformChannel(user))
super(results, results.length >= context.page_size, context)
}
nextPage() {
this.context.page = this.context.page + 1
return new SearchPagerChannels(this.context)
}
}
class ExtendableCommentPager extends CommentPager {
/**
* @param {import("./types.d.ts").HomeContext & {url: string; id: number|null}} context
*/
constructor(context) {
if (!context.id) {
const resp = callUrl(context.url)
const html = resp.body
const matched = html.match(/window\.__sc_hydration = (.+);/)
if (!matched) {
throw new ScriptException('Could not find comment info')
}
/** @type {import("./types").SCHydration[]} */
const json = JSON.parse(matched[1])
for (let object of json) {
if (object.hydratable === 'sound') {
/** @type {import("./types").SoundcloudTrack} */
const data = object.data
context.id = data.id
break
}
}
}
// https://api-v2.soundcloud.com/tracks/1506477625/comments?sort=newest&threaded=1&client_id=TihN0nuDfhghD9GVPbTtrSEa558lYo4V&limit=20&offset=0&linked_partitioning=1&app_version=1684153290&app_locale=en
const limit = context.page_size
const offset = (context.page - 1) * limit
const url = `${API_URL}tracks/${context.id}/comments?sort=newest&threaded=1&client_id=${CLIENT_ID}&limit=${limit}&offset=${offset}&linked_partitioning=1${URL_ADDITIVE}`
const resp = callUrl(url)
/** @type {import("./types").CommentResponse} */
const json = JSON.parse(resp.body)
const comments = json['collection'].map((comment) => {
return new Comment({
contextUrl: context.url,
author: new PlatformAuthorLink(
new PlatformID(PLATFORM, comment.user.id.toString(), config.id, PLATFORM_CLAIMTYPE),
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
comment.user.username,
comment.user.permalink_url,
comment.user.avatar_url
),
message: comment.body,
rating: new RatingLikes(0),
date: parseInt(new Date(comment.created_at).getTime() / 1000),
replyCount: 0,
context: null,
})
})
super(comments, json['next_href'] !== null, context)
}
nextPage() {
this.context.page = this.context.page + 1
return new ExtendableCommentPager(this.context)
}
}
//* CONVERTERS
/**
* Convert a Soundcloud person to a PlatformChannel
* @param { import("./types").SoundcloudUser } scu
* @returns { PlatformChannel }
*/
function soundcloudUserToPlatformChannel(scu) {
if (!scu || typeof scu !== 'object') {
throw new ScriptException('Invalid SoundCloud user object');
}
const visuals = scu.visuals?.visuals || [];
const banner = visuals?.[0]?.visual_url || '';
const links = visuals.map(v => v.link).filter(Boolean);
id: new PlatformID(PLATFORM, scu.id.toString(), config.id, PLATFORM_CLAIMTYPE),
banner,
subscribers: scu.followers_count || 0,
}
/**
* Convert a Soundcloud Track to a PlatformVideo
* @param { import("./types").SoundcloudTrack } sct
* @returns { PlatformVideo }
*/
function soundcloudTrackToPlatformVideo(sct) {
return new PlatformVideo({
id: new PlatformID(PLATFORM, sct.id.toString(), config.id),
name: sct.title,
thumbnails: new Thumbnails([new Thumbnail(sct.artwork_url !== null ? sct.artwork_url.replace('large', 't500x500') : sct.artwork_url, 0)]),
author: new PlatformAuthorLink(
new PlatformID(PLATFORM, sct.user_id.toString(), config.id, PLATFORM_CLAIMTYPE),
sct.user.username,
sct.user.permalink_url,
sct.user.avatar_url
),
uploadDate: parseInt(new Date(sct.created_at).getTime() / 1000),
duration: parseInt(sct.duration / 1000),
viewCount: sct.playback_count,
url: sct.permalink_url,
isLive: false,
})
}
/**
* Replace the "m." prefix in a SoundCloud URL with an empty string.
*
* @param {string} url - The SoundCloud URL to modify.
* @returns {string} - The modified URL without the "m." prefix.
*/
function removeMobilePrefix(url) {
return url.trim().replace("https://m.", "https://");
}
/**
* Currently the trending pages has some duplicates, this function ensures that the array is unique based on the specified property.
* Ensures that each item in the array is unique based on the specified property.
* @param {Array} array - The array of objects to process.
* @param {string} property - The property to use for uniqueness.
* @returns {Array} - A new array with unique items based on the specified property.
*/
function ensureUniqueByProperty(array, property) {
const seen = new Set();
return array.filter(item => {
if (item[property] && !seen.has(item[property])) {
seen.add(item[property]);
return true;
}
return false;
});
}
function extractSoundCloudId(url) {
if (!url) return null;
const match = url.match(REGEX_CHANNEL);
if (match) {
return match[2]; // The second capturing group contains the SoundCloud ID
}
return null; // Return null if no match
}
function isSoundCloudChannelPlaylistUrl(url) {
return REGEX_PLAYLISTS_CHANNEL.test(url);
}
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
function isSoundCloudDefaultPlaylist(url) {
const defaultPlaylistPatternRegex = /^https?:\/\/(www\.)?soundcloud\.com\/[a-zA-Z0-9_-]+\/(likes|popular-tracks|toptracks|tracks|reposts)$/;
return defaultPlaylistPatternRegex.test(url);
}
function standardPlaylistPager(url){
const urlDetails = extractSoundCloudDetails(url);
const channelSlug = urlDetails.userId;
const playlist = urlDetails.trackId;
const channel = source.getChannel(`${URL_BASE}/${channelSlug}`);
const info = systemPlaylistsMaps[playlist];
const apiPath = info?.apiPath ?? playlist;
const playlistTitle = info?.playlistTitle ?? playlist;
const apiBasePath = info?.apiBasePath ?? 'https://api-v2.soundcloud.com/users';
let withNext = [
`${apiBasePath}/${channel.id.value}/${apiPath}?client_id=${CLIENT_ID}&limit=10&offset=0&linked_partitioning=1&app_version=1735826482&app_locale=en`
]
class ChannelPlaylistsPager extends VideoPager {
constructor({
results,
hasMore,
context,
}) {
super(results, hasMore, context);
}
nextPage() {
let withNext = this.context.withNext ?? [];
let all = this.results ?? [];
let batch = http.batch();
withNext.forEach(url => {
batch.GET(url, {});
});
const responses = batch.execute();
withNext = [];
for(var ct = 0; ct < responses.length; ct++) {
const res = responses[ct];
if(res.isOk) {
const body = JSON.parse(res.body);
if(body.next_href) {
withNext.push(`${body.next_href}&client_id=${CLIENT_ID}`);
}
const currentCollection = body.collection.filter(c => c.track || c.kind === 'track').map(c => soundcloudTrackToPlatformVideo(c.track ?? c));
all = [...all, ...currentCollection]
}
}
const hasMore = !!withNext.length;
return new ChannelPlaylistsPager({results: all, hasMore, context: { withNext }});
}
}
const author = new PlatformAuthorLink(
new PlatformID(PLATFORM,channel.id.value.toString(),config.id,PLATFORM_CLAIMTYPE),
channel.name,
channel.url,
channel.thumbnail,
);
let contentPager = new ChannelPlaylistsPager({ context: { withNext } }).nextPage();
return new PlatformPlaylistDetails({
url: url,
id: new PlatformID(PLATFORM, '', config.id),
author: author,
name: playlistTitle,
// thumbnail: "",
videoCount: 0,
contents: contentPager
});
}
function extractSoundCloudDetails(url) {
if (!url) return null;
const match = url.match(/^https?:\/\/(www\.)?soundcloud\.com\/([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)\/?$/);
if (match) {
return {
userId: match[2], // Extracted user/artist name
trackId: match[3] // Extracted track identifier
};
}
return null; // Return null if the URL doesn't match the expected pattern
}
function dateToUnixSeconds(date) {
if (!date) {