Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
227 lines (215 sloc) 9.13 KB
request = require 'request'
API = 'http://api.soundcloud.com'
exports.Client = class Client
constructor: (@client_id, @format='json')->
get: (resource, params, next) ->
data =
url: "#{API}/#{resource}.#{@format}"
method: 'GET'
qs:
client_id: @client_id
json: params
request data, (err, response, body) ->
next err, body
parse_args: (args) ->
ids = []
filters = {}
next = null
for arg in args
switch typeof arg
when 'string' then ids.push arg
when 'number' then ids.push arg
when 'object' then filters = arg
when 'function' then next = arg
return [ids[0], ids[1], filters, next]
# Users
#
# Properties
# id, permalink, username, uri, permalink_url, avatar_url, country,
# full_name, city, description, discogs-name, myspace-name, website,
# website-title, online, track_count, playlist_count, followers_count,
# followings_count, public_favorites_count, avatar_date
#
# Filters
# q
#
# Resources
# GET /users
# GET /users/{id} a user
# GET /users/{id}/tracks list of tracks of the user
# GET /users/{id}/playlists list of playlists (sets) of the user
# GET /users/{id}/followings a users followings
# GET /users/{id}/followings/{id2} a user who is followed by the user
# GET /users/{id}/followers a users followers
# GET /users/{id}/followers/{id2} user who is following the user
# GET /users/{id}/comments list of comments from this user
# GET /users/{id}/favorites users favorites
# GET /users/{id}/favorites/{id2} track favorited by the user
# GET /users/{id}/groups list of joined groups
# GET /users/{id}/web-profiles list of web profiles
users: ->
[id, id2, filters, next] = @parse_args arguments
@get "users", filters, next if not id
@get "users/#{id}", filters, next if id
users_tracks: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/tracks", filters, next
users_playlists: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/playlists", filters, next
users_followings: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/followings", filters, next if not id2
@get "users/#{id}/followings/#{id2}", filters, next if id2
users_followers: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/followers", filters, next if not id2
@get "users/#{id}/followers/#{id2}", filters, next if id2
users_comments: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/comments", filters, next
users_favorites: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/favorites", filters, next
users_groups: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/groups", filters, next
users_web_profiles: ->
[id, id2, filters, next] = @parse_args arguments
@get "users/#{id}/web-profiles", filters, next
# Tracks
#
# Properties
# id, created_at, user_id, user, title, permalink, permalink_url, uri,
# sharing, purchase_url, artwork_url, description, label, duration, genre,
# shared_to_count, tag_list, label_id, label_name, license, release,
# release_day, release_month, release_year, streamable, downloadable, state,
# track_type, waveform_url, download_url, stream_url, video_url, bpm,
# commentable, isrc, key_signature, comment_count, download_count,
# playback_count, favoritings_count, original_format, original_content_size,
# created_with, asset_data, artwork_data, user_favorite
#
# Filters
# q, tags, filter, license, order, bpm[from], bpm[to], duration[from],
# duration[to], created_at[from], created_at[to], ids, genres, types
#
# Resources
# GET /tracks/{id} a track
# GET /tracks/{id}/comments comments for the track
# GET /tracks/{id}/comments/{comment-id} a comment for the track
# GET /tracks/{id}/favoriters users who favorited the track
# GET /tracks/{id}/favoriters/{user-id} a user who has favorited to the track
# GET /tracks/{id}/shared-to/emails email addresses who are invited to the track
# GET /tracks/{id}/shared-to/users users who have access to the track
# GET /tracks/{id}/secret-token secret token of the track
tracks: ->
[id, id2, filters, next] = @parse_args arguments
@get "tracks", filters, next if not id
@get "tracks/#{id}", filters, next if id
tracks_comments: ->
[id, id2, filters, next] = @parse_args arguments
@get "tracks/#{id}/comments", filters, next if not id2
@get "tracks/#{id}/comments/#{id2}", filters, next if id2
tracks_favoriters: ->
[id, id2, filters, next] = @parse_args arguments
@get "tracks/#{id}/favoriters", filters, next if not id2
@get "tracks/#{id}/favoriters/#{id2}", filters, next if id2
tracks_secret_token: ->
[id, id2, filters, next] = @parse_args arguments
@get "tracks/#{id}/secret-token", filters, next
tracks_shared_to_users: ->
[id, id2, filters, next] = @parse_args arguments
@get "tracks/#{id}/shared-to/users", filters, next
tracks_shared_to_emails: ->
[id, id2, filters, next] = @parse_args arguments
@get "tracks/#{id}/shared-to/emails", filters, next
# Playlists
#
# Properties
#
# id, created_at, user_id, user, title, permalink, permalink_url, uri,
# sharing, purchase_url, artwork_url, description, label, duration, genre,
# shared_to_count, tag_list, label_id, label_name, license, release,
# release_day, release_month, release_year, streamable, downloadable, iean,
# playlist_type
#
# Filters
# q, filter, license
#
# Resources
# GET /playlists/{id} a playlist
# GET /playlists/{id}/shared-to/emails email addresses who are invited to the playlist
# GET /playlists/{id}/shared-to/users users who have access to the playlist
# GET /playlists/{id}/secret-token secret token of the playlist
playlists: ->
[id, id2, filters, next] = @parse_args arguments
@get "playlists", filters, next if not id
@get "playlists/#{id}", filters, next if id
playlists_shared_to_emails: ->
[id, id2, filters, next] = @parse_args arguments
@get "playlists/#{id}/shared-to/emails", filters, next
playlists_shared_to_users: ->
[id, id2, filters, next] = @parse_args arguments
@get "playlists/#{id}/shared-to/users", filters, next
playlists_secret_token: ->
[id, id2, filters, next] = @parse_args arguments
@get "playlists/#{id}/secret-token", filters, next
# Groups
#
# Properties
# id, uri, created_at, permalink, permalink_url, artwork_url, name,
# description, short_description, creator
#
# Filters
# q
#
# Resources
# GET /groups/{id} a group
# GET /groups/{id}/moderators list of users who moderate the group
# GET /groups/{id}/members list of users who joined the group
# GET /groups/{id}/contributors list of users who contributed a track to the group
# GET /groups/{id}/users list of users who contributed to, joined or moderate the group
# GET /groups/{id}/tracks list of contributed and approved tracks
# GET /groups/{id}/pending_tracks list of contributed but not approved tracks (for moderators)
# GET /groups/{id}/pending_tracks/{id} a contributed but not approved track (for moderators)
# GET /groups/{id}/contributions list of contributed tracks (for moderators)
# GET /groups/{id}/contributions/{id} a contributed track (for moderators)
groups: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups", filters, next if not id
@get "groups/#{id}", filters, next if id
groups_moderators: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/moderators", filters, next
groups_members: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/members", filters, next
groups_contributors: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/contributors", filters, next
groups_users: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/users", filters, next
groups_tracks: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/tracks", filters, next
groups_pending_tracks: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/pending_tracks", filters, next if not id2
@get "groups/#{id}/pending_tracks/#{id2}", filters, next if id2
groups_contributions: ->
[id, id2, filters, next] = @parse_args arguments
@get "groups/#{id}/contributions", filters, next if not id2
@get "groups/#{id}/contributions/#{id2}", filters, next if id2
# Comments
#
# Properties
# id, uri, created_at, body, timestamp, user_id, user, track_id
#
# Filters
#
# Resources
# GET /comments/{id} a comment
comments: ->
[id, id2, filters, next] = @parse_args arguments
@get "comments/#{id}", {}, next