-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonglocator-exfm.coffee
100 lines (76 loc) · 3.23 KB
/
songlocator-exfm.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
100
###
SongLocator resolver for Ex.fm.
2013 (c) Andrey Popp <[email protected]>
Based on Tomahawk YouTube resolver.
2011 (c) Lasconic <[email protected]>
###
((root, factory) ->
if typeof exports == 'object'
SongLocator = require('songlocator-base')
module.exports = factory(SongLocator)
else if typeof define == 'function' and define.amd
define (require) ->
SongLocator = require('songlocator-base')
root.SongLocator.EXFM = factory(SongLocator)
else
root.SongLocator.EXFM = factory(SongLocator)
) this, ({BaseResolver, extend}) ->
class Resolver extends BaseResolver
name: 'exfm'
options: extend({}, BaseResolver::options, {searchMaxResults: 11})
resolve: (qid, track, artist, album, search = false) ->
query = "#{track or ''} #{artist or ''}".trim()
this.request
url: "http://ex.fm/api/v3/song/search/#{encodeURIComponent(query)}"
params:
start: 0,
results: this.options.searchMaxResults
callback: (error, response) =>
return if error
return unless response.results > 0
results = for song in response.songs
# use soundcloud instead
continue if song.url.indexOf("http://api.soundcloud.com") == 0
continue if song.url.indexOf("https://api.soundcloud.com") == 0
continue unless song.artist
if song.title?
dTitle = if song.title.indexOf("\n") != -1
song.title
.split("\n")
.map((v) -> v.trim())
.join(' ')
else
song.title
dTitle = dTitle
.replace("\u2013","")
.replace(" ", " ")
.replace("\u201c","")
.replace("\u201d","")
if dTitle.toLowerCase().indexOf(song.artist.toLowerCase() + " -") == 0
dTitle = dTitle.slice(song.artist.length + 2).trim()
else if dTitle.toLowerCase().indexOf(song.artist.toLowerCase() + "-") == 0
dTitle = dTitle.slice(song.artist.length + 1).trim()
else if dTitle.toLowerCase() == song.artist.toLowerCase()
continue
else if dTitle.toLowerCase().indexOf(song.artist.toLowerCase()) == 0
dTitle = dTitle.slice(song.artist.length).trim()
continue if not (
dTitle.toLowerCase().indexOf(track.toLowerCase()) != -1 \
and (search or song.artist.toLowerCase().indexOf((artist or '').toLowerCase()) != -1) \
or (search or artist == "" and album == ""))
result =
title: dTitle or title
artist: song.artist
album: song.album
source: this.name
id: song.id
linkURL: song.sources?[0]
imageURL: song.image.large or song.image.medium or song.image.small
audioURL: song.url
audioPreviewURL: undefined
mimetype: "audio/mpeg"
duration: undefined
this.results(qid, if not search then [results[0]] else results)
search: (qid, searchString) ->
this.resolve(qid, searchString, undefined, undefined, true)
{Resolver}