Skip to content
Browse files

Improvements, fixes and new examples

  • Loading branch information...
1 parent 88865fe commit 3e050bd30c37c81a1069694f0b647c1d0a3d3a7c @stagas committed Jun 9, 2011
Showing with 120 additions and 47 deletions.
  1. +40 −9 README.md
  2. +56 −30 beatport.js
  3. +23 −7 example.js
  4. +1 −1 package.json
View
49 README.md
@@ -13,20 +13,51 @@ Usage / Examples
var Beatport = require('beatport')
-// initialize client
-var bp = Beatport()
+// initialize and configure client
+var bp = Beatport({ perPage: 10, sortBy: 'releaseDate desc' })
// resources (i.e: featured/releases) as methods (camelCased, i.e: featuredReleases)
-bp.releases({
- facets: [ 'genreName:Trance', 'performerName:Above&Beyond' ]
-, perPage: 5
+//
+// a search for tracks by 'digweed'
+bp.search({ query: 'digweed', facets: { fieldType: [ 'track' ] } }, function(err, results, metadata) {
+ results.forEach(function(track) {
+ console.log(track.title)
+ })
+})
+
+// get tracks of genre Tech House and performers John Digweed and Nick Muir
+bp.tracks({
+ facets: {
+ genreName: 'Tech House'
+ , performerName: [ 'John Digweed', 'Nick Muir' ]
+ }
, page: 1
}, function(err, results, metadata) {
- // do something
+ results.forEach(function(track) {
+ console.log(track.title)
+ })
})
-bp.labelDetail({ id: 804 }, function(err, results, metadata) {
- // do something
+// get details of label with id 804
+bp.labelsDetail({ id: 804 }, function(err, results, metadata) {
+ results.mostPopularReleases.forEach(function(release) {
+ console.log(release.name, release.artists.names)
+ })
})
-```
+```
+
+Improving the Beatport data
+---------------------------
+In order to make the resulting Beatport data more Javascript-friendly, by default
+the client attempts to improve the data. If you don't want this behavior, just initialize
+the client with an `improve: false` option.
+
+This is what it does, so far:
+
+- Adds a .camelot field containing the Camelot system representation of the track key (ie '8B')
+- Finds remixers in the .artists field and extracts them to their own array .remixers
+- Parses the ids and names of artists and remixers and creates arrays as properties in the Object type
+of the existing arrays: .artists.names, .artists.ids, .remixers.names, .remixers.ids
+- Converts date strings to Date objects
+- Adds .title field to the tracks, with a proper representation of the track, like this: Artist Name - Track Name (MixName)
View
86 beatport.js
@@ -9,7 +9,12 @@ var agent = require('superagent')
var Beatport = module.exports = function(o) {
if (!(this instanceof Beatport)) return new Beatport(o)
-
+
+ this.options = {
+ improve: o && 'undefined' !== typeof o.improve ? o.improve : true
+ }
+ o && delete o.improve
+
this.query = {
v: '1.0'
, format: 'json'
@@ -22,18 +27,24 @@ var Beatport = module.exports = function(o) {
}
Beatport.prototype.api = function(resource, o, cb) {
+ var self = this
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)
+ if (err) return cb(err)
+ if (self.options.improve) {
+ Array.isArray(data.results) && data.results.forEach(utils.improve)
+ if ('object' === typeof data.results) {
+ ;[ 'topDownloads', 'featuredReleases', 'mostPopularReleases' ].forEach(function(k) {
+ Array.isArray(data.results[k])
+ && data.results[k].forEach(utils.improve)
+ })
+ }
}
- cb(err, data.results, data.metadata)
+ cb(null, data.results, data.metadata)
})
}
@@ -102,49 +113,64 @@ utils.camelot = function(k) {
o['G#M'] = o['AbM']
o['A#M'] = o['BbM']
- if (!o[k]) console.log(k)
+ //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)
+utils.improve = function(item) {
+ // convert to proper Date objects
+ ;[ 'releaseDate', 'publishDate', 'lastPublishDate' ].forEach(function(k) {
+ if (item[k]) item[k] = new Date(item[k])
+ })
+
+ ;[ 'genres', 'subGenres', 'artists' ].forEach(function(k) {
+ if (item[k]) {
+ item[k].names = []
+ item[k].ids = []
+ item[k].forEach(function(el) {
+ if (el.type && el.type.toLowerCase() === 'remixer') {
+ item.remixers = item.remixers || []
+ item.remixers.names = item.remixers.names || []
+ item.remixers.ids = item.remixers.ids || []
+ item.remixers.push(el)
+ item.remixers.names.push(el.name)
+ item.remixers.ids.push(el.id)
} else {
- track.remixers.push(artist)
- track.remixers.names.push(artist.name)
- track.remixers.ids.push(artist.id)
+ item[k].names.push(el.name)
+ item[k].ids.push(el.id)
}
})
- track.title = track.artists.names.join(' & ')
- + ' - '
- + track.name
- + ' (' + track.mixName + ')'
- }(item))
+ }
+ })
+
+ if (item.remixers && item.remixers.length) {
+ item.remixers.forEach(function(el) {
+ item.artists.splice(item.artists.indexOf(el), 1)
+ })
}
+
+ if (item.key) item.camelot = utils.camelot(item.key)
+
+ if (item.type === 'track')
+ item.title = item.artists.names.join(' & ')
+ + ' - '
+ + item.name
+ + ' (' + item.mixName + ')'
}
+// create prototype methods
;['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
View
30 example.js
@@ -1,17 +1,33 @@
var Beatport = require('./beatport')
-// initialize client
-var bp = Beatport()
+// initialize and configure client
+var bp = Beatport({ perPage: 10, sortBy: 'releaseDate desc' })
// resources (i.e: featured/releases) as methods (camelCased, i.e: featuredReleases)
-bp.releases({
- facets: [ 'genreName:Trance', 'performerName:Above&Beyond' ]
-, perPage: 5
+//
+// a search for tracks by 'digweed'
+bp.search({ query: 'digweed', facets: { fieldType: [ 'track' ] } }, function(err, results, metadata) {
+ results.forEach(function(track) {
+ console.log(track.title)
+ })
+})
+
+// get tracks of genre Tech House and performers John Digweed and Nick Muir
+bp.tracks({
+ facets: {
+ genreName: 'Tech House'
+ , performerName: [ 'John Digweed', 'Nick Muir' ]
+ }
, page: 1
}, function(err, results, metadata) {
- console.log(results)
+ results.forEach(function(track) {
+ console.log(track.title)
+ })
})
+// get details of label with id 804
bp.labelsDetail({ id: 804 }, function(err, results, metadata) {
- console.log(results)
+ results.mostPopularReleases.forEach(function(release) {
+ console.log(release.name, release.artists.names)
+ })
})
View
2 package.json
@@ -1,6 +1,6 @@
{ "name" : "beatport"
, "description" : "Beatport API client for Node.js"
-, "version" : "0.0.1"
+, "version" : "0.0.2"
, "homepage" : "https://github.com/stagas/node-beatport"
, "author" : "George Stagas <gstagas@gmail.com> (http://stagas.com/)"
, "repository" :

0 comments on commit 3e050bd

Please sign in to comment.
Something went wrong with that request. Please try again.