Commits

Edoardo Tosca  committed 68b7a5c

added more tests and refactoring. added first sample logging

  • Participants
  • Parent commits 6a3df36

Comments (0)

Files changed (5)

File log4j.properties

+log4j.rootLogger=DEBUG, A1
+log4j.appender.A1=org.apache.log4j.ConsoleAppender
+log4j.appender.A1.layout=org.apache.log4j.PatternLayout
+log4j.appender.A1.layout.ConversionPattern=%d %-5p %c: %m%n
   :dependencies [[org.clojure/clojure "1.5.1"]
                  [clj-http "0.7.0"]
                  [org.clojure/data.json "0.2.1"]
-                 [digest "1.4.3"]])
+                 [digest "1.4.3"]
+                 [org.clojure/tools.logging "0.2.6"]
+                 [log4j/log4j "1.2.16" :exclusions [javax.mail/mail
+                                                    javax.jms/jms
+                                                    com.sun.jdmk/jmxtools
+                                                    com.sun.jmx/jmxri]]])

File src/clj_grooveshark/artist.clj

 (ns clj-grooveshark.artist
   (:use clj-grooveshark.core)
+  (:use [clojure.tools.logging :only (error debug)])
   )
 
+
 (defn get-artist-search-results
   "Search for an artist given a query. A limit and a page number can be specified optionally"
   [query & [limit page]]
     )
   )
 
+(defn handle
+  [response callback]
+  (if (contains? response :errors )
+    (:errors response)
+    (do
+      (debug response)
+      (callback response)
+      )
+    )
+
+  )
+
+
 (defn get-artist-albums
   "Retrieve albums of an artist, given an artist id"
   [artist-id]
   (let [response (execute (a-query "getArtistAlbums" {:artistID artist-id}))]
-    (if (contains? response :errors)
-      (:errors response)
-      (:albums (:result response))
-      )
+    (handle response (fn [x] (:albums (:result response))))
     )
   )
 
 (defn get-artist-popular-songs
   "Retrieves artists most popular songs"
   [artist-id]
-  (execute (a-query "getArtistPopularSongs", {:artistID artist-id}))
+  (let [response (execute (a-query "getArtistPopularSongs", {:artistID artist-id}))]
+    (handle response (fn [x] (:songs (:result x))))
+    )
   )
 
 (defn get-artist-verified-albums
 (defn get-artists-info
   "Returns information for the given list of artist ids."
   [artist-id & more]
-  (execute (a-query "getArtistsInfo" {:artistIDs  (concat (list artist-id) more)}))
+  (execute (a-query "getArtistsInfo" {:artistIDs (concat (list artist-id) more)}))
   )
 
 (defn get-artists-info2
   "Returns information for the given list of artist ids."
   [artist-id & more]
-  (exec plain (a-query2 "getArtistsInfo" {:artistIDs  (concat (list artist-id) more)}))
+  (execute (a-query2 "getArtistsInfo" {:artistIDs (concat (list artist-id) more)}))
   )
 
 

File src/clj_grooveshark/core.clj

   (:use clj-grooveshark.configuration)
   )
 
+;(org.apache.log4j.BasicConfigurator/configure)
+
 (defn a-query
   ([method] (json/write-str {:method method :header {:wsKey (:key configuration)}}))
   ([method parameters] (json/write-str {:method method :parameters parameters :header {:wsKey (:key configuration)}}))
   (client/get (:url configuration) {:body query} {:as :json})
   )
 
-(defn execute [query]
-;  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
-  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
-  )
+;(defn execute [query]
+;  ;  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
+;  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword)
+;  )
 
 (defn execute-secure [query]
-;  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
-  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
+  ;  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
+  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword)
   )
 
 
-(defn exec
-  [url query]
-  (json/read-str (:body (client/get (str url "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
-  )
-
 (def secure
   (:secure-url configuration)
   )
   )
 
 
+(defn- do-execute
+  [url query]
+  (json/read-str (:body (client/get (str url "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword)
+  )
+
+(defn execute
+  ([query] (do-execute plain query))
+  ([url query] (do-execute url query))
+  ;  (json/read-str (:body (client/get (str url "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
+  )
+
+
+
+
+
 
 
 

File test/clj_grooveshark/artist_test.clj

 (deftest should-get-artists-info
   (let [query (stringify {"result" {"artists" [{"ArtistID" 1261,"ArtistName" "Mogwai","IsVerified" "1"}]}})]
     (with-redefs [client/get (fn [url body json] query)]
-      (is (= {:result {:artists [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified "1"}]}} (get-artists-info 1261)))
+      (is (= {:result {:artists [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified "1"}]}} (get-artists-info2 1261)))
       )
     )
   )
 
 
 
+(deftest should-return-albums-for-an-artists
+  (let [response (stringify {:header {:hostname "RHL109"}, :result {:pager {:numPages 342, :hasPrevPage false, :hasNextPage true}, :albums [{:AlbumID 1550, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified false, :AlbumName "Unknown Album"}]}})]
+    (with-redefs [client/get (fn [url body json] response)]
+      (is (= [{:AlbumID 1550, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified false, :AlbumName "Unknown Album"}] (get-artist-albums 1261)))
+      )
+    )
+  )
+
+(deftest should-return-a-list-of-popular-songs-for-an-artist
+  (let [response (stringify {:header {:hostname "RHL081"}, :result {:songs [{:AlbumID 8790822, :ArtistID 12, :SongID 38508082, :AlbumName "Disco 2", :ArtistName "Joaquin Sabina", :SongName "Y Sin Embargo", :Flags 0, :IsVerified false, :Popularity "1314002909", :IsLowBitrateAvailable false, :CoverArtFilename nil}]}})]
+    (with-redefs [client/get (fn [url body json] response)]
+      (println (get-artist-popular-songs 1261))
+      (is (= [{:AlbumID 8790822, :ArtistID 12, :SongID 38508082, :AlbumName "Disco 2", :ArtistName "Joaquin Sabina", :SongName "Y Sin Embargo", :Flags 0, :IsVerified false, :Popularity "1314002909", :IsLowBitrateAvailable false, :CoverArtFilename nil}] (get-artist-popular-songs 1261)))
+      )
+    )
+  )
+
+(deftest should-return-an-empty-list-when-artists-id-does-not-exists
+  (let [response (stringify {"header" {"hostname" "RHL110"}, "result" {"songs" []}})]
+    (with-redefs [client/get (fn [url body json] response)]
+      (is (empty? (get-artist-popular-songs "11111")))
+      )
+    )
+  )
+
+
+
+