【Scala】Scala&Play FrameworkでTwitter4jをいじってみる Part5【Twitter4j】
前回の続きです。今回で完成させていきます。
routes
GET / controllers.HomeController.index GET /search controllers.SearchTweetsController.search
ルーティングは上記のとおりになっています。
HomeControllerは初期画面表示を行い、SearchTweetsControllerはTweetの検索処理と検索結果画面表示を行います。
Controllers
HomeController
package controllers import javax.inject._ import forms.SearchTweets import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.{ I18nSupport, MessagesApi } import play.api.mvc._ import play.api.data.format.Formats._ @Singleton class HomeController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport { private val searchTweetsForm: Form[SearchTweets] = Form { mapping( "lat" -> of[Double], "lng" -> of[Double], "range" -> of[Double], "keyword" -> nonEmptyText )(SearchTweets.apply)(SearchTweets.unapply) } def index = Action { implicit request => Ok(views.html.index(searchTweetsForm, Nil)) } }
こちらの、HomeControllerは、初期画面表示を行います。
searchTweetsFormをマッピングしています。
indexメソッドで、初期画面表示を行っています。
SearchTweetsController
package controllers import javax.inject.{ Inject, Singleton } import forms.SearchTweets import models.Tweet import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.{ I18nSupport, MessagesApi } import play.api.mvc.{ Action, AnyContent, Controller } import twitter4j._ import play.api.data.format.Formats._ @Singleton class SearchTweetsController @Inject()( val messagesApi: MessagesApi, ) extends Controller with I18nSupport { private val searchTweetsForm: Form[SearchTweets] = Form { mapping( "lat" -> of[Double], "lng" -> of[Double], "range" -> of[Double], "keyword" -> nonEmptyText )(SearchTweets.apply)(SearchTweets.unapply) } def search(): Action[AnyContent] = Action { implicit request => val twitter = new TwitterFactory().getInstance() val query = new Query() val form = searchTweetsForm.bindFromRequest.get val geo = new GeoLocation(form.lat, form.lng) query.setGeoCode(geo, form.range, Query.KILOMETERS) query.setQuery(form.keyword) query.setCount(100) var result: QueryResult = null try { result = twitter.search(query) } catch { case e: TwitterException => println("error") } val itr = result.getTweets().iterator() var tweetList: List[Tweet] = Nil while (itr.hasNext()) { val status = itr.next() val tweet = new Tweet(status.getText, status.getUser.getProfileImageURL) tweetList = tweetList.::(tweet) } Ok(views.html.index(searchTweetsForm, tweetList)) } }
こちらは、Tweetの検索処理を行います。
twitter4jの使い方などは、そこらじゅうに情報が転がっているので、説明はしません。
まとめ
とりあえず、ScalaとPlay Frameworkを使って、簡単なアプリを作ってみました。
さらなる学習のために、このアプリをより多機能にしたりしていきたいとおもいます。
【Scala】Scala&Play FrameworkでTwitter4jをいじってみる Part4【Twitter4j】
前回の続きです。
今回はモデルについて書いていきます。
Tweet.scala
package models case class Tweet(text: String, profileImg: String)
こちらは、検索結果画面に表示するTweetを表すクラスです。
Tweetを最大100件取得し、このオブジェクトに値を詰めていきます。
次回で完成させていきます。
【Scala】Scala&Play FrameworkでTwitter4jをいじってみる Part3【Twitter4j】
前回の続きです。
今回は、build.sbtなどの設定ファイルの詳細について書いていきます。
build.sbt
name := みなさんが決めたやつ organization := みなさんが決めたやつ version := "1.0.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayScala) scalaVersion := "2.11.11" libraryDependencies ++= Seq( "com.adrianhurt" %% "play-bootstrap" % "1.1-P25-B3", "org.twitter4j" % "twitter4j-core" % "4.0.6", "org.twitter4j" % "twitter4j-stream" % "4.0.6" ) TwirlKeys.templateImports ++= Seq("forms._")
build.sbtはこんな感じです。
libraryDependenciesには、見た目を最低限整えるために、play-bootstrapを入れています。
また、twitter4jのライブラリも追加しています。
twitter4j.properties
debug=true oauth.consumerKey=発行してゲットしたやつ oauth.consumerSecret=発行してゲットしたやつ oauth.accessToken=発行してゲットしたやつ oauth.accessTokenSecret=発行してゲットしたやつ twitter4j.loggerFactory=twitter4j.NullLoggerFactory
twitter4j.propertiesを作成し、confフォルダの中に格納します。
consumerKeyなどの取得方法は、広大なネットにいくらでもあります。
次回は、controllerの中身などについて書いていきたいと思います。
【Scala】コップ本の自分用メモ2
集合とマップの作成・初期化・操作
// イミュータブルな集合の作成・初期化・操作 var footballSet = Set("Messi", "CR7") footballSet += "Pogba" println(footballSet.contains("Neymar")) // ミュータブルな集合の作成・初期化・操作 import scala.collection.mutable val footballSet2 = mutable.Set("Honda", "Kagawa") footballSet2 += "Okazaki" println(footballSet2) // ミュータブルマップ import scala.collection.mutable val footballMap = mutable.Map[Int, String]() footballMap += (1 -> "Messi") footballMap += (2 -> "Suarez") footballMap += (3 -> "Neymar") // イミュータブルマップ val footballMap2 = Map(1 -> "Pogba", 2 -> "Hazard", 3 -> "Reus")