Skip to content

Commit

Permalink
adding ?search and ?nyt
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Walsh committed Sep 8, 2011
1 parent e852710 commit 174f94a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 9 deletions.
4 changes: 3 additions & 1 deletion WordnikBot/src/main/scala/com/wordnik/irc/PluginFinder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
26 changes: 18 additions & 8 deletions WordnikBot/src/main/scala/com/wordnik/irc/ScalaBot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()*/
Expand All @@ -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) ) {
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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=####
Original file line number Diff line number Diff line change
@@ -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("+"))
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 174f94a

Please sign in to comment.