Skip to content

Commit

Permalink
Fix soundcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovenoboyo committed Dec 28, 2024
1 parent de47979 commit ba46f00
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 49 deletions.
Binary file modified soundcloud/.yarn/install-state.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion soundcloud/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DIST_DIR := dist

all: build opt pack
all: build pack

build:
@corepack yarn install
Expand Down
10 changes: 5 additions & 5 deletions soundcloud/esbuild.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const esbuild = require('esbuild')
// include this if you need some node support:
// npm i @esbuild-plugins/node-modules-polyfill --save-dev
// const { NodeModulesPolyfillPlugin } = require('@esbuild-plugins/node-modules-polyfill')
const { NodeModulesPolyfillPlugin } = require('@esbuild-plugins/node-modules-polyfill')

esbuild.build({
// supports other types like js or ts
Expand All @@ -10,11 +10,11 @@ esbuild.build({
bundle: true,
sourcemap: true,
plugins: [
// NodeModulesPolyfillPlugin({
// url: true
// })
NodeModulesPolyfillPlugin({
url: true
})
], // include this if you need some node support
minify: false, // might want to use true for production build
format: 'cjs', // needs to be CJS for now
target: ['es2018'] // don't go over es2020 because quickjs doesn't support it
target: ['es2020'] // don't go over es2020 because quickjs doesn't support it
})
5 changes: 3 additions & 2 deletions soundcloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"permissions": {
"hosts": [
"soundcloud.com",
"*.soundcloud.com"
"*.soundcloud.com",
"*.sndcdn.com"
],
"paths": {}
},
Expand All @@ -21,7 +22,7 @@
"devDependencies": {
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
"@extism/js-pdk": "^1.1.1",
"@moosync/edk": "^0.0.6",
"@moosync/edk": "^0.0.7",
"@moosync/packer": "^0.1.4",
"esbuild": "^0.24.0",
"ts-loader": "^9.4.2",
Expand Down
40 changes: 15 additions & 25 deletions soundcloud/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Playlist, Song, api } from '@moosync/edk/api'
import { Artist, Exports, Playlist, Song, api } from '@moosync/edk'
import { SoundcloudApi } from './soundcloudApi'

class SoundCloudExtension {
Expand All @@ -8,12 +8,6 @@ class SoundCloudExtension {
api.setSecure({ key: 'apiKey', value: key })
}

async onStarted() {
this.fetchPreferences()
this.registerListeners()
console.info('Started soundcloud extension')
}

private fetchPreferences() {
const key = api.getSecure<string>({ key: 'apiKey' })
this.soundcloudApi.generateKey(key)
Expand All @@ -33,34 +27,27 @@ class SoundCloudExtension {
})

api.on('getSearch', async (term) => {
console.log('inside search', term)
const songs = await this.soundcloudApi.searchSongs(term, false)
const artists = await this.soundcloudApi.searchArtist(term, false)
const playlists = await this.soundcloudApi.searchPlaylists(term, false)
return {
songs,
artists,
songs: [],
artists: [],
albums: [],
playlists,
genres: []
}
})

api.on('getArtistSongs', async (artist) => {
// const extraInfo = api.utils.getArtistExtraInfo(artist)
// let artistId: string
// if (!extraInfo || !extraInfo['artist_id']) {
// const soundcloudArtist = (await this.soundcloudApi.searchArtist(artist.artist_name, false))[0]
// if (soundcloudArtist) {
// artistId = api.utils.getArtistExtraInfo(soundcloudArtist).artist_id
// await api.setArtistEditableInfo(artist.artist_id, {
// artist_id: artistId
// })
// }
// } else {
// artistId = extraInfo['artist_id']
// }
api.on('getArtistSongs', async (artist: Artist) => {
const soundcloudArtist = (await this.soundcloudApi.searchArtist(artist.artist_name, false))[0]
if (soundcloudArtist) {
const artistId = soundcloudArtist.artist_id.replace('soundcloud:users:', '')
const songs = await this.soundcloudApi.getArtistSongs(artistId, false)
return { songs }
}

// const songs = await this.soundcloudApi.getArtistSongs(artistId, false)
return {
songs: []
}
Expand Down Expand Up @@ -118,4 +105,7 @@ export function entry() {
console.log('Initialized soundcloud ext')
}

export * from '@moosync/edk'
module.exports = {
...module.exports,
...require('@moosync/edk').Exports
}
33 changes: 23 additions & 10 deletions soundcloud/src/soundcloudApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { api, Artist, Playlist, Song } from '@moosync/edk/api'
import { api, Artist, Playlist, Song } from '@moosync/edk'
import { PlaylistInfo, Playlists, TrackInfo, Tracks, UserInfo } from './types'
import { URL, URLSearchParams } from 'url'

// https://github.com/DevAndromeda/soundcloud-scraper/blob/master/src/constants/Constants.js
const SCRIPT_URL_MATCH_REGEX =
Expand Down Expand Up @@ -30,6 +29,7 @@ export class SoundcloudApi {
const data = this.getRaw(new URL(u))
if (data.includes(',client_id:"')) {
const a = data.split(',client_id:"')
console.log('got key', a[1].split('"')[0])
return a[1].split('"')[0]
}
}
Expand All @@ -38,16 +38,20 @@ export class SoundcloudApi {

public async generateKey(key?: string) {
if (!key) {
console.log('fetching key')
key = await this.fetchKey()
console.log('fetched key', key)
}

console.log('setting key', key)
this.key = key
this.updateKeyCallback(key)
// this.updateKeyCallback(key)
}

private getRaw(url: URL) {
console.log('Fetching', url)
const resp = Http.request({
url,
url: url.toString(),
headers: {
'x-requested-with': 'https://soundcloud.com'
},
Expand All @@ -62,6 +66,10 @@ export class SoundcloudApi {
invalidateCache: boolean,
maxTries = 0
): Promise<T | undefined> {
if (!this.key) {
await this.generateKey()
}

const parsedParams = new URLSearchParams({
...params,
client_id: this.key
Expand All @@ -73,7 +81,9 @@ export class SoundcloudApi {
// }

try {
const resp = JSON.parse(this.getRaw(parsedUrl))
const raw = this.getRaw(parsedUrl)
console.log('raw', parsedUrl)
const resp = JSON.parse(raw)
// this.cacheHandler.addToCache(url.toString(), resp)
return resp
} catch (e) {
Expand Down Expand Up @@ -185,7 +195,7 @@ export class SoundcloudApi {
'/search/playlists',
{
q: term,
limit: 50
limit: 10
},
invalidateCache
)
Expand All @@ -194,6 +204,7 @@ export class SoundcloudApi {
}

public async searchSongs(term: string, invalidateCache: boolean) {
console.log('requesting songs')
const data = await this.get<TrackInfo>(
'/search/tracks',
{
Expand All @@ -203,6 +214,7 @@ export class SoundcloudApi {
invalidateCache
)

console.log('got data', data)
return await this.parseSongs(invalidateCache, ...data.collection)
}

Expand Down Expand Up @@ -237,9 +249,7 @@ export class SoundcloudApi {
}

private findStreamURL(track: Tracks) {
const streamUrl = track.media.transcodings.find(
(val) => val.format.protocol === 'progressive' && !val.url.includes('preview')
)?.url
const streamUrl = track.media.transcodings.find((val) => val.format.protocol === 'progressive')?.url

return streamUrl
}
Expand All @@ -261,6 +271,7 @@ export class SoundcloudApi {

if (t.streamable && t.media?.transcodings) {
const streamUrl = this.findStreamURL(t)
console.log('got stream url', streamUrl)
if (streamUrl) {
// this.cacheHandler.addToCache(`song:${t.id}`, streamUrl)
songs.push({
Expand Down Expand Up @@ -314,7 +325,9 @@ export class SoundcloudApi {

const data = await this.get<TrackInfo>(`/users/${urn}/tracks`, params, invalidateCache)
if (data.collection) {
tracks.push(...(await this.parseSongs(invalidateCache, ...data.collection)))
const songs = await this.parseSongs(invalidateCache, ...data.collection)
console.log('ot collections', songs)
tracks.push(...songs)
}

next = data.next_href
Expand Down
2 changes: 1 addition & 1 deletion soundcloud/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "./dist",
"moduleResolution": "Bundler",
"lib": [], // this ensures unsupported globals aren't suggested
"types": ["@extism/js-pdk"] // while this makes the IDE aware of the ones that are
"types": ["@extism/js-pdk", "@types/node"] // while this makes the IDE aware of the ones that are
},
"include": ["src/index.ts"]
}
10 changes: 5 additions & 5 deletions soundcloud/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ __metadata:
languageName: node
linkType: hard

"@moosync/edk@npm:^0.0.6":
version: 0.0.6
resolution: "@moosync/edk@npm:0.0.6"
checksum: 10c0/a7f1f2e9f8e21b633f61b9955d37bac9d787b0b2cd51d65a9caf915631947b8af9bbb7bafc384e7a9a51135d2fcb97d43a4569b118c42eabc90981e426f7aadb
"@moosync/edk@npm:^0.0.7":
version: 0.0.7
resolution: "@moosync/edk@npm:0.0.7"
checksum: 10c0/b3ddc0c848259446bad37c298b0c26d28747c373e76c13d47890eeeecb2e2eb4d923643fe91ced816c096974aa49670adb82cc6e2c00d284b62ccceb882df9ff
languageName: node
linkType: hard

Expand Down Expand Up @@ -1232,7 +1232,7 @@ __metadata:
dependencies:
"@esbuild-plugins/node-modules-polyfill": "npm:^0.2.2"
"@extism/js-pdk": "npm:^1.1.1"
"@moosync/edk": "npm:^0.0.6"
"@moosync/edk": "npm:^0.0.7"
"@moosync/packer": "npm:^0.1.4"
esbuild: "npm:^0.24.0"
node-fetch: "npm:^3.2.6"
Expand Down

0 comments on commit ba46f00

Please sign in to comment.