import logger from 'electron-timber' ; import _ from 'lodash' ; import { StreamData , StreamQuery } from '../plugins.types' ; import StreamProviderPlugin from '../streamProvider' ; import * as Audius from '../../rest/Audius' ; class AudiusPlugin extends StreamProviderPlugin { constructor ( ) { super ( ) ; this . name = 'Audius Plugin' ; this . sourceName = 'Audius' ; this . description = 'A plugin that adds Audius search and streaming support to Nuclear.' ; this . baseUrl = 'https://audius.co' ; this . image = null ; this . init ( ) ; } async init ( ) { this . apiEndpoint = await Audius . _findHost ( ) ; } search ( query ) { const terms = query . artist + ' ' + query . track ; return Audius . trackSearch ( this . apiEndpoint , terms ) . then ( data => data . json ( ) ) . then ( results => { const info = results . data [ 0 ] ; return info ? this . createStreamData ( info ) : null ; } ) . catch ( err => { logger . error ( `Error while looking up streams for ${ terms } on Audius` ) ; logger . error ( err ) ; } ) ; } getAlternateStream ( query , currentStream ) { const terms = query . track ; return Audius . trackSearch ( this . apiEndpoint , terms ) . then ( data => data . json ( ) ) . then ( results => { const info = _ . find ( results . data , result => result && result . id !== currentStream . id ) ; return info ? this . createStreamData ( info ) : null ; } ) . catch ( err => { logger . error ( `Error while looking up alternate streams for ${ terms } on Audius` ) ; logger . error ( err ) ; } ) ; } createStreamData ( result ) { return { source : this . sourceName , id : result . id , stream : `${ this . apiEndpoint }/tracks/${ result . id }/stream?app_name=Nuclear` , duration : result . duration , title : result . title , thumbnail : result . artwork ? result . artwork [ '480x480' ] : '' , originalUrl : `${ this . baseUrl }${ result . permalink }` } ; } getStreamForId ( id ) { return Audius . getTrack ( this . apiEndpoint , id ) . then ( data => data . json ( ) ) . then ( results => { return results . data ? this . createStreamData ( results . data ) : null ; } ) . catch ( err => { logger . error ( `Error while looking up streams for id: ${ id } on Audius` ) ; logger . error ( err ) ; } ) ; } } export default AudiusPlugin ;