Skip to content

Instantly share code, notes, and snippets.

@ngsw-taro
Last active December 21, 2015 20:49
Show Gist options
  • Save ngsw-taro/6363959 to your computer and use it in GitHub Desktop.
Save ngsw-taro/6363959 to your computer and use it in GitHub Desktop.
Scalaの練習。SlideshareのAPI叩いてみた
// 使用例
import com.taroid.slideshare4s.{SortOrder, SlideShare, Query}
object Main {
def main(args: Array[String]) {
val apiKey = System.getenv("SLIDESHARE_API_KEY")
val sharedSecret = System.getenv("SLIDESHARE_SHARED_SECRET")
val ss = SlideShare(apiKey, sharedSecret)
val query = Query(words = "kotlin", itemsPerPage = 10, language = "ja", sortOrder = SortOrder.LATEST)
ss.searchSlideshow(query).foreach(println)
}
}
package com.taroid.slideshare4s
import com.taroid.slideshare4s.SortOrder.SortOrder
case class Query(
words: String,
page: Int = 1,
itemsPerPage: Int = 12,
language: String = "**",
sortOrder: SortOrder = SortOrder.RELEVANCE
) {
require(words != null)
require(words.size > 0)
require(language != null)
require(sortOrder != null)
}
package com.taroid.slideshare4s
trait SlideShare {
def apiKey: String
def sharedSecret: String
protected def currentTimeSeconds: Long = System.currentTimeMillis / 1000
protected def hash: String = Utils.toSha1Hex(sharedSecret + currentTimeSeconds)
def searchSlideshow(query: Query): Seq[Slideshow]
}
object SlideShare {
def apply(apiKey: String, sharedSecret: String): SlideShare = {
require(apiKey != null)
require(apiKey.size > 0)
require(sharedSecret != null)
require(sharedSecret.size > 0)
return new SlideShareImpl(apiKey, sharedSecret)
}
}
package com.taroid.slideshare4s
import java.text.SimpleDateFormat
import scala.xml.XML
private class SlideShareImpl(val apiKey: String, val sharedSecret: String) extends SlideShare {
assert(apiKey != null)
assert(apiKey.size > 0)
assert(sharedSecret != null)
assert(sharedSecret.size > 0)
override def searchSlideshow(query: Query): Seq[Slideshow] = {
require(query != null)
import SlideShareImpl._
val url = Utils.createUrlWithParams(Urls.SEARCH,
"api_key" -> apiKey,
"ts" -> currentTimeSeconds,
"hash" -> hash,
"q" -> query.words,
"page" -> query.page,
"items_per_page" -> query.itemsPerPage,
"lang" -> query.language,
"sort" -> query.sortOrder
)
val dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
return (XML.load(url) \ Tags.SLIDESHOW) map { e =>
Slideshow(
id = (e \ Tags.ID).text.toLong,
title = (e \ Tags.TITLE).text,
username = (e \ Tags.USERNAME).text,
description = (e \ Tags.DESC).text,
url = (e \ Tags.URL).text,
thumbnailUrl = (e \ Tags.THUMB_URL).text,
created = dateFormat.parse((e \ Tags.CREATED).text),
updated = dateFormat.parse((e \ Tags.UPDATED).text),
language = (e \ Tags.LANG).text
)
}
}
}
private object SlideShareImpl {
private object Urls {
private val BASE = "https://www.slideshare.net/api/2/"
val SEARCH = BASE + "search_slideshows"
}
private object Tags {
val SLIDESHOW = "Slideshow"
val ID = "ID"
val TITLE = "Title"
val USERNAME = "Username"
val DESC = "Description"
val URL = "URL"
val THUMB_URL = "ThumbnailURL"
val CREATED = "Created"
val UPDATED = "Updated"
val LANG = "Language"
}
}
package com.taroid.slideshare4s
import java.util.Date
case class Slideshow(
id: Long,
title: String,
username: String,
description: String,
url: String,
thumbnailUrl: String,
created: Date,
updated: Date,
language: String
) {
require(title != null)
require(username != null)
require(description != null)
require(url != null)
require(thumbnailUrl != null)
require(created != null)
require(updated != null)
require(language != null)
}
package com.taroid.slideshare4s
object SortOrder extends Enumeration {
type SortOrder = Value
val RELEVANCE = Value("relevance")
val MOSTVIEWED = Value("mostviewd")
val MOSTDOWNLOADED = Value("mostdownloaded")
val LATEST = Value("latest")
}
package com.taroid.slideshare4s
import java.security.MessageDigest
private object Utils {
def toSha1Hex(str: String): String = {
require(str != null)
val messageDigest = MessageDigest.getInstance("SHA-1")
messageDigest.update(str.getBytes)
return messageDigest.digest.map("%02x".format(_)).mkString
}
def createUrlWithParams(url: String, params: (String, Any)*): String = {
require(url != null)
require(params.forall(_ != null))
val builder = params.foldLeft(new StringBuilder(url).append("?")) {
(b, p) => b.append(p._1).append("=").append(p._2.toString).append("&")
}
return builder.substring(0, builder.size - 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment