Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Added ability to pull in album info
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisa van Gelder committed Jun 27, 2011
1 parent 5e1ff36 commit ccd4bcb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lastfm-api/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name := "lastfm-api"

organization := "com.gu.arts"

version := "0.1.5-SNAPSHOT"
version := "0.1.6-SNAPSHOT"

libraryDependencies += "joda-time" % "joda-time" % "1.6.2"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gu.arts.music.net.lastfm

import net.liftweb.json.JsonParser.parse
import java.net.URLEncoder
import net.liftweb.json.JsonAST.JValue

case class ArtistProfile(name: String, url: String, bio: ArtistBiography, tags: ArtistTags, stats: ArtistStats, image: List[ArtistImage]) {
def getImage(imageSize: String, default: String) = {
Expand Down Expand Up @@ -40,6 +41,22 @@ case class ArtistTrack(name: String, url: String)
case class SimilarArtists(artist: List[ArtistProfileSimple]){
lazy val artists = artist
}
case class LastFmAlbumSimple(name: String)

case class TopLevelAlbum(album: LastFmAlbum)

case class Wiki(summary: String, content: String)

case class LastFmAlbum(name: String, image: List[ArtistImage], wiki: Option[Wiki]){
def getImage(imageSize: String, default: String) = {
val flattened = image flatMap { artistImage =>
if (imageSize == artistImage.size) Some(artistImage)
else None
}
val artistImage = flattened.headOption getOrElse ArtistImage(default, "")
artistImage.text
}
}

abstract class ArtistApi extends LastfmApi {
override val searchToken = "mbid"
Expand All @@ -51,6 +68,46 @@ object ArtistProfileByName extends ArtistApi {
def apply(name: String)(implicit lastfmApiKey: String) = (parse(retrieve(URLEncoder.encode(name, "utf-8"), lastfmApiKey)) \ "artist").extractOpt[ArtistProfile]
}


object LastFmAlbum extends ArtistApi {
override val method = "album.getinfo"
override val searchToken = "artist"
override val searchToken2 = "album"


def apply(name: String, albumName: String)(implicit lastfmApiKey: String) = Some((parse(retrieveWithMoreTokens(name, albumName, lastfmApiKey))).extract[TopLevelAlbum].album)


// def apply(name: String, albumName: String)(implicit lastfmApiKey: String) = {
// val retrieved = retrieveWithMoreTokens(name, albumName, lastfmApiKey)
// log.info("Retrieved: %s" format retrieved)
// val parsed: JValue = parse(retrieved)
// log.info("Parsed: %s" format parsed)
// val lastFmAlbum = parsed.extract[TopLevelThing].album
// log.info("LastFmAlbum name:%s" format lastFmAlbum.name)
// Some(lastFmAlbum)
// }

def main(args: Array[String]) {
val json = """
{
"album":{
"name":"Revolver",
"artist":"The Beatles",
"id":"1353",
"mbid":"e27d2d03-b4f2-4d0e-801b-adea739382b6",
"url":"http:\/\/www.last.fm\/music\/The+Beatles\/Revolver",
"releasedate":" 5 Dec 1995, 00:00",
}
}
"""

val p = parse(json)
val r = p.extract[TopLevelThing]
println("r = " + r)
}
}

object ArtistProfile extends ArtistApi {
override val method = "artist.getinfo"
def apply(mbid: String)(implicit lastfmApiKey: String) = (parse(retrieve(mbid, lastfmApiKey)) \ "artist").extractOpt[ArtistProfile]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class LastfmApi extends ApiConfiguration {
implicit val formats = net.liftweb.json.DefaultFormats
val log = LoggerFactory getLogger getClass
val searchToken: String
val searchToken2: String = ""
val method: String
val host = "ws.audioscrobbler.com/2.0"

Expand All @@ -31,6 +32,14 @@ abstract class LastfmApi extends ApiConfiguration {
val h = new Http
h(request as_str).replace("#text", "text").replace("@attr", "attr")
}

def retrieveWithMoreTokens(token1: String, token2: String, apiKey: String) = {
val query = Map("method" -> method, "api_key" -> apiKey, searchToken -> token1, searchToken2 -> token2, "format" -> "json")
val request = :/(host) <:< headers <<? query
log.info("Retrieving resource http://ws.audioscrobbler.com/2.0%s" format request.path)
val h = new Http
h(request as_str).replace("#text", "text").replace("@attr", "attr")
}
}

object Trimmer {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Checkout the project, demo, to see how to call the companion objects for the cas
#Publishing

To publish to your local Maven/IVY repository, when in sbt (using ./sbt10) run the command
reload
clean
compile

publish-local

Expand Down

0 comments on commit ccd4bcb

Please sign in to comment.