Created
September 28, 2011 11:47
-
-
Save urcadox/1247751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code { | |
package model { | |
import java.net.URL | |
import dispatch._ | |
import dispatch.liftjson.Js._ | |
import net.liftweb.json.JsonAST._ | |
import net.liftweb | |
import net.liftweb.util.Helpers._ | |
case class VideoData(title: String, playerURL: String, thumbnail: String, pageURL: String) | |
abstract class EmbeddedVideo(pageURL: URL) { | |
val getTitle: String; | |
val getPlayerURL: URL; | |
val getThumbnailURL: URL; | |
} | |
class YoutubeEmbeddedVideo(pageURL: URL) extends EmbeddedVideo(pageURL) { | |
private val getVideoID: String = { | |
pageURL.getQuery.substring(2) | |
} | |
val apiURL = "http://gdata.youtube.com/feeds/api/videos/"+getVideoID+"?v=2&alt=jsonc" | |
val getTitle: String = { | |
Http(url(apiURL.toString) ># { | |
json => json \ "data" \ "title" | |
}).values.toString | |
} | |
val getPlayerURL: URL = { | |
new URL("http://www.youtube.com/embed/"+getVideoID) | |
} | |
val getThumbnailURL: URL = { | |
new URL(Http(url(apiURL.toString) ># { | |
json => json \ "data" \ "thumbnail" \ "hqDefault" | |
}).values.toString) | |
} | |
} | |
class DailymotionEmbeddedVideo(pageURL: URL) extends EmbeddedVideo(pageURL) { | |
private val getVideoID: String = { | |
pageURL.getPath.substring( | |
pageURL.getPath.lastIndexOf("/")+1, | |
pageURL.getPath.indexOf("_") | |
) | |
} | |
val apiURL = "http://www.dailymotion.com/services/oembed?format=json&url="+pageURL.toString | |
val getTitle: String = { | |
Http(url(apiURL.toString) ># { | |
json => json \ "title" | |
}).values.toString | |
} | |
val getPlayerURL: URL = { | |
new URL("http://www.dailymotion.com/embed/video/"+getVideoID) | |
} | |
val getThumbnailURL: URL = { | |
new URL(Http(url(apiURL.toString) ># { | |
json => json \ "thumbnail_url" | |
}).values.toString.replace("large", "medium")) | |
} | |
} | |
class VimeoEmbeddedVideo(pageURL: URL) extends EmbeddedVideo(pageURL) { | |
private val getVideoID: String = { | |
pageURL.getPath.substring(1) | |
} | |
val apiURL = "http://vimeo.com/api/v2/video/"+getVideoID+".json" | |
val getTitle: String = { | |
Http(url(apiURL.toString) ># { | |
json => json \ "title" | |
}).values.toString | |
} | |
val getPlayerURL: URL = { | |
new URL("http://player.vimeo.com/video/"+getVideoID) | |
} | |
val getThumbnailURL: URL = { | |
new URL( | |
Http(url(apiURL.toString) ># { | |
json => json \ "thumbnail_medium" | |
}).values.toString) | |
} | |
} | |
object EmbeddedVideo { | |
def apply(pageURL: String) = { | |
val embeddedVideo = for { | |
validPageURL <- tryo(new URL(pageURL)) | |
v <- validPageURL.getHost match { | |
case "youtube.com" | | |
"www.youtube.com" => Some(tryo(new YoutubeEmbeddedVideo(validPageURL))) | |
case "dailymotion.com" | | |
"www.dailymotion.com" => Some(tryo(new DailymotionEmbeddedVideo(validPageURL))) | |
case "vimeo.com" | | |
"www.vimeo.com" => Some(tryo(new VimeoEmbeddedVideo(validPageURL))) | |
case _ => None | |
} | |
} yield v | |
embeddedVideo.get map (v => VideoData( | |
v.getTitle, | |
v.getPlayerURL.toString, | |
v.getThumbnailURL.toString, | |
pageURL.toString | |
)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment