Browse Source

Added album.getinfo API

Abhinav Sarkar 10 years ago
parent
commit
b21ec6a771
1 changed files with 92 additions and 12 deletions
  1. 92
    12
      src/clj_lastfm/core.clj

+ 92
- 12
src/clj_lastfm/core.clj View File

@@ -3,10 +3,12 @@
3 3
            (java.text SimpleDateFormat)
4 4
            (java.util TimeZone))
5 5
   (:use [clj-lastfm.filecache]
6
+        [clojure.contrib.core :only (-?> -?>>)]
6 7
         [clojure.contrib.def :only (defvar-)]
7 8
         [clojure.contrib.json :only (read-json)]
8 9
         [clojure.contrib.logging]
9
-        [clojure.contrib.math :only (ceil)]))
10
+        [clojure.contrib.math :only (ceil)]
11
+        [clojure.string :only (blank? trim)]))
10 12
 
11 13
 ;;;;;;;;;; Basic ;;;;;;;;;;
12 14
 
@@ -45,25 +47,31 @@
45 47
 
46 48
 (defn- str-1? [n] (-> n safe-parse-int (= 1)))
47 49
 
48
-(defn- blank? [s] (every? #(Character/isWhitespace #^Character %) s))
49
-
50 50
 (defn- struct? [obj] (instance? clojure.lang.PersistentStructMap obj))
51 51
 
52
-(def #^{:private true :tag SimpleDateFormat} sdftz
52
+(def #^{:private true :tag SimpleDateFormat} full-date-format
53 53
   (doto (SimpleDateFormat. "EEE, dd MMMM yyyy HH:mm:ss +0000")
54 54
     (.setTimeZone (TimeZone/getTimeZone "GMT"))))
55 55
 
56
-(def #^{:private true :tag SimpleDateFormat} sdf
56
+(def #^{:private true :tag SimpleDateFormat} date-format-wo-tz
57 57
   (doto (SimpleDateFormat. "EEE, dd MMMM yyyy HH:mm:ss")
58 58
     (.setTimeZone (TimeZone/getTimeZone "GMT"))))
59 59
 
60
+(def #^{:private true :tag SimpleDateFormat} short-date-format-wo-tz
61
+  (doto (SimpleDateFormat. "dd MMM yyyy, HH:mm")
62
+    (.setTimeZone (TimeZone/getTimeZone "GMT"))))
63
+
60 64
 (defn- parse-date [date-str]
61 65
   (if (some #(% date-str) [nil? blank?])
62 66
     nil
63
-    (try
64
-      (.parse sdftz date-str)
65
-      (catch java.text.ParseException e
66
-        (.parse sdf date-str)))))
67
+    (let [clean-date-str (trim date-str)]
68
+      (try
69
+        (.parse full-date-format clean-date-str)
70
+        (catch java.text.ParseException e
71
+          (try
72
+            (.parse date-format-wo-tz clean-date-str)
73
+            (catch java.text.ParseException e
74
+              (.parse short-date-format-wo-tz clean-date-str))))))))
67 75
 
68 76
 (defn- remove-nil-values [m]
69 77
   (apply hash-map (apply concat (filter #(-> % fnext nil? not) m))))
@@ -193,7 +201,8 @@
193 201
 
194 202
 (defrecord Artist [name url mbid streamable listeners playcount bio])
195 203
 
196
-(defrecord Album [name id url mbid artist playcount])
204
+(defrecord Album
205
+  [name id url mbid artist listeners playcount release-date wiki])
197 206
 
198 207
 (defrecord Track
199 208
   [name url mbid artist playcount listeners streamable])
@@ -573,7 +582,75 @@
573 582
 
574 583
 ;;;;;;;;;; Album ;;;;;;;;;;
575 584
 
576
-(defvar- album-from-map (partial record-from-map Album 6))
585
+(defvar- album-from-map (partial record-from-map Album 9))
586
+
587
+(defn- parse-album [data]
588
+  (merge
589
+    (Album.
590
+      (:name data)
591
+      (-> data :id safe-parse-int)
592
+      (:url data)
593
+      (:mbid data)
594
+      (-> data :artist artist-from-name)
595
+      (-> data :listeners safe-parse-int)
596
+      (-> data :playcount safe-parse-int)
597
+      (-> data :releasedate parse-date)
598
+      (-?> data :wiki parse-bio))
599
+    {:user-playcount
600
+      (-?> data :userplaycount safe-parse-int)}))
601
+
602
+;;;;;;;;;; album.getinfo ;;;;;;;;;;
603
+
604
+(defn- parse-album-getinfo [data-fn]
605
+  (let [data (data-fn)]
606
+    (do
607
+      (debug (str "parse-album-getinfo: " data))
608
+      (-> data :album parse-album))))
609
+
610
+(defvar- get-album
611
+  (create-get-obj-fn {:method "album.getinfo"} parse-album-getinfo))
612
+
613
+(defmulti album-info
614
+  (fn [arg1 & rest-args]
615
+    (cond
616
+      (instance? Album arg1) :album
617
+      (mbid? arg1) :mbid
618
+      (and (string? arg1)
619
+           (-?>> rest-args first (instance? Artist))) :album-name-artist
620
+      :else :album-name-artist-name)))
621
+
622
+(defmethod album-info :album
623
+  ([album]
624
+    (album-info album nil nil))
625
+  ([album username]
626
+    (album-info album username nil))
627
+  ([album username lang]
628
+    (album-info (-> album :name) (-> album :artist) username lang)))
629
+
630
+(defmethod album-info :mbid
631
+  ([mbid]
632
+    (album-info mbid nil nil))
633
+  ([mbid username]
634
+    (album-info mbid username nil))
635
+  ([mbid username lang]
636
+    (get-album {:mbid mbid :username username :lang lang})))
637
+
638
+(defmethod album-info :album-name-artist
639
+  ([album-name artist]
640
+    (album-info album-name artist nil nil))
641
+  ([album-name artist username]
642
+    (album-info album-name artist username nil))
643
+  ([album-name artist username lang]
644
+    (album-info album-name (:name artist) username lang)))
645
+
646
+(defmethod album-info :album-name-artist-name
647
+  ([album-name artist-name]
648
+    (album-info album-name artist-name nil nil))
649
+  ([album-name artist-name username]
650
+    (album-info album-name artist-name username nil))
651
+  ([album-name artist-name username lang]
652
+    (get-album
653
+      {:album album-name :artist artist-name :username username :lang lang})))
577 654
 
578 655
 ;;;;;;;;;; album.gettoptags ;;;;;;;;;;
579 656
 
@@ -620,8 +697,11 @@
620 697
   (create-paged-get-obj-fn
621 698
     {:method "album.search"} parse-album-search))
622 699
 
623
-(defn album-search [album-name]
700
+(defn album-search
701
+  ([album-name]
624 702
     (get-album-search {:album album-name}))
703
+  ([album-name limit]
704
+    (get-album-search {:album album-name :limit limit})))
625 705
 
626 706
 ;;;;;;;;;; Tag ;;;;;;;;;;
627 707
 

Loading…
Cancel
Save