-
Notifications
You must be signed in to change notification settings - Fork 0
/
songlocator-rdio.coffee
99 lines (76 loc) · 2.48 KB
/
songlocator-rdio.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
###
SongLocator resolver for rdio.
2013 (c) Andrey Popp <[email protected]>
###
{BaseResolver} = require 'songlocator-base'
{OAuth} = require 'oauth'
getArgs = (argc) ->
args =
method: null
params: null
callback: null
if argc.length >= 1
args['method'] = argc[0]
if argc.length == 2
if typeof argc[1] == 'function'
args['callback'] = argc[1]
else
args['params'] = argc[1]
else if argc.length >= 3
args['params'] = argc[1]
args['callback'] = argc[2]
args
class exports.Resolver extends BaseResolver
name: 'rdio'
score: 0.9
endpoint: 'http://api.rdio.com/1/'
constructor: ->
super
this.oauth = if this.options.consumerKey? and this.options.consumerSecret?
new OAuth(
'http://api.rdio.com/oauth/request_token',
'http://api.rdio.com/oauth/access_token',
this.options.consumerKey,
this.options.consumerSecret,
'1.0', 'oob', 'HMAC-SHA1')
else
throw new Error('provide consumerKey and consumerSecret')
makeRequest: (method, params, callback) ->
args = getArgs(arguments)
params = args.params or {}
params['method'] = args.method
callback = args.callback or ->
callbackWrapper = (error, results) ->
try
if results != null
results = JSON.parse(results)
catch e
callback.call({}, error, results)
accessToken = {}
oauthToken = accessToken.oauthAccessToken or ''
oauthTokenSecret = accessToken.oauthAccessTokenSecret or ''
this.oauth.post(this.endpoint, oauthToken, oauthTokenSecret, params,
'application/x-www-form-urlencoded', callbackWrapper)
makeSearch: (qid, query, search = true) ->
this.makeRequest 'search', {query: query, types: 'Track'}, (error, response) =>
return if error?
results = for r in response.result.results.slice(0, this.options.maxSearchResults)
result =
title: r.name
artist: r.artist
album: r.album
source: this.name
id: r.key
linkURL: r.shortUrl
imageURL: r.icon
audioURL: undefined
audioPreviewURL: undefined
mimetype: undefined
duration: r.duration
results = [results[0]] if not search and results.length > 0
this.results(qid, results)
search: (qid, query) ->
this.makeSearch(qid, query)
resolve: (qid, title, artist, album) ->
query = "#{title or ''} #{artist or ''} #{album or ''}"
this.makeSearch(qid, query, false)