Browse Source

moved to Clojure 1.2. changed structs to records

Abhinav Sarkar 10 years ago
parent
commit
6a8599fc09
4 changed files with 105 additions and 93 deletions
  1. 2
    2
      project.clj
  2. 94
    89
      src/clj_lastfm/core.clj
  3. 8
    1
      src/clj_lastfm/examples.clj
  4. 1
    1
      src/clj_lastfm/filecache.clj

+ 2
- 2
project.clj View File

@@ -1,7 +1,7 @@
1 1
 (defproject clj-lastfm "1.0.0-SNAPSHOT"
2 2
   :description "Clojure interface to last.fm API"
3
-  :dependencies [[org.clojure/clojure "1.1.0"]
4
-                 [org.clojure/clojure-contrib "1.1.0"]
3
+  :dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
4
+                 [org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
5 5
                  [log4j "1.2.15" :exclusions [javax.mail/mail
6 6
                                               javax.jms/jms
7 7
                                               com.sun.jdmk/jmxtools

+ 94
- 89
src/clj_lastfm/core.clj View File

@@ -3,11 +3,10 @@
3 3
            (java.text SimpleDateFormat)
4 4
            (java.util TimeZone))
5 5
   (:use [clj-lastfm.filecache]
6
-        [clojure.contrib.def :only (defstruct- defvar-)]
7
-        [clojure.contrib.json.read :only (read-json)]
6
+        [clojure.contrib.def :only (defvar-)]
7
+        [clojure.contrib.json :only (read-json)]
8 8
         [clojure.contrib.logging]
9
-        [clojure.contrib.math :only (ceil)]
10
-        [clojure.walk :only (keywordize-keys)]))
9
+        [clojure.contrib.math :only (ceil)]))
11 10
 
12 11
 ;;;;;;;;;; Basic ;;;;;;;;;;
13 12
 
@@ -101,7 +100,7 @@
101 100
 
102 101
 (defn- get-data [params]
103 102
   (let [url (create-url params)
104
-        data (-> url get-url read-json keywordize-keys)]
103
+        data (-> url get-url read-json)]
105 104
     (if (-> data :error nil?)
106 105
       data
107 106
       (throw (IllegalArgumentException. (-> data :message str))))))
@@ -177,35 +176,52 @@
177 176
     #(hash-map search-key (-> % :results :opensearch:Query :searchTerms))
178 177
     parse-unpaged-fn get-fn))
179 178
 
180
-;;;;;;;;;; forward declaration ;;;;;;;;;;
181 179
 
182
-(declare bio-struct artist-struct tag-struct album-struct user-struct
183
-  track-struct event-struct venue-struct location-struct shout-struct)
180
+;;;;;;;;;; record defs ;;;;;;;;;;
184 181
 
185
-(declare artist-from-name tag-from-name user-from-name)
182
+(defrecord Tag [name url])
186 183
 
187
-;;;;;;;;;; Bio/Wiki ;;;;;;;;;;
184
+(defrecord Bio [published summary content])
185
+
186
+(defrecord Location [latitude longitude street postalcode city country])
187
+
188
+(defrecord Venue [id name location url website phonenumber])
189
+
190
+(defrecord Event [id title artists headliner venue start description
191
+  attendence reviews tag url website cancelled tags])
192
+
193
+(defrecord Shout [body author date])
188 194
 
189
-(defstruct- bio-struct :published :summary :content)
195
+(defrecord Artist [name url mbid streamable listeners playcount bio])
196
+
197
+(defrecord Album [name id url mbid artist playcount])
198
+
199
+(defrecord Track
200
+  [name url mbid artist playcount listeners streamable])
201
+
202
+(defrecord User [name url realname])
203
+
204
+;;;;;;;;;; forward declaration ;;;;;;;;;;
205
+
206
+(declare artist-from-name tag-from-name user-from-name
207
+  album-from-map user-from-map track-from-map)
208
+
209
+;;;;;;;;;; Bio/Wiki ;;;;;;;;;;
190 210
 
191 211
 (defn- parse-bio [data]
192 212
   (do
193 213
     (debug (str "parse-bio: " data))
194
-    (struct
195
-      bio-struct
214
+    (Bio.
196 215
       (-> data :published parse-date)
197 216
       (data :summary)
198 217
       (data :content))))
199 218
 
200 219
 ;;;;;;;;;; Location ;;;;;;;;;;
201 220
 
202
-(defstruct- location-struct
203
-  :latitude :longitude :street :postalcode :city :country)
204
-
205 221
 (defn- parse-location [data]
206 222
   (do
207 223
     (debug (str "parse-location: " data))
208
-    (struct location-struct
224
+    (Location.
209 225
       (-> data :geo:point :geo:lat safe-parse-double)
210 226
       (-> data :geo:point :geo:long safe-parse-double)
211 227
       (data :street)
@@ -215,13 +231,10 @@
215 231
 
216 232
 ;;;;;;;;;; Venue ;;;;;;;;;;
217 233
 
218
-(defstruct- venue-struct
219
-  :id :name :location :url :website :phonenumber)
220
-
221 234
 (defn- parse-venue [data]
222 235
   (do
223 236
     (debug (str "parse-venue: " data))
224
-    (struct venue-struct
237
+    (Venue.
225 238
       (data :id)
226 239
       (data :name)
227 240
       (-> data :location parse-location)
@@ -231,10 +244,6 @@
231 244
 
232 245
 ;;;;;;;;;; Event ;;;;;;;;;;
233 246
 
234
-(defstruct- event-struct
235
-  :id :title :artists :headliner :venue :start :description
236
-  :attendence :reviews :tag :url :website :cancelled :tags)
237
-
238 247
 (defvar- parse-event-artists
239 248
   (create-parse-string-or-list-fn
240 249
     #(artist-from-name %) #(-> % :artists :artist)))
@@ -246,7 +255,7 @@
246 255
 (defn- parse-event [data]
247 256
   (do
248 257
     (debug (str "parse-event: " data))
249
-    (struct event-struct
258
+    (Event.
250 259
       (data :id)
251 260
       (data :title)
252 261
       (parse-event-artists data)
@@ -264,26 +273,20 @@
264 273
 
265 274
 ;;;;;;;;;; Shout ;;;;;;;;;;
266 275
 
267
-(defstruct- shout-struct :body :author :date)
268
-
269 276
 (defn- parse-shout [data]
270 277
   (do
271 278
     (debug (str "parse-shout: " data))
272
-    (struct shout-struct
279
+    (Shout.
273 280
       (data :body)
274 281
       (-> data :author user-from-name)
275 282
       (-> data :date parse-date))))
276 283
 
277 284
 ;;;;;;;;;; Artist ;;;;;;;;;;
278 285
 
279
-(defstruct- artist-struct
280
-  :name :url :mbid :streamable :listeners :playcount :bio)
281
-
282 286
 (defn- parse-artist [data]
283 287
   (do
284 288
     (debug (str "parse-artist: " data))
285
-    (struct
286
-      artist-struct
289
+    (Artist.
287 290
       (data :name)
288 291
       (data :url)
289 292
       (data :mbid)
@@ -292,8 +295,10 @@
292 295
       (-> data :stats :playcount safe-parse-int)
293 296
       (-> data :bio parse-bio))))
294 297
 
295
-(defn- artist-from-name [artst-name]
296
-  (struct-map artist-struct :name artst-name))
298
+(defn- artist-from-map [field-map]
299
+  (merge (Artist. nil nil nil nil nil nil nil) field-map))
300
+
301
+(defn- artist-from-name [artist-name] (artist-from-map {:name artist-name}))
297 302
 
298 303
 ;;;;;;;;;; artist.getinfo ;;;;;;;;;;
299 304
 
@@ -328,12 +333,12 @@
328 333
 ;;;;;;;;;; artist.getsimilar ;;;;;;;;;;
329 334
 
330 335
 (defn- parse-artist-similar-1 [data]
331
-  (struct-map artist-struct
332
-    :name (data :name)
333
-    :url (data :url)
334
-    :mbid (data :mbid)
335
-    :streamable (-> data :streamable str-1?)
336
-    :match (-> data :match safe-parse-double)))
336
+  (artist-from-map
337
+    {:name (data :name)
338
+     :url (data :url)
339
+     :mbid (data :mbid)
340
+     :streamable (-> data :streamable str-1?)
341
+     :match (-> data :match safe-parse-double)}))
337 342
 
338 343
 (defvar- parse-artist-similar
339 344
   (create-parse-one-or-more-fn
@@ -362,7 +367,7 @@
362 367
 
363 368
 (defvar- parse-artist-toptags
364 369
   (create-parse-one-or-more-fn
365
-    #(struct tag-struct (% :name) (% :url))
370
+    #(Tag. (% :name) (% :url))
366 371
     #(-> % :toptags :tag)))
367 372
 
368 373
 (defvar- get-artist-toptags
@@ -380,16 +385,16 @@
380 385
 ;;;;;;;;;; artist.gettopalbums ;;;;;;;;;;
381 386
 
382 387
 (defn- parse-artist-topalbums-1 [data]
383
-  (struct-map album-struct
384
-    :name (data :name)
385
-    :url (data :url)
386
-    :mbid (data :mbid)
387
-    :artist (struct-map artist-struct
388
-              :name (-> data :artist :name)
389
-              :url (-> data :artist :url)
390
-              :mbid (-> data :artist :mbid))
391
-    :playcount (-> data :playcount safe-parse-int)
392
-    :rank (-> data attr-kw :rank safe-parse-int)))
388
+  (album-from-map
389
+    {:name (data :name)
390
+     :url (data :url)
391
+     :mbid (data :mbid)
392
+     :artist (artist-from-map
393
+              {:name (-> data :artist :name)
394
+               :url (-> data :artist :url)
395
+               :mbid (-> data :artist :mbid)})
396
+     :playcount (-> data :playcount safe-parse-int)
397
+     :rank (-> data attr-kw :rank safe-parse-int)}))
393 398
 
394 399
 (defvar- parse-artist-topalbums
395 400
   (create-parse-one-or-more-fn
@@ -410,11 +415,11 @@
410 415
 ;;;;;;;;;; artist.gettopfans ;;;;;;;;;;
411 416
 
412 417
 (defn- parse-artist-topfans-1 [data]
413
-  (struct-map user-struct
414
-    :name (data :name)
415
-    :url (data :url)
416
-    :realname (data :realname)
417
-    :weight (-> data :weight safe-parse-int)))
418
+  (user-from-map
419
+    {:name (data :name)
420
+     :url (data :url)
421
+     :realname (data :realname)
422
+     :weight (-> data :weight safe-parse-int)}))
418 423
 
419 424
 (defvar- parse-artist-topfans
420 425
   (create-parse-one-or-more-fn
@@ -435,18 +440,18 @@
435 440
 ;;;;;;;;;; artist.gettoptracks ;;;;;;;;;;
436 441
 
437 442
 (defn- parse-artist-toptracks-1 [data]
438
-  (struct-map track-struct
439
-    :name (data :name)
440
-    :url (data :url)
441
-    :mbid (data :mbid)
442
-    :artist (struct-map artist-struct
443
-              :name (-> data :artist :name)
444
-              :url (-> data :artist :url)
445
-              :mbid (-> data :artist :mbid))
446
-    :playcount (-> data :playcount safe-parse-int)
447
-    :listeners (-> data :listeners safe-parse-int)
448
-    :streamable (-> data :streamable text-kw str-1?)
449
-    :streamable-full (-> data :streamable :fulltrack str-1?)))
443
+  (track-from-map
444
+    {:name (data :name)
445
+     :url (data :url)
446
+     :mbid (data :mbid)
447
+     :artist (artist-from-map
448
+              {:name (-> data :artist :name)
449
+               :url (-> data :artist :url)
450
+               :mbid (-> data :artist :mbid)})
451
+     :playcount (-> data :playcount safe-parse-int)
452
+     :listeners (-> data :listeners safe-parse-int)
453
+     :streamable (-> data :streamable text-kw str-1?)
454
+     :streamable-full (-> data :streamable :fulltrack str-1?)}))
450 455
 
451 456
 (defvar- parse-artist-toptracks
452 457
   (create-parse-one-or-more-fn
@@ -544,11 +549,11 @@
544 549
 
545 550
 (defvar- parse-artist-search-unpaged
546 551
   (create-parse-one-or-more-fn
547
-    #(struct-map artist-struct
548
-        :name (% :name)
549
-        :url (% :url)
550
-        :mbid (% :mbid)
551
-        :streamable (-> % :streamable str-1?))
552
+    #(artist-from-map
553
+        {:name (% :name)
554
+         :url (% :url)
555
+         :mbid (% :mbid)
556
+         :streamable (-> % :streamable str-1?)})
552 557
     #(-> % :results :artistmatches :artist)))
553 558
 
554 559
 (defvar- parse-artist-search
@@ -564,7 +569,8 @@
564 569
 
565 570
 ;;;;;;;;;; Album ;;;;;;;;;;
566 571
 
567
-(defstruct- album-struct :name :id :url :mbid :artist :playcount)
572
+(defn- album-from-map [field-map]
573
+  (merge (Album. nil nil nil nil nil nil) field-map))
568 574
 
569 575
 ;;;;;;;;;; album.search ;;;;;;;;;;
570 576
 
@@ -572,12 +578,12 @@
572 578
 
573 579
 (defvar- parse-album-search-unpaged
574 580
   (create-parse-one-or-more-fn
575
-    #(struct-map album-struct
576
-        :name (% :name)
577
-        :url (% :url)
578
-        :id (-> % :id safe-parse-int)
579
-        :artist (-> % :artist artist-from-name)
580
-        :streamable (-> % :streamable str-1?))
581
+    #(album-from-map
582
+        {:name (% :name)
583
+         :url (% :url)
584
+         :id (-> % :id safe-parse-int)
585
+         :artist (-> % :artist artist-from-name)
586
+         :streamable (-> % :streamable str-1?)})
581 587
     #(-> % :results :albummatches :album)))
582 588
 
583 589
 (defvar- parse-album-search
@@ -593,22 +599,21 @@
593 599
 
594 600
 ;;;;;;;;;; Tag ;;;;;;;;;;
595 601
 
596
-(defstruct- tag-struct :name :url)
597
-
598 602
 (defn- tag-from-name [tag-name]
599
-  (struct tag-struct tag-name (lastfm-url (str "/tag/" tag-name))))
603
+  (Tag. tag-name (lastfm-url (str "/tag/" tag-name))))
600 604
 
601 605
 ;;;;;;;;;; Track ;;;;;;;;;;
602 606
 
603
-(defstruct- track-struct
604
-  :name :url :mbid :artist :playcount :listeners :streamable)
607
+(defn- track-from-map [field-map]
608
+  (merge (Track. nil nil nil nil nil nil nil) field-map))
605 609
 
606 610
 ;;;;;;;;;; User ;;;;;;;;;;
607 611
 
608
-(defstruct- user-struct :name :url :realname)
612
+(defn- user-from-map [field-map]
613
+  (merge (User. nil nil nil) field-map))
609 614
 
610 615
 (defn- user-from-name [user-name]
611
-  (struct-map user-struct :name user-name))
616
+  (user-from-map {:name user-name}))
612 617
 
613 618
 (comment
614 619
 

+ 8
- 1
src/clj_lastfm/examples.clj View File

@@ -6,4 +6,11 @@
6 6
 (defn print-topten-tracks [artist-name]
7 7
   (doseq [[idx name]
8 8
             (->> artist-name artist-toptracks (take 10) (map :name) indexed)]
9
-    (println (format "%s. %s" idx name))))
9
+    (println (format "%s. %s" idx name))))
10
+
11
+(defn print-topten-album-search-results [album-name]
12
+  (doseq [[idx [name artist]]
13
+            (->> album-name album-search (take 10)
14
+              (map #(vector (:name %) (-> % :artist :name)))
15
+              indexed)]
16
+    (-> "%s. \"%s\" by %s" (format idx name artist) println)))

+ 1
- 1
src/clj_lastfm/filecache.clj View File

@@ -1,6 +1,6 @@
1 1
 (ns clj-lastfm.filecache
2 2
   (:import (java.io File))
3
-  (:use [clojure.contrib.duck-streams]
3
+  (:use [clojure.contrib.duck-streams :only (reader copy)]
4 4
         [clojure.contrib.logging]))
5 5
 
6 6
 (def default-cache-dir (File. (System/getProperty "java.io.tmpdir")))

Loading…
Cancel
Save