Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 9f4d7297608e015655cd3fb0ea73feabd54fac95 @stagas committed Jun 6, 2011
@@ -0,0 +1,19 @@
+Copyright (c) 2011 George Stagakis
+
+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,32 @@
+node-beatport
+=============
+Beatport API client - resources as methods, also attempts to improve the
+resulting data to make them more useful.
+
+Installation
+------------
+ npm install beatport
+
+Usage / Examples
+----------------
+```javascript
+
+var Beatport = require('beatport')
+
+// initialize client
+var bp = Beatport()
+
+// resources (i.e: featured/releases) as methods (camelCased, i.e: featuredReleases)
+bp.releases({
+ facets: [ 'genreName:Trance', 'performerName:Above&Beyond' ]
+, perPage: 5
+, page: 1
+}, function(err, results, metadata) {
+ // do something
+})
+
+bp.labelDetail({ id: 804 }, function(err, results, metadata) {
+ // do something
+})
+
+```
@@ -0,0 +1,153 @@
+//
+// beatport api client
+// by stagas
+//
+// mit licenced
+//
+
+var agent = require('superagent')
+
+var Beatport = module.exports = function(o) {
+ if (!(this instanceof Beatport)) return new Beatport(o)
+
+ this.query = {
+ v: '1.0'
+ , format: 'json'
+ , perPage: 10
+ , 'facets[]': []
+ }
+ for (var k in o) {
+ this.query[k] = o[k]
+ }
+}
+
+Beatport.prototype.api = function(resource, o, cb) {
+ for (var k in this.query) {
+ if (!o[k]) o[k] = this.query[k]
+ }
+ o['facets[]'] = o['facets[]'].concat(utils.facets(o.facets))
+ delete o.facets
+ agent.get('http://api.beatport.com' + resource, o, function(err, res, data) {
+ Array.isArray(data.results) && data.results.forEach(utils.improveTrack)
+ if ('object' === typeof data.results) {
+ Array.isArray(data.results.topDownloads)
+ && data.results.topDownloads.forEach(utils.improveTrack)
+ }
+ cb(err, data.results, data.metadata)
+ })
+}
+
+// utils
+var utils = {}
+
+utils.get = function(a, b, cb) {
+ b = b || {}
+ agent.get(base + a, b, function(err, res, data) {
+ cb(err, data)
+ })
+}
+
+utils.facets = function(o) {
+ var facets = []
+ for (var k in o) {
+ if (Array.isArray(o[k])) {
+ o[k].forEach(function(el) {
+ facets.push(k + ':' + el)
+ })
+ } else {
+ facets.push(k + ':' + o[k])
+ }
+ }
+ return facets
+}
+
+utils.camelCase = function(s) {
+ ;['/', '-'].forEach(function(c) {
+ if (~s.indexOf(c))
+ s = s.split(c)[0] + s.split(c)[1][0].toUpperCase() + s.split(c)[1].slice(1)
+ })
+ return s
+}
+
+utils.camelot = function(k) {
+ var n = 0, o = {}
+
+ k = k.standard.letter
+ + (k.standard.flat ? 'b' : k.standard.sharp ? '#' : '')
+ + (k.standard.chord === 'minor' ? 'm' : 'M')
+
+ ;['Abm', 'Ebm', 'Bbm', 'Fm', 'Cm', 'Gm'
+ , 'Dm', 'Am', 'Em', 'Bm', 'F#m', 'Dbm'
+ ].map(function(el) {
+ n++
+ return (o[el] = n + 'A')
+ })
+
+ n = 0
+
+ ;['BM', 'F#M', 'DbM', 'AbM', 'EbM', 'BbM'
+ , 'FM', 'CM', 'GM', 'DM', 'AM', 'EM'
+ ].map(function(el) {
+ n++
+ return (o[el] = n + 'B')
+ })
+
+ o['C#m'] = o['Dbm']
+ o['D#m'] = o['Ebm']
+ o['G#m'] = o['Abm']
+ o['A#m'] = o['Bbm']
+
+ o['C#M'] = o['DbM']
+ o['D#M'] = o['EbM']
+ o['G#M'] = o['AbM']
+ o['A#M'] = o['BbM']
+
+ if (!o[k]) console.log(k)
+
+ return o[k]
+}
+
+utils.improveTrack = function(item) {
+ if (item.key) {
+ ;(function(track) {
+ track.artists.names = []
+ track.remixers = []
+ track.remixers.names = []
+ track.remixers.ids = []
+ track.artists.ids = []
+ track.camelot = utils.camelot(track.key)
+ track.artists.forEach(function(artist) {
+ if (artist.type.toLowerCase() === 'artist') {
+ track.artists.names.push(artist.name)
+ track.artists.ids.push(artist.id)
+ } else {
+ track.remixers.push(artist)
+ track.remixers.names.push(artist.name)
+ track.remixers.ids.push(artist.id)
+ }
+ })
+ track.title = track.artists.names.join(' & ')
+ + ' - '
+ + track.name
+ + ' (' + track.mixName + ')'
+ }(item))
+ }
+}
+
+;['genres', 'tracks', 'releases', 'charts', 'artists', 'labels'
+, 'releases/detail', 'charts/detail', 'artists/detail', 'labels/detail'
+, 'genres/detail', 'genres/overview', 'home'
+, 'featured/releases', 'featured/charts', 'featured/labels'
+, 'slideshows/header', 'slideshows/feature', 'slideshows/small'
+, 'most-popular', 'search'].forEach(function(k) {
+ var key = k, v = '1.0'
+
+ // api v2.0
+ if (~['search'].indexOf(k)) v = '2.0'
+
+ key = utils.camelCase(k)
+ Beatport.prototype[key] = function(o, cb) {
+ o.v = o.v || v
+ this.api('/catalog/' + k, o, cb)
+ }
+})
@@ -0,0 +1,17 @@
+var Beatport = require('./beatport')
+
+// initialize client
+var bp = Beatport()
+
+// resources (i.e: featured/releases) as methods (camelCased, i.e: featuredReleases)
+bp.releases({
+ facets: [ 'genreName:Trance', 'performerName:Above&Beyond' ]
+, perPage: 5
+, page: 1
+}, function(err, results, metadata) {
+ console.log(results)
+})
+
+bp.labelsDetail({ id: 804 }, function(err, results, metadata) {
+ console.log(results)
+})
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+*.sock
@@ -0,0 +1,9 @@
+[submodule "support/should"]
+ path = support/should
+ url = git://github.com/visionmedia/should.js.git
+[submodule "support/express"]
+ path = support/express
+ url = git://github.com/visionmedia/express.git
+[submodule "support/connect"]
+ path = support/connect
+ url = git://github.com/senchalabs/connect.git
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
@@ -0,0 +1,5 @@
+
+test:
+ @./test/runner
+
+.PHONY: test
@@ -0,0 +1,114 @@
+
+# SuperAgent
+
+ Progressive high-level request HTTP client for node.
+
+ Mikeal's library is great, but when you want something in between node's low level API, and a high level API, things start to fall apart. The aim of this library is to provide a similar high-level interface, as well as a progressive one.
+
+ __WARNING__: work in progress
+
+## Installation
+
+ $ npm install superagent
+
+## TODO
+
+ - progressive multipart support (req / res)
+ - schema relative redirects
+ - SSL
+
+## Examples
+
+### GET
+
+ Below is a simple __GET__ request, with buffered body available at `res.body` and `body`. By default when passing a callback there is no need to listen on "end", as the callback is invoked when the response is complete.
+
+ var agent = require('superagent');
+
+ agent.get('http://google.com', function(err, res, body){
+ console.log(res.statusCode);
+ console.log(body);
+ });
+
+### GET Query String
+
+ The second object passed becomes the querystring:
+
+ agent.get('http://google.com', { search: 'foobar' }, function(err, res, body){
+ // whatever
+ });
+
+### Redirects
+
+ The default maximum redirects defaults to `5`, however we can alter this with `.redirects(n)` or `.maxRedirects(n)`.
+
+ var req = agent.get('http://google.com', function(err, res, body){
+ console.log(res.statusCode);
+ console.log(body);
+ });
+
+ req.on('redirect', function(location){
+ console.log('redirecting to %s', location);
+ });
+
+### Buffered Responses
+
+ By default data is not buffered, and you must listen on response "data" events like you normally would. SuperAgent provides the `.buffer()` and `.parse()` methods, which are equivalent, and check `Content-Type` header and associate a parser with the response.
+
+ For example the following will automatically parse/buffer `text/*`, `application/json`, and `application/x-www-form-urlencoded` responses:
+
+ agent
+ .get('http://localhost:3000')
+ .parse()
+ .on('response', function(res){
+ res.on('end', function(){
+ console.log(res.body);
+ });
+ }).end();
+
+ Sometimes it is not ideal to simply halt processing until the data is buffered / parsed, which is why SuperAgent gives you the choice of this API, or the callback API.
+
+### POSTing JSON
+
+ Posting some JSON is easy, the `.json()` method writes a jsonified version of the object passed, providing a content-length and content-type.
+
+ agent
+ .post('http://localhost:3000')
+ .json({ foo: 'bar' });
+
+### POSTing Form Data
+
+ The same can be done for `application/x-www-form-urlencoded` data, using the `.form()` method.
+
+ agent
+ .post('http://localhost:3000')
+ .form({ foo: 'bar' });
+
+## Running Tests
+
+ $ make test
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+
+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,2 @@
+
+module.exports = require('./lib/superagent');
Oops, something went wrong. Retry.

0 comments on commit 9f4d729

Please sign in to comment.