Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 0c166952f5952f2651c770939d0711fb5dd9e562 @linus committed Apr 7, 2011
Showing with 499 additions and 0 deletions.
  1. +61 −0 README.md
  2. +84 −0 discogs.coffee
  3. +88 −0 discogs.js
  4. +64 −0 docs/discogs.html
  5. +186 −0 docs/docco.css
  6. +16 −0 package.json
@@ -0,0 +1,61 @@
+Discogs: A simple JavaScript wrapper for the Discogs API
+========================================================
+
+`discogs` is a simple wrapper for the [Discogs API](http://www.discogs.com/help/api), written in CoffeeScript and usable in e.g. Node.js.
+
+## Requirements
+
+- [Node](http://github.com/ry/node)
+- [request](http://github.com/mikeal/request)
+- [node-compress](http://github.com/waveto/node-compress)
+
+## Installation
+
+Recommended installation is using [npm](http://github.com/isaacs/npm)
+
+ npm bundle discogs
+
+## Usage
+
+ var discogs = require('discogs');
+
+ var client = discogs.discogs({api_key: 'foo4711'});
+
+ client.artist('Jay-Z', function(err, artist) {
+ console.log(artist.realname); // Shawn Corey Carter
+ });
+
+## Credits
+
+Linus G Thiel <linus@hanssonlarsson.se>
+
+## Thank you
+
+- [Discogs](http://discogs.com/) for an outstanding service and a great API
+- [Ryan Dahl](http://github.com/ry) for the awesome Node.js
+- [Jeremy Ashkenas](http://github.com/jashkenas) for the beautiful CoffeeScript
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010 Hansson & Larsson <info@hanssonlarsson.se>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
+querystring = require 'querystring'
+request = require 'request'
+compress = require 'compress'
+
+class Discogs
+ # Discogs is a simple wrapper for the [Discogs API](http://www.discogs.com/help/api)
+ #
+ # Initialize with a config object
+ constructor: (@config) ->
+ # Default format
+ @config.f ?= @config.format or 'json'
+ @params = querystring.stringify(config)
+
+ @gunzip = new compress.Gunzip()
+ @gunzip.init()
+
+ # Return a proper url with api_key and format
+ getUrl: (url) ->
+ url = "http://www.discogs.com/#{url}" if url.substr(0, 7) isnt 'http://'
+ sep = if "?" in url then "&" else "?"
+
+ "#{url}#{sep}#{@params}"
+
+ # Make a request
+ request: (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 @config.f is 'json'
+
+ next(null, body)
+ else
+ next(error)
+
+ # Use this if you have a discogs url
+ get: (url, next) ->
+ @request url, next
+
+ # Get a release
+ release: (id, next) ->
+ @request 'release/' + id,
+ Discogs.responseHandler('release', next)
+
+ # Get an artist
+ artist: (name, next) ->
+ @request 'artist/' + name,
+ Discogs.responseHandler('artist', next)
+
+ # Get a label
+ label: (name, next) ->
+ @request 'label/' + name,
+ Discogs.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'
+ @request 'search?' + querystring.stringify(type: type, q: query),
+ Discogs.responseHandler('search', next)
+
+ @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])
+
+# This is the entry point.
+#
+# Needs a [Discogs API key](https://www.discogs.com/users/login).
+#
+# Takes an object as such:
+#
+# config = {
+# api_key: 'string' # Required.
+# f: 'json|xml' # Optional, defaults to 'json'. If 'xml' consumer is expected to take care of parsing
+# }
+exports.discogs = (config) -> new Discogs(config)
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 0c16695

Please sign in to comment.