Permalink
Browse files

adding ?search and ?nyt

  • Loading branch information...
1 parent e852710 commit 174f94aa1f73709c81f78a86ba1b61bb9a4f3f60 Robin Walsh committed Sep 8, 2011
@@ -20,7 +20,9 @@ object PluginFinder {
"stfu" -> new ChatterPlugin,
"lunch" -> new YelpPlugin,
"help" -> new HelperPlugin,
- "twitter" -> new TwitterSearchPlugin
+ "twitter" -> new TwitterSearchPlugin,
+ "nyt" -> new NYTimesPlugin,
+ "search" -> new SearchPlugin
)
@@ -6,8 +6,8 @@ import org.jibble.pircbot._
class ScalaBot(name:String, commandChar: Char) extends PircBot {
this.setName(name) // set our IRC name
- //val logger = new MongoLoggerPlugin
- //logger.start()
+ val logger = new MongoLoggerPlugin
+ logger.start()
/* val urlWatcher = new URLWatcherPlugin
urlWatcher.start()*/
@@ -25,6 +25,7 @@ class ScalaBot(name:String, commandChar: Char) extends PircBot {
}
}
+
override def onMessage(channel:String, sender:String, login:String, hostname:String, message:String) {
// called whenever we see a public message in a channel
if ( this.isCommand(message) ) {
@@ -35,14 +36,23 @@ class ScalaBot(name:String, commandChar: Char) extends PircBot {
} else {
// do whatever we need to do on a non-command message.
- // I imagine this will get more intricate over time.
+ // I imagine this will get more intricate over time.
val m = Message(channel, sender, message)
- //val h = new Commander(this, m, channel)
- //h.start()
-
- //logger ! m
+ logger ! m
+ }
+ }
+
+ override def onPrivateMessage(sender:String, login:String, hostname:String, message:String) {
+ if ( this.isCommand(message) ) {
+ val c = parseCommand(message)
+ val commander = new Commander(this, sender, sender, c)
+ commander.start()
+ commander ! c
- // urlWatcher ! h // Zeke said no on this one...
+ } else {
+ // do whatever we need to do on a non-command message.
+ // I imagine this will get more intricate over time.
+ val m = Message(sender, sender, message)
}
}
@@ -0,0 +1,55 @@
+package com.wordnik.irc.plugins
+
+import util.Random
+import net.liftweb.json.DefaultFormats
+import net.liftweb.json.JsonParser._
+import scala.io.Source
+import java.net. {URLEncoder, URLDecoder}
+
+import xml.parsing.ConstructingParser
+
+/*
+ * -rwalsh (via IntelliJ)
+ */
+
+/*
+{
+ "offset": "0",
+ "results": [
+ {
+ "body": "LEAD: SATELLITE monitoring shows that Antarctica has shed more than 11,000 of its 5 million square miles of ice since the 1970's, and scientists say further study is needed to determine whether the findings indicate that global warming is melting polar ice. SATELLITE monitoring shows that Antarctica has shed more than 11,000 of its 5 million square",
+ "byline": "By WALTER SULLIVAN",
+ "date": "19900814",
+ "title": "Antarctica Sheds Ice and Scientists Wonder Why",
+ "url": "http:\/\/www.nytimes.com\/1990\/08\/14\/science\/antarctica-sheds-ice-and-scientists-wonder-why.html"
+ },
+ {
+ */
+
+case class NYTResults ( offset: String, results: List[NYTResult] )
+case class NYTResult ( body: String, byline: String, date: String, title: String, url: String )
+
+class NYTimesPlugin extends GenericPlugin {
+
+ override def help() = { "This will search for articles containing your query and return one of the results." }
+
+ val r = new Random
+ val key = "4eda2bce953f2bc71529351dd028db8b:3:62536233" // this is stupid!
+ val baseUrl = "http://api.nytimes.com/svc/search/v1/article?format=json&api-key=%s&query=".format(key)
+
+ implicit val formats = DefaultFormats // For casting JSON to case classes
+
+ def search(query:String): List[String] = {
+ val nyturl = baseUrl + query
+ val result = parse(Source.fromURL(nyturl).mkString).extract[NYTResults]
+ val choice = r.shuffle(result.results).head
+ val repl = "%s (%s) -> %s".format(choice.title, choice.byline, choice.url)
+ List(repl)
+ }
+
+ override def process(args:List[String]): List[String] = {
+ search(args.mkString("+"))
+ }
+}
+
+// http://api.nytimes.com/svc/search/v1/article?format=json&query=lakes+ice&rank=closest&api-key=####
@@ -0,0 +1,53 @@
+package com.wordnik.irc.plugins
+
+import util.Random
+import net.liftweb.json.DefaultFormats
+import scala.io.Source
+import net.liftweb.json.JsonParser._
+
+/*
+ * -rwalsh (via IntelliJ)
+ */
+
+case class BlekkoResult (
+ DYM: DymObject,
+ ERROR: ErrorObject,
+ RESULT: List[BlekkoSearchResult]
+ )
+case class BlekkoSearchResult (
+ c: Int,
+ display_url: String,
+ n_group: Int,
+ short_host: String,
+ short_host_url: String,
+ snippet: String,
+ url: String,
+ url_title: String
+ )
+case class DymObject()
+case class ErrorObject()
+
+class SearchPlugin extends GenericPlugin {
+ override def help() = { "Web search!" }
+
+ val r = new Random
+ val key = "b425e57b" // this is stupid!
+ val baseUrl = "http://blekko.com/ws/?auth=%s&q=".format(key)
+
+ implicit val formats = DefaultFormats // For casting JSON to case classes
+
+ def search(query:String): List[String] = {
+ val nyturl = baseUrl + query + "+/json"
+ val result = parse(Source.fromURL(nyturl).mkString).extract[BlekkoResult]
+ val choice = r.shuffle(result.RESULT).head
+ val snippet = choice.snippet
+ val url = choice.url
+ List("%s (%s)".format(snippet, new TinyPlugin().tinify(url)))
+ }
+
+ override def process(args:List[String]): List[String] = {
+ search(args.mkString("+"))
+ }
+
+
+}
@@ -27,6 +27,8 @@ case class TwitterResultMetaData( result_type: String )
class TwitterSearchPlugin extends GenericPlugin {
+ override def help() = { "This will search Twitter for your query and return a random one of the results." }
+
val r = new Random
val baseUrl = "http://search.twitter.com/search.json?rpp=100&q="
implicit val formats = DefaultFormats // For casting JSON to case classes

0 comments on commit 174f94a

Please sign in to comment.