eed3si9n / repatch-twitter   dispatch0.11.1_0.1.0

Website GitHub

a plugin for Dispatch to use Twitter API

Scala versions: 2.11 2.10

repatch-twitter

repatch-twitter is a Dispatch 0.11 plugin for Twitter API.

license

setup

resolvers += Resolver.sonatypeRepo("public")

// libraryDependencies += "com.eed3si9n" %% "repatch-twitter-core" % "dispatch0.11.0_0.1.0"
libraryDependencies += "com.eed3si9n" %% "repatch-twitter-core" % "dispatch0.11.1_0.1.0"

authentication

repatch.twitter.request.AbstractClient abstracts the OAuth authentication to use Twitter API. OAuthClient implements it.

scala> import dispatch._, Defaults._
import dispatch._
import Defaults._

scala> import com.ning.http.client.oauth._
import com.ning.http.client.oauth._

scala> import repatch.twitter.request._
import repatch.twitter.request._

scala> val consumer = new ConsumerKey("abcd", "secret")
consumer: com.ning.http.client.oauth.ConsumerKey = {Consumer key, key="abcd", secret="secret"}

scala> val accessToken = new RequestToken("xyz", "secret")
accessToken: com.ning.http.client.oauth.RequestToken = { key="xyz", secret="secret"}

scala> val client = OAuthClient(consumer, accessToken)
client: repatch.twitter.request.OAuthClient = <function1>

There's ProperitesClient that can load the consumer key and the access token from the given file name:

scala> val prop = new java.io.File(System.getProperty("user.home"), ".foo.properties")
prop: java.io.File = /Users/you/.foo.properties

scala> val client = PropertiesClient(prop)
client: repatch.twitter.request.OAuthClient = <function1>

search

Here's how you can search for tweets:

scala> val x = http(client(Search("#scala").geocode_mi(40.7142, -74.0064, 10).count(2)) OK
         as.repatch.twitter.response.Search)
x2: dispatch.Future[repatch.twitter.response.Search] = scala.concurrent.impl.Promise$DefaultPromise@6bc9806d

scala> val search = x()
search: repatch.twitter.response.Search = Search(List(Tweet(330931826879234049,Rocking the contravariance. Hard. #nerd...

In the above, repatch.twitter.request.Search is a request builder, supporting methods such as geocode_mi and count, which return Search. Using as.repatch.twitter.response.Search converter returns repatch.twitter.response.Search. Alternatively, you can return json, and parse individual fields as follows:

scala> val x = http(client(Search("#scala").geocode_mi(40.7142, -74.0064, 10).count(2)) OK as.json4s.Json)
x: dispatch.Future[org.json4s.JValue] = scala.concurrent.impl.Promise$DefaultPromise@3252d2de

scala> val json = x()
json: org.json4s.JValue = 
JObject(List((statuses,JArray(List(JObject(List((metadata,JObject(List((result_...

scala> {
         import repatch.twitter.response.Search._
         import repatch.twitter.response.Tweet._
         for {
           t <- statuses(json)
         } yield(id_str(t), text(t))
       }
res0: List[(String, String)] = List((330931826879234049,Rocking the contravariance. Hard. #nerd #scala), (330877539461500928,RT @mhamrah: Excellent article on structuring distributed systems with #rabbitmq. Thanks @heroku Scaling Out with #Scala and #Akka http://t…))

timelines

Here's how to get your timeline.

scala> val x = http(client(Status.home_timeline) OK as.repatch.twitter.response.Tweets)
x: dispatch.Future[repatch.twitter.response.Statuses] = scala.concurrent.impl.Promise$DefaultPromise@41ad625a

scala> x()
res0: List[repatch.twitter.response.Tweet] = 
List(Tweet(331691122629951489,Partially applying a function that has an implicit parameter http://t.co/CwWQAkkBAN,....

Returns the 20 most recent mentions.

scala> val timeline = http(client(Status.mentions_timeline) OK as.repatch.twitter.response.Tweets)
timeline: dispatch.Future[List[repatch.twitter.response.Tweet]] = scala.concurrent.impl.Promise$DefaultPromise@53a5af37

scala> timeline()
res0: List[repatch.twitter.response.Tweet] = 
List(Tweet(333386163618455553,@eed3si9n I keep seeing pros and cons...

Returns a collection of the most recent Tweets posted by the user indicated by the screen_name or user_id parameters.

scala> val timeline = http(client(Status.user_timeline("twitterapi")) OK as.repatch.twitter.response.Tweets)
timeline: dispatch.Future[List[repatch.twitter.response.Tweet]] = scala.concurrent.impl.Promise$DefaultPromise@40878d74

scala> timeline()
res0: List[repatch.twitter.response.Tweet] = 
List(Tweet(330369772619452416,We're extending API v1 Retirement...

Returns the most recent tweets authored by the authenticating user that have been retweeted by others.

scala> val timeline = http(client(Status.retweets_of_me) OK as.repatch.twitter.response.Tweets)
timeline: dispatch.Future[List[repatch.twitter.response.Tweet]] = scala.concurrent.impl.Promise$DefaultPromise@14128779

tweets

Send a tweet.

scala> val x = http(client(Status.update("testing from REPL"))
         OK as.repatch.twitter.response.Tweet)
x: dispatch.Future[repatch.twitter.response.Tweet = scala.concurrent.impl.Promise$DefaultPromise@65056d18

To reply to a tweet, call in_reply_to_status_id(id) method on Status.Update class.

Retweets a tweet.

scala> val rt = http(client(Status.retweet(res0.id)) OK as.repatch.twitter.response.Tweet)
rt: dispatch.Future[repatch.twitter.response.Tweet] = scala.concurrent.impl.Promise$DefaultPromise@6758cd8

scala> rt()
res7: repatch.twitter.response.Tweet = Tweet(333519011696484352,RT @PLT_Hulk: ...

Returns up to 100 of the first retweets of a given tweet.

scala> val rts = http(client(Status.retweets(BigInt("317744323254943744"))) OK
         as.repatch.twitter.response.Tweets)
rts: dispatch.Future[List[repatch.twitter.response.Tweet]] = scala.concurrent.impl.Promise$DefaultPromise@98fd639

scala> rts()
res4: List[repatch.twitter.response.Tweet] = 
List(Tweet(317776021933916160,RT @eed3si9n: scala> :k Monad // Finds locally imported types.

Returns a single Tweet, specified by the id parameter.

scala> val x = http(client(Status.show(BigInt("317744323254943744"))) OK
         as.repatch.twitter.response.Tweet)
x: dispatch.Future[repatch.twitter.response.Tweet] = scala.concurrent.impl.Promise$DefaultPromise@4c603a77

scala> x()
res2: repatch.twitter.response.Tweet = 
Tweet(317744323254943744,scala> :k Monad // Finds locally imported types

Destroys the status specified by the required ID parameter.

scala> x()
res0: repatch.twitter.response.Tweet = Tweet(333505115287846913, ...

scala> val deleted = http(client(Status.destroy(res0.id)) OK as.repatch.twitter.response.Tweet)
deleted: dispatch.Future[repatch.twitter.response.Tweet] = scala.concurrent.impl.Promise$DefaultPromise@a852084

scala> deleted()
res3: repatch.twitter.response.Tweet = Tweet(333505115287846913, ...

favorites

Returns the 20 most recent Tweets favorited by the authenticating or specified user.

scala> val myfavs = http(client(Favorite.list) OK as.repatch.twitter.response.Tweets)
favs: dispatch.Future[List[repatch.twitter.response.Tweet]] = scala.concurrent.impl.Promise$DefaultPromise@4482d3a3

scala> myfavs()
res0: List[repatch.twitter.response.Tweet] = List(Tweet(331773677295239169

scala> val favs = http(client(Favorite.list("PLT_HULK")) OK as.repatch.twitter.response.Tweets)
favs: dispatch.Future[List[repatch.twitter.response.Tweet]] = scala.concurrent.impl.Promise$DefaultPromise@366ec9d4

scala> favs()
res1: List[repatch.twitter.response.Tweet] = List(Tweet(246253900770983937,@PLT_Hulk is angry lately.,...

Favorites the status specified in the ID parameter as the authenticating user.

scala> val fav = http(client(Favorite(res0.id)) OK as.repatch.twitter.response.Tweet)
fav: dispatch.Future[repatch.twitter.response.Tweet] = scala.concurrent.impl.Promise$DefaultPromise@12810908

scala> fav()
res1: repatch.twitter.response.Tweet = Tweet(333297803155611649,

Un-favorites the status specified in the ID parameter as the authenticating user.

scala> val unfav = http(client(Favorite.destroy(res0.id)) OK as.repatch.twitter.response.Tweet)
unfav: dispatch.Future[repatch.twitter.response.Tweet] = scala.concurrent.impl.Promise$DefaultPromise@7f3e93cb

scala> unfav()
res1: repatch.twitter.response.Tweet = Tweet(333297803155611649

streaming

Returns public statuses that match one or more filter predicates.

scala> http(client(PublicStream.track("scala")) > 
         as.repatch.twitter.response.stream.TweetOrJson {
         case Right(tweet) => println(tweet) 
         case Left(json)   => println(json) 
       })
res0: dispatch.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@3f77f4ea

scala> Tweet(333804083792191488,Looking for a Hoogle like to Scala? ....

Streams messages for a single user, as described in User streams.

scala> http(client(UserStream()) > as.repatch.twitter.response.stream.TweetOrJson {
         case Right(tweet) => println(tweet) 
         case Left(json)   => println(json) 
       })
res0: dispatch.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@451094e7

friends & followers

Returns a cursored collection of user IDs for every user the specified user is following.

scala> val x = http(client(Friend.ids) OK as.json4s.Json)
x: dispatch.Future[org.json4s.JValue] = scala.concurrent.impl.Promise$DefaultPromise@72d53ac7

scala> {
         import repatch.twitter.response.Friend._
         val json = x()
         ids(json)
       }
res0: List[BigInt] = List(812340000, ...

Returns a cursored collection of user IDs for every user following the specified user.

scala> val x = http(client(Follower.ids) OK as.json4s.Json)
x: dispatch.Future[org.json4s.JValue] = scala.concurrent.impl.Promise$DefaultPromise@7449d884

scala> {
         import repatch.twitter.response.Follower._
         val json = x()
         ids(json)
       }
res0: List[BigInt] = List(1234567, ...

Allows the authenticating users to follow the user specified in the ID parameter.

scala> val u = http(client(Friendship("twitterapi")) OK as.repatch.twitter.response.User)
u: dispatch.Future[repatch.twitter.response.User] = scala.concurrent.impl.Promise$DefaultPromise@50e3fa1d

scala> u()
res0: repatch.twitter.response.User = User(6253282,twitterapi,...

Allows the authenticating user to unfollow the user specified in the ID parameter.

scala> val u = http(client(Friendship.destroy("twitterapi")) OK as.repatch.twitter.response.User)
u: dispatch.Future[repatch.twitter.response.User] = scala.concurrent.impl.Promise$DefaultPromise@487793db

scala> u()
res0: repatch.twitter.response.User = User(6253282,twitterapi,...

users

Returns a variety of information about the user specified by the required user_id or screen_name parameter.

scala> val u = http(client(User.show("twitterapi")) OK as.repatch.twitter.response.User)
u: dispatch.Future[repatch.twitter.response.User] = scala.concurrent.impl.Promise$DefaultPromise@3aff776

scala> u().status map {_.text}
res0: Option[String] = Some(We have deprecated HTTP 1.0 support for the Streaming API: https://t.co/JfieFem8Kf)