Permalink
Browse files

Initial commit.

  • Loading branch information...
kingcons committed Oct 10, 2011
0 parents commit e499be52dd8e1b7d17c35e03a916230f126d2403
Showing with 115 additions and 0 deletions.
  1. +1 −0 README.txt
  2. +27 −0 cl-echonest.asd
  3. +4 −0 gen-docs.sh
  4. +22 −0 src/config.lisp
  5. +22 −0 src/errors.lisp
  6. +6 −0 src/package.lisp
  7. +25 −0 src/utils.lisp
  8. +3 −0 tests/package.lisp
  9. +5 −0 tests/tests.lisp
@@ -0,0 +1 @@
+This is the stub README.txt for the "cl-echonest" project.
@@ -0,0 +1,27 @@
+(defsystem #:cl-echonest
+ :name "cl-echonest"
+ :description "A binding to the echonest.com API"
+ :version "0.1"
+ :license "BSD"
+ :author "Brit Butler <redline6561@gmail.com>"
+ :depends-on (#:drakma #:md5 #:st-json #:flexi-streams)
+ :components ((:module src
+ :serial t
+ :components ((:file "package")
+ (:file "config")
+ (:file "errors")
+ (:file "utils"))))
+ :in-order-to ((test-op (load-op cl-echonest-tests)))
+ :perform (test-op :after (op c)
+ (funcall (intern "RUN!" :cl-echonest-tests))))
+
+(defsystem #:cl-echonest-tests
+ :depends-on (cl-echonest fiveam)
+ :pathname "tests/"
+ :serial t
+ :components ((:file "package")
+ (:file "tests")))
+
+(defmethod operation-done-p ((op test-op)
+ (c (eql (find-system :cl-echonest))))
+ (values nil))
@@ -0,0 +1,4 @@
+#!/bin/sh
+sbcl --eval "(ql:quickload '(cl-echonest sb-introspect cl-api))" \
+ --eval "(cl-api:api-gen :cl-echonest \"docs/index.html\")" \
+ --eval "(progn (terpri) (sb-ext:quit))"
@@ -0,0 +1,22 @@
+(in-package #:cl-echonest)
+
+;; All queryparams should be utf-8 encoded.
+(setf drakma:*drakma-default-external-format* :utf-8)
+;; tell Drakma to handle JSON as strings
+(pushnew '("application" . "json") drakma:*text-content-types*
+ :test (lambda (x y)
+ (and (equalp (car x) (car y))
+ (equalp (cdr x) (cdr y)))))
+
+;;;; Echonest API Configuration
+(defvar *api-version* "4"
+ "The supported echonest API version.")
+
+(defvar *api-url*
+ (format nil "http://developer.echonest.com/api/v~d" *api-version*)
+ "The URL for echonest API calls.")
+
+(defparameter *api-key* ""
+ "A valid echonest API key.")
+
+;; non-commercial users should expect a rate limit of 120 calls per minute
@@ -0,0 +1,22 @@
+(in-package :cl-echonest)
+
+(define-condition echonest-error (error)
+ ((message :initarg :message :reader message))
+ (:report (lambda (condition stream)
+ (format stream "~A" (message condition))))
+ (:documentation "The base condition for all errors in ROMREADER."))
+
+(defvar *response-codes*
+ '((-1 . "Unknown Error")
+ (0 . "Success")
+ (1 . "Missing/ Invalid API Key")
+ (2 . "This API key is not allowed to call this method")
+ (3 . "Rate Limit Exceeded")
+ (4 . "Missing Parameter")
+ (5 . "Invalid Parameter"))
+ "A list of echonest response codes. Duh.
+From http://api.echonest.com/docs/v4/index.html#response-codes")
+
+(defun response-message (respcode)
+ "Retrieve the message corresponding to the number RESPCODE."
+ (rest (nth (1- respcode) *response-codes*)))
@@ -0,0 +1,6 @@
+(defpackage #:cl-echonest
+ (:documentation "Homepage: <a href=\"http://github.com/redline6561/cl-echonest\">Github</a>")
+ (:use #:cl)
+ (:import-from #:flexi-streams
+ #:string-to-octets))
+
@@ -0,0 +1,25 @@
+(in-package :cl-echonest)
+
+(defun format-md5 (byte-vector)
+ "Given a BYTE-VECTOR, coerce it to a list and print it in hexadecimal."
+ (format nil "~(~{~2,'0X~}~)" (coerce byte-vector 'list)))
+
+(defmethod md5sum ((string string))
+ "Creates an MD5 byte-array of STRING and prints it as lower-case hexadecimal."
+ (let ((string (string-to-octets string :external-format :utf-8)))
+ (format-md5 (md5:md5sum-sequence string))))
+
+(defmethod md5sum ((file pathname))
+ "Creates an MD5 byte-array of FILE and prints it as lower-case hexadecimal."
+ (format-md5 (md5:md5sum-file file)))
+
+(defun echonest-call (type name params &key (method :get) (format "json"))
+ (let ((url (format nil "~a/~a/~a" *api-url* type name)))
+ (multiple-value-bind (response status headers)
+ (drakma:http-request url :method method
+ :parameters (append `(("format" . ,format)
+ ("api_key" . ,*api-key*))
+ params))
+ (if (= status 200)
+ response
+ (error 'echonest-error :message response)))))
@@ -0,0 +1,3 @@
+(defpackage :cl-echonest-tests
+ (:use :cl :cl-echonest :fiveam)
+ (:export #:run!))
@@ -0,0 +1,5 @@
+(in-package :cl-echonest-tests)
+
+(def-suite :cl-echonest)
+(in-suite :cl-echonest)
+

0 comments on commit e499be5

Please sign in to comment.