TW4S
An unofficial Twitter library for Scala
Getting an API Key from Twitter
-
Visit the Twitter Developers Website
- Fill in their info and you will get 4 important values needed for OAuth
- An API Key
- An API Secret Key
- An Access Token
- A Secret Access Token
- Go over the Twitter API Docs
Installing
If you don't feel comfortable putting your keys inside your codebase, you can add them as environment varibles. This is optional, yet highly recommended. If you choose not to, all 4 keys must be fed as parameters to the TW4SService object.
Linux
Put these lines in /etc/environment or /etc/profile.
TW4S_API_KEY=<YOUR API KEY HERE>
TW4S_API_SECRET=<YOUR API SECRET HERE>
TW4S_ACCESS_TOKEN=<YOUR ACCESS TOKEN HERE>
TW4S_ACCESS_TOKEN_SECRET=<YOUR SECRET ACCESS TOKEN HERE>
Mac
Put these lines in /etc/launchd.conf
setenv TW4S_API_KEY <YOUR API KEY HERE>
setenv TW4S_API_SECRET <YOUR API SECRET HERE>
setenv TW4S_ACCESS_TOKEN <YOUR ACCESS TOKEN HERE>
setenv TW4S_ACCESS_TOKEN_SECRET <YOUR SECRET ACCESS TOKEN HERE>
Restart your computer.
Use the maven clean install directive to compile and make sure everythng works. I recommend you do not use -DskipTests. The tests will check your connection to Twitter
Maven/SBT
<dependency>
<groupId>com.celexus</groupId>
<artifactId>TW4S</artifactId>
<version>1.1.2</version>
</dependency>
libraryDependencies += "com.celexus" % "TW4S" % "1.1.2"
Using the TW4SService
// If you've set these the OAUTH values as environment variables
val tw4s: TW4SService = new TW4SService()
// If not, you have to provide it to the TW4SService
val tw4s: TW4SService = new TW4SService(apiKey,apiSecret,accessToken,accessSecret)
Information calls
User Settings
val settings: AccountSettings = tw4s.settings
settings.cookiePersonalization -> true
settings.discoverableEmail -> true
settings.discoverablePhone -> false
settings.displaySensitiveMedia -> false
settings.geoEnabled -> true
settings.handle -> "pronouncekamrin"
settings.https -> true
settings.lang -> "en"
settings.restricted -> false
User Credentials
val cred: Credentials = tw4s.credentials
cred.creationDate -> Wed Aug 14 01 35 39 EDT 2013
cred.description -> "Javatar, Scalaite, and Hadooper"
cred.favoritesCount -> 124
cred.followerCount -> 28
cred.following -> false
cred.followRequestSent -> false
cred.friendsCount -> 90
cred.geoEnabled -> true
cred.handle -> "pronouncekamrin"
cred.id -> 1234578912
cred.lang -> "en"
cred.listsCount -> 2
cred.location -> "Washington, DC"
cred.name -> "Cameron Cook"
cred.restricted -> false
cred.statusCount -> 328
cred.url -> http://t.co/3Fx2zXnG5P
cred.verified -> false
cred.lastStatus -> (Status Object)
Blocked Users
val blocked_names: Set[String] = tw4s.blocked()
val blocked_ids: Set[String] = tw4s.blocked_ids()
Users
val noradio: User = tw4s.user("noradio")
val users: Set[User] = tw4s.users(Array("noradio"))
val contributees: Set[User] = tw4s.contributees("themattharris")
val searched: Set[User] = tw4s.searchUsers("TwitterAPI")
noradio.created -> Mon Apr 02 03 47 28 EDT 2007
noradio.description -> "Engineer on Twitter's Timeline Service team. http://t.co/l43j15GWTO \nPast: liberal arts; @rails core; @37signals; http://t.co/UetR47Ibr4; http://t.co/pngt73C50G"
noradio.favorites_count -> 2838
noradio.following -> false
noradio.followRequestSent -> false
noradio.friendCount -> 1125
noradio.geoEnabled -> true
noradio.handle -> noradio
noradio.id -> "3191321"
noradio.isTranslator -> false
noradio.location -> San Francisco, CA
noradio.name -> "Marcel Molina"
noradio.restricted -> false
noradio.statusCount -> 8431
noradio.timeZone -> "Pacific Time (US & Canada)"
noradio.URL -> http://t.co/rMcy6r6jri
noradio.lastStatus -> (Status object)
Lists
val lists: Set[TwitterList] = tw4s.lists("twitterapi")
val memberships: Set[TwitterList] = tw4s.memberships("twitter")
lists.foreach {
l: TwitterList =>
l.created
l.fullName
l.id
l.memberCount
l.mode
l.name
l.owner
l.slug
l.subscriberCount
l.URL
}
Places
val dfPlace: Place = tw4s.place("df51dec6f4ee2b2c")
val places: Set[Place] = tw4s.place(37.76893497, -122.42284884)
val searched: Set[Place] = tw4s.searchPlace("Toronto")
dfPlace.countryCode -> "US"
dfPlace.countryName -> "United States"
dfPlace.fullName -> "Presidio, San Francisco"
dfPlace.placeType -> "neighborhood"
dfPlace.boundingBox -> MULTIPOINT ((37.786925 -122.4891333), (37.8128675 -122.4891333), (37.786925 -122.446306), (37.8128675 -122.446306), (37.786925 -122.4891333))
Suggestions
val users: Set[User] = tw4s.suggestUsers("twitter")
val slugs: Set[String] = tw4s.suggestSlugs()
Favorites
val favorites: Set[Status] = tw4s.favorites()
favorites.foreach {
s: Status =>
s.created
s.id
s.language
s.retweetCount
s.source
s.text
s.hashtags
}
Tweets
val queried: Set[Status] = tw4s.queryTweets("@noradio")
val mentions: Set[Status] = tw4s.mentions
val tweets: Set[Status] = tw4s.tweets()
val retweeted: Set[Status] = tw4s.retweeted()
val retweeters: Set[String] = tw4s.retweeters("327473909412814850")
val tweet: Status = tw4s.getTweet("210462857140252672")
Update calls
Update Profile Information
val newDesc = "My profile's new description"
val newName = "Ada Lovelace"
val newLoc = "Washington, DC"
val newURL = "http://www.example.com"
val updated = tw4s.updateProfile(name = newName, description = newDesc, location = newLoc, url = newURL)
Tweeting/Retweeting
val status: Status = tw4s.tweet("First Tweet!")
val retweet: Status = tw4s.retweet("241259202004267009") // a tweet ID
val deleted = tw4s.deleteTweet(status.id)
val deletedRT = tw4s.deleteTweet(retweet.id)
Following/Unfollowing
val noradio: User = tw4s.follow("noradio")
val deletedFollow: User = tw4s.unfollow(userId = noradio.id)
License, Attribution, etc
TW4S is licensed under the Apache License, version 2. It is in no way associated with Twitter.
I use scribe-java for OAuth. It's an awesome project. I recommend you check it out.