Skip to content

Commit ba46f00

Browse files
committed
Fix soundcloud
1 parent de47979 commit ba46f00

File tree

8 files changed

+53
-49
lines changed

8 files changed

+53
-49
lines changed

soundcloud/.yarn/install-state.gz

2.21 KB
Binary file not shown.

soundcloud/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DIST_DIR := dist
22

3-
all: build opt pack
3+
all: build pack
44

55
build:
66
@corepack yarn install

soundcloud/esbuild.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const esbuild = require('esbuild')
22
// include this if you need some node support:
33
// npm i @esbuild-plugins/node-modules-polyfill --save-dev
4-
// const { NodeModulesPolyfillPlugin } = require('@esbuild-plugins/node-modules-polyfill')
4+
const { NodeModulesPolyfillPlugin } = require('@esbuild-plugins/node-modules-polyfill')
55

66
esbuild.build({
77
// supports other types like js or ts
@@ -10,11 +10,11 @@ esbuild.build({
1010
bundle: true,
1111
sourcemap: true,
1212
plugins: [
13-
// NodeModulesPolyfillPlugin({
14-
// url: true
15-
// })
13+
NodeModulesPolyfillPlugin({
14+
url: true
15+
})
1616
], // include this if you need some node support
1717
minify: false, // might want to use true for production build
1818
format: 'cjs', // needs to be CJS for now
19-
target: ['es2018'] // don't go over es2020 because quickjs doesn't support it
19+
target: ['es2020'] // don't go over es2020 because quickjs doesn't support it
2020
})

soundcloud/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"permissions": {
1111
"hosts": [
1212
"soundcloud.com",
13-
"*.soundcloud.com"
13+
"*.soundcloud.com",
14+
"*.sndcdn.com"
1415
],
1516
"paths": {}
1617
},
@@ -21,7 +22,7 @@
2122
"devDependencies": {
2223
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
2324
"@extism/js-pdk": "^1.1.1",
24-
"@moosync/edk": "^0.0.6",
25+
"@moosync/edk": "^0.0.7",
2526
"@moosync/packer": "^0.1.4",
2627
"esbuild": "^0.24.0",
2728
"ts-loader": "^9.4.2",

soundcloud/src/index.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Playlist, Song, api } from '@moosync/edk/api'
1+
import { Artist, Exports, Playlist, Song, api } from '@moosync/edk'
22
import { SoundcloudApi } from './soundcloudApi'
33

44
class SoundCloudExtension {
@@ -8,12 +8,6 @@ class SoundCloudExtension {
88
api.setSecure({ key: 'apiKey', value: key })
99
}
1010

11-
async onStarted() {
12-
this.fetchPreferences()
13-
this.registerListeners()
14-
console.info('Started soundcloud extension')
15-
}
16-
1711
private fetchPreferences() {
1812
const key = api.getSecure<string>({ key: 'apiKey' })
1913
this.soundcloudApi.generateKey(key)
@@ -33,34 +27,27 @@ class SoundCloudExtension {
3327
})
3428

3529
api.on('getSearch', async (term) => {
30+
console.log('inside search', term)
3631
const songs = await this.soundcloudApi.searchSongs(term, false)
3732
const artists = await this.soundcloudApi.searchArtist(term, false)
3833
const playlists = await this.soundcloudApi.searchPlaylists(term, false)
3934
return {
40-
songs,
41-
artists,
35+
songs: [],
36+
artists: [],
4237
albums: [],
4338
playlists,
4439
genres: []
4540
}
4641
})
4742

48-
api.on('getArtistSongs', async (artist) => {
49-
// const extraInfo = api.utils.getArtistExtraInfo(artist)
50-
// let artistId: string
51-
// if (!extraInfo || !extraInfo['artist_id']) {
52-
// const soundcloudArtist = (await this.soundcloudApi.searchArtist(artist.artist_name, false))[0]
53-
// if (soundcloudArtist) {
54-
// artistId = api.utils.getArtistExtraInfo(soundcloudArtist).artist_id
55-
// await api.setArtistEditableInfo(artist.artist_id, {
56-
// artist_id: artistId
57-
// })
58-
// }
59-
// } else {
60-
// artistId = extraInfo['artist_id']
61-
// }
43+
api.on('getArtistSongs', async (artist: Artist) => {
44+
const soundcloudArtist = (await this.soundcloudApi.searchArtist(artist.artist_name, false))[0]
45+
if (soundcloudArtist) {
46+
const artistId = soundcloudArtist.artist_id.replace('soundcloud:users:', '')
47+
const songs = await this.soundcloudApi.getArtistSongs(artistId, false)
48+
return { songs }
49+
}
6250

63-
// const songs = await this.soundcloudApi.getArtistSongs(artistId, false)
6451
return {
6552
songs: []
6653
}
@@ -118,4 +105,7 @@ export function entry() {
118105
console.log('Initialized soundcloud ext')
119106
}
120107

121-
export * from '@moosync/edk'
108+
module.exports = {
109+
...module.exports,
110+
...require('@moosync/edk').Exports
111+
}

soundcloud/src/soundcloudApi.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { api, Artist, Playlist, Song } from '@moosync/edk/api'
1+
import { api, Artist, Playlist, Song } from '@moosync/edk'
22
import { 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
65
const 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

soundcloud/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"outDir": "./dist",
55
"moduleResolution": "Bundler",
66
"lib": [], // this ensures unsupported globals aren't suggested
7-
"types": ["@extism/js-pdk"] // while this makes the IDE aware of the ones that are
7+
"types": ["@extism/js-pdk", "@types/node"] // while this makes the IDE aware of the ones that are
88
},
99
"include": ["src/index.ts"]
1010
}

soundcloud/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@ __metadata:
253253
languageName: node
254254
linkType: hard
255255

256-
"@moosync/edk@npm:^0.0.6":
257-
version: 0.0.6
258-
resolution: "@moosync/edk@npm:0.0.6"
259-
checksum: 10c0/a7f1f2e9f8e21b633f61b9955d37bac9d787b0b2cd51d65a9caf915631947b8af9bbb7bafc384e7a9a51135d2fcb97d43a4569b118c42eabc90981e426f7aadb
256+
"@moosync/edk@npm:^0.0.7":
257+
version: 0.0.7
258+
resolution: "@moosync/edk@npm:0.0.7"
259+
checksum: 10c0/b3ddc0c848259446bad37c298b0c26d28747c373e76c13d47890eeeecb2e2eb4d923643fe91ced816c096974aa49670adb82cc6e2c00d284b62ccceb882df9ff
260260
languageName: node
261261
linkType: hard
262262

@@ -1232,7 +1232,7 @@ __metadata:
12321232
dependencies:
12331233
"@esbuild-plugins/node-modules-polyfill": "npm:^0.2.2"
12341234
"@extism/js-pdk": "npm:^1.1.1"
1235-
"@moosync/edk": "npm:^0.0.6"
1235+
"@moosync/edk": "npm:^0.0.7"
12361236
"@moosync/packer": "npm:^0.1.4"
12371237
esbuild: "npm:^0.24.0"
12381238
node-fetch: "npm:^3.2.6"

0 commit comments

Comments
 (0)