Code Monkey home page Code Monkey logo

diffbot-scala-client's Introduction

Scala API for Diffbot services

Installation

Please see the INSTALL file for details. The easiest way is using sbt, by cloning the repository (preferably a released tag) then

 > publish-local

from this project in sbt. After that, you can simply use it as a dependency, as the following:

  libraryDependencies += "com.diffbot" %% "diffbot-scala-api" % "1.0",

to your build configuration (typically build.sbt, or project/Build.scala in your project's folder).

Configuration

As this client uses spray for its middleware, you can override certain configuration of spray, by adding an application.conf file.

The Diffbot API specific configuration is quite simple. You just have to create an implicit value with the token you gathered, like this:

  implicit val token: Token = Token("0123456789abcdef0123456789abcdef")

You can override the timeouts and the version too with different implicit values, like:

  /**The first parameter is how long the client waits for the server, while the second is for the timeout parameter passed to the server in milliseconds, which can be None if you prefer not to specify.*/
  implicit val t: DiffbotTimeout = DiffbotTimeout(/*client*/timeout=125.second, timeoutParameterInMillis=Some(100000L))
  /** The service version to use. */
  implicit val v: Version = Version.v2

You will also need an ActorSystem for this client:

  implicit val diffbotSystem:ActorSystem = ActorSystem("diffbotExample")//Just an example

Usage

Analyze API

Just execute the following code and later you can examine results:

  val result: Future[JsValue] = Diffbot("analyze", url)

When you get the results, you can use the usual technics on scala Futures, for example:

  result onSuccess {case t => println(t.prettyPrint)}

If you prefer synchronous calls, you can wait till the result is ready, using Await.

Article API

Just execute the following code and later you can examine results:

  val result: Future[JsValue] = Diffbot("article", url)

You can handle the cases similarly to the Analyze API.

Other APIs

Please check the parameters for other Diffbot products on the homepage: http://diffbot.com/products/

Complete example

Here is a whole sample program:

		package com.diffbot.api.scala
		
		import scala.concurrent.Future
		import spray.json._
		import akka.actor.ActorSystem
		
		object Main extends App {
		  //We need an ActorSystem
		  implicit val diffbotSystem:ActorSystem = ActorSystem("diffbotExample")
		
		  //We need a token it consists of 32 hexadecimal digits
		  //please check the http://diffbot.com/pricing/ page for details
		  implicit val token: Token = Token("0123456789abcdef0123456789abcdef")
        
          val url = "http://www.xconomy.com/san-francisco/2012/07/25/diffbot-is-using-computer-vision-to-reinvent-the-semantic-web/"
          
          //You can override the version and the timeout too with implicit values
		  //import scala.concurrent.duration._
          //implicit val t: DiffbotTimeout = DiffbotTimeout(125.second, Some(100000L))
          //implicit val v: Version = Version.v2
          //or just use our suggested defaults
		  import Implicits._
          
		  //Call the API
		  val f: Future[JsValue] = Diffbot.call("article", url)
		
		  //Use the result in some way
		  import scala.concurrent.ExecutionContext.Implicits.global
		  f.onComplete(t => {println(t.get.prettyPrint)})
		  //TODO, you might want to handle failure too
		
		  //This way you can simulate synchronous method calls
		  import scala.concurrent.Await
		  import scala.concurrent.duration._
		  val json: JsValue = Await.result(f, 125.seconds)
		  
		  //Shut down the actor system, we no longer need them.
		  diffbotSystem.shutdown
		}

Notes

Please check the Spray http-client documentation for additional customization options, especially Request level clients. For configuration (like proxy, or different connection specific options), please see the Spray configuration page.

-Initial commit by Gábor Bakos-

diffbot-scala-client's People

Contributors

aborg0 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.