/
discogs.coffee
77 lines (64 loc) · 2.12 KB
/
discogs.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
querystring = require 'querystring'
request = require 'request'
compress = require 'compress'
# This is the entry point.
#
# Takes an optional format parameter, defaulting to 'json'. If 'xml' consumer is expected to take care of parsing
#
# client = discogs("xml")
exports = module.exports = (format) ->
gunzip = new compress.Gunzip()
gunzip.init()
# Return a proper url with optional format
getUrl = (url) ->
sep = if "?" in url then "&" else "?"
url = "http://api.discogs.com/#{url}" if url.substr(0, 7) isnt 'http://'
url += "#{sep}f=#{format}" if format
url
# Make a request
discogsRequest = (url, next) ->
request
uri: getUrl url
headers: {'accept-encoding': 'gzip'}
encoding: 'binary'
(error, res, body) =>
if not error and 200 <= res.statusCode < 400
if body
body = gunzip.inflate(body) if 'gzip' in res.headers['content-type']
body = JSON.parse(body) if 'json' in res.headers['content-type'] or not format
next null, body
else
next error
responseHandler = (type, next) ->
(err, res) ->
return next(err, res) if err or res not instanceof Object or type not of res?.resp
next(null, res.resp[type])
# API
# Use this if you have a discogs url
get: (url, next) ->
discogsRequest url, next
# Get a release
master: (id, next) ->
discogsRequest 'master/' + id,
responseHandler('master', next)
# Get a release
release: (id, next) ->
discogsRequest 'release/' + id,
responseHandler('release', next)
# Get an artist
artist: (name, next) ->
discogsRequest 'artist/' + name,
responseHandler('artist', next)
# Get a label
label: (name, next) ->
discogsRequest 'label/' + name,
responseHandler('label', next)
# Search for something
# Valid types:
# `all`, `artists`, `labels`, `releases`, `needsvote`, `catno`, `forsale`
search: (query, type, next) ->
if type instanceof Function
next = type
type = 'all'
discogsRequest 'search?' + querystring.stringify(type: type, q: query),
responseHandler('search', next)