PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/scala/BandSlurper.scala

https://github.com/robb1e/Gig-Recommender
Scala | 85 lines | 70 code | 15 blank | 0 comment | 2 complexity | 2bc0f948720c0c60801e51f33a96c383 MD5 | raw file
  1. package com.gu.music.recommendations
  2. import com.gu.WebClient
  3. import scala.util.parsing.json.JSON
  4. import net.liftweb.json.JsonParser.parse
  5. import net.liftweb.json.{DefaultFormats, Formats}
  6. import scala.xml._
  7. import java.io._
  8. import com.mongodb.casbah.MongoConnection
  9. import com.mongodb.casbah.commons.Imports._
  10. import com.mongodb.casbah.commons.conversions.scala._
  11. case class Band(name: String, mbid: String = "")
  12. case class BandList(bands: List[Band])
  13. class BandSlurper {
  14. val mongoConn = MongoConnection()
  15. val mongoDB = mongoConn("sxsw")
  16. val bandsCollection = mongoDB("bands")
  17. val similarCollection = mongoDB("similar")
  18. val configCollection = mongoDB("config")
  19. bandsCollection.ensureIndex(MongoDBObject("name" -> 1), "name", true)
  20. similarCollection.ensureIndex(MongoDBObject("name" -> 1), "name", true)
  21. def init = configCollection.findOne(MongoDBObject("reload" -> true)).map(res => load)
  22. def load = {
  23. val bandData = Option(getClass.getResourceAsStream("/sxswbands.json"))
  24. .map(scala.io.Source.fromInputStream(_))
  25. .map(_.mkString)
  26. .getOrElse(throw new Exception("no band json file found"))
  27. val bandsJson = JSON.parseFull(bandData).map(b => b.asInstanceOf[List[AnyRef]])
  28. val bandsCase = bandsJson.get.map(b =>
  29. Band(b.asInstanceOf[Map[String, String]].getOrElse("band_name", ""), b.asInstanceOf[Map[String, String]].getOrElse("musicbrainz", "")))
  30. .filter(bd => bd.mbid != "")
  31. bandsCase.foreach(b => {
  32. bandsCollection.findOne(MongoDBObject("mbid" -> b.mbid.trim)) match {
  33. case Some(i) => println("ignoring " + b.name)
  34. case _ => {
  35. bandsCollection += MongoDBObject("name" -> b.name.trim, "mbid" -> b.mbid.trim)
  36. try{
  37. getSimilarAndUpdate(b.name.trim, b.mbid.trim)
  38. } catch {
  39. case ex: Exception => println("ERROR " + ex.getMessage)
  40. }
  41. }
  42. }
  43. })
  44. }
  45. private def getSimilarAndUpdate(name: String, mbid: String) = {
  46. val query = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&mbid=%s&api_key=b25b959554ed76058ac220b7b2e0a026" format mbid
  47. println("looking for similar bands for '%s' at %s" format (name, query))
  48. val similar = WebClient.get(query)
  49. val someXml = XML.loadString(similar)
  50. val similarBands = (someXml \ "similarartists" \ "artist").map(s => Band((s \ "name").text.trim, (s \ "mbid").text.trim))
  51. similarBands.foreach(s => similarCollection.findOne(MongoDBObject("mbid" -> s.mbid)) match {
  52. case Some(result) => {
  53. similarCollection.update(MongoDBObject("mbid" -> s.mbid), MongoDBObject("$addToSet" -> MongoDBObject("like" -> MongoDBObject("name" -> name, "mbid" -> mbid))))
  54. }
  55. case _ => {
  56. similarCollection += MongoDBObject("name" -> s.name, "mbid" -> s.mbid)
  57. similarCollection.update(MongoDBObject("mbid" -> s.mbid), MongoDBObject("$addToSet" -> MongoDBObject("like" -> MongoDBObject("name" -> name, "mbid" -> mbid))))
  58. }
  59. })
  60. }
  61. def getBands = None
  62. def getSimilarBandsTo(mbid: String) = {
  63. similarCollection.findOne(MongoDBObject("mbid" -> mbid))
  64. }
  65. def topArtists(lastfm: String) = {
  66. val topArtists = WebClient.get("http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=%s&api_key=b25b959554ed76058ac220b7b2e0a026" format lastfm)
  67. val someXml = XML.loadString(topArtists)
  68. (someXml \ "topartists" \ "artist").map(a => Band((a \ "name").text.trim, (a \ "mbid").text.trim)).filter(b => b.mbid != "")
  69. }
  70. }