-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robin Walsh
committed
Sep 8, 2011
1 parent
e852710
commit 174f94a
Showing
5 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
WordnikBot/src/main/scala/com/wordnik/irc/plugins/NYTimesPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=#### |
53 changes: 53 additions & 0 deletions
53
WordnikBot/src/main/scala/com/wordnik/irc/plugins/SearchPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("+")) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters