1- import { api , Artist , Playlist , Song } from '@moosync/edk/api '
1+ import { api , Artist , Playlist , Song } from '@moosync/edk'
22import { PlaylistInfo , Playlists , TrackInfo , Tracks , UserInfo } from './types'
3- import { URL , URLSearchParams } from 'url'
43
54// https://github.com/DevAndromeda/soundcloud-scraper/blob/master/src/constants/Constants.js
65const SCRIPT_URL_MATCH_REGEX =
@@ -30,6 +29,7 @@ export class SoundcloudApi {
3029 const data = this . getRaw ( new URL ( u ) )
3130 if ( data . includes ( ',client_id:"' ) ) {
3231 const a = data . split ( ',client_id:"' )
32+ console . log ( 'got key' , a [ 1 ] . split ( '"' ) [ 0 ] )
3333 return a [ 1 ] . split ( '"' ) [ 0 ]
3434 }
3535 }
@@ -38,16 +38,20 @@ export class SoundcloudApi {
3838
3939 public async generateKey ( key ?: string ) {
4040 if ( ! key ) {
41+ console . log ( 'fetching key' )
4142 key = await this . fetchKey ( )
43+ console . log ( 'fetched key' , key )
4244 }
4345
46+ console . log ( 'setting key' , key )
4447 this . key = key
45- this . updateKeyCallback ( key )
48+ // this.updateKeyCallback(key)
4649 }
4750
4851 private getRaw ( url : URL ) {
52+ console . log ( 'Fetching' , url )
4953 const resp = Http . request ( {
50- url,
54+ url : url . toString ( ) ,
5155 headers : {
5256 'x-requested-with' : 'https://soundcloud.com'
5357 } ,
@@ -62,6 +66,10 @@ export class SoundcloudApi {
6266 invalidateCache : boolean ,
6367 maxTries = 0
6468 ) : Promise < T | undefined > {
69+ if ( ! this . key ) {
70+ await this . generateKey ( )
71+ }
72+
6573 const parsedParams = new URLSearchParams ( {
6674 ...params ,
6775 client_id : this . key
@@ -73,7 +81,9 @@ export class SoundcloudApi {
7381 // }
7482
7583 try {
76- const resp = JSON . parse ( this . getRaw ( parsedUrl ) )
84+ const raw = this . getRaw ( parsedUrl )
85+ console . log ( 'raw' , parsedUrl )
86+ const resp = JSON . parse ( raw )
7787 // this.cacheHandler.addToCache(url.toString(), resp)
7888 return resp
7989 } catch ( e ) {
@@ -185,7 +195,7 @@ export class SoundcloudApi {
185195 '/search/playlists' ,
186196 {
187197 q : term ,
188- limit : 50
198+ limit : 10
189199 } ,
190200 invalidateCache
191201 )
@@ -194,6 +204,7 @@ export class SoundcloudApi {
194204 }
195205
196206 public async searchSongs ( term : string , invalidateCache : boolean ) {
207+ console . log ( 'requesting songs' )
197208 const data = await this . get < TrackInfo > (
198209 '/search/tracks' ,
199210 {
@@ -203,6 +214,7 @@ export class SoundcloudApi {
203214 invalidateCache
204215 )
205216
217+ console . log ( 'got data' , data )
206218 return await this . parseSongs ( invalidateCache , ...data . collection )
207219 }
208220
@@ -237,9 +249,7 @@ export class SoundcloudApi {
237249 }
238250
239251 private findStreamURL ( track : Tracks ) {
240- const streamUrl = track . media . transcodings . find (
241- ( val ) => val . format . protocol === 'progressive' && ! val . url . includes ( 'preview' )
242- ) ?. url
252+ const streamUrl = track . media . transcodings . find ( ( val ) => val . format . protocol === 'progressive' ) ?. url
243253
244254 return streamUrl
245255 }
@@ -261,6 +271,7 @@ export class SoundcloudApi {
261271
262272 if ( t . streamable && t . media ?. transcodings ) {
263273 const streamUrl = this . findStreamURL ( t )
274+ console . log ( 'got stream url' , streamUrl )
264275 if ( streamUrl ) {
265276 // this.cacheHandler.addToCache(`song:${t.id}`, streamUrl)
266277 songs . push ( {
@@ -314,7 +325,9 @@ export class SoundcloudApi {
314325
315326 const data = await this . get < TrackInfo > ( `/users/${ urn } /tracks` , params , invalidateCache )
316327 if ( data . collection ) {
317- tracks . push ( ...( await this . parseSongs ( invalidateCache , ...data . collection ) ) )
328+ const songs = await this . parseSongs ( invalidateCache , ...data . collection )
329+ console . log ( 'ot collections' , songs )
330+ tracks . push ( ...songs )
318331 }
319332
320333 next = data . next_href
0 commit comments