Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/services/AVTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const Service = require('./Service')
const Helpers = require('../helpers')
const enhanceMetadata = require('./EnhanceMetadata')

/**
* Create a new instance of AVTransport
Expand Down Expand Up @@ -196,10 +197,14 @@ class AVTransport extends Service {
const track = Helpers.ParseDIDL(metadata)
track.position = position
track.duration = duration
if (trackUri) track.uri = trackUri
if (process.env.ENHANCE_METADATA) {
const mediaInfo = await this.GetMediaInfo()
await enhanceMetadata(track, metadata, mediaInfo)
}
track.albumArtURL = !track.albumArtURI ? null
: track.albumArtURI.startsWith('http') ? track.albumArtURI
: 'http://' + this.host + ':' + this.port + track.albumArtURI
if (trackUri) track.uri = trackUri
track.queuePosition = queuePosition
return track
} else { // No metadata.
Expand Down
9 changes: 8 additions & 1 deletion lib/services/ContentDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const Service = require('./Service')
const Helpers = require('../helpers')
const enhanceMetadata = require('./EnhanceMetadata')

/**
* Create a new instance of ContentDirectory
Expand Down Expand Up @@ -46,14 +47,20 @@ class ContentDirectory extends Service {
async GetResult (options) {
return this.Browse(options)
.then(this._parseResult)
.then(data => {
.then(async data => {
let items = []
if (data.Result['DIDL-Lite'].container) {
items = this._enumItems(data.Result['DIDL-Lite'].container)
} else {
items = this._enumItems(data.Result['DIDL-Lite'].item)
}

if (process.env.ENHANCE_METADATA && items) {
for (const item of items) {
await enhanceMetadata(item)
}
}

return {
returned: data.NumberReturned,
total: data.TotalMatches,
Expand Down
78 changes: 78 additions & 0 deletions lib/services/EnhanceMetadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const axios = require('axios')
const jsdom = require('jsdom')
const Helpers = require('../helpers')

/**
* Get a soundcloud client_id
* Stolen from https://github.com/3jackdaws/soundcloud-lib
*/
class SoundCloudClientId {
constructor () {
this.clientId = null
}

/**
* Get the id
* @returns {string} The client_id
*/
async get () {
if (this.clientId) return this.clientId

const dom = await jsdom.JSDOM.fromURL('https://soundcloud.com/mt-marcy/cold-nights')
const scripts = dom.window.document.querySelectorAll('script')

for (const script of scripts) {
const src = script.getAttribute('src')
if (src) {
const scriptSrc = await axios({ url: src, method: 'GET' })
if (scriptSrc.status === 200) {
const clientIds = scriptSrc.data.match(/client_id=([a-zA-Z0-9]+)/)
if (clientIds && clientIds.length > 1) {
this.clientId = clientIds[1]
break
}
}
}
}
return this.clientId
}
}

const soundCloudClientId = new SoundCloudClientId()

/**
* Enhance metadata with data from original sources
* * Add radio title and album art
* * Add soundcloud album art
*/
module.exports = async function enhanceMetadata (track, metadata, mediaInfo) {
// Radio
if (mediaInfo && mediaInfo.CurrentURI && mediaInfo.CurrentURI.startsWith('x-sonosapi-stream')) {
track.albumArtURI = `/getaa?s=1&u=${encodeURIComponent(mediaInfo.CurrentURI)}`
if (mediaInfo.CurrentURIMetaData) {
const miMetadata = await Helpers.ParseXml(mediaInfo.CurrentURIMetaData)
const parsedMediainfo = Helpers.ParseDIDL(miMetadata)

// For radio set artist = stream title
if (!track.artist) track.artist = parsedMediainfo.title
}
}

// Soundcloud
const uriParts = /x-sonos-http:track%3a(\d+)\.mp3\?sid=160/.exec(track.uri)
if (uriParts) {
const id = uriParts[1]
const clientId = process.env.SOUNDCLOUD_CLIENT_ID || await soundCloudClientId.get()
const apiUrl = `http://api-v2.soundcloud.com/tracks/${id}?client_id=${clientId}`
try {
const res = await axios({ url: apiUrl, method: 'GET' })
if (res.status === 200 && res.data && res.data.artwork_url) {
track.albumArtURI = res.data.artwork_url.replace('large', 't500x500')
}
} catch (error) {
if (error.response.status === 401) {
console.log('Soundcloud crendentials are not valid, api returned 401')
}
}
}
}
Loading