Commit c485e96a authored by Sven Greiner's avatar Sven Greiner
Browse files

Add url shortener (bitly)

parent a8cc8ddf
......@@ -9,4 +9,6 @@
<channel>#example</channel>
<channel>#anotherexample</channel>
</channels>
<bitly user="" key="" />
</configuration>
......@@ -15,8 +15,11 @@ class AnnounceBot(config: Configuration) extends PircBot {
onDisconnect()
val urlShortener = new BitlyUrlShortener(config.bitlyUser, config.bitlyKey)
new PeriodicRssFetcher(config.url, config.pollInterval,
(s: String) => { stats.incAnnounces(); config.channels.map(sendMessage(_, s)) }) start
(s: String) => { stats.incAnnounces(); config.channels.map(sendMessage(_, s)) },
(s: String) => { urlShortener.shorten(s) }) start
}
override def onConnect() {
......
package de.sammyshp.announcebot
import java.net.{URLEncoder, URL}
import java.io.{BufferedReader, InputStreamReader}
class BitlyUrlShortener(user: String, key: String) {
def shorten(url: String) = {
val apiCall = "http://api.bitly.com/v3/shorten?login=%s&apiKey=%s&longUrl=%s&format=txt&domain=j.mp".format(user, key, URLEncoder.encode(url, "UTF-8"))
httpGetLine(apiCall) match {
case Some(s) => s
case None => url
}
}
private def httpGetLine(url: String) = {
try {
val in = new BufferedReader(new InputStreamReader(new URL(url).openStream()))
val result = in.readLine() match {
case null => None
case s => Some(s)
}
in.close()
result
} catch {
case e => None
}
}
}
......@@ -7,7 +7,9 @@ class Configuration(
val nick: String,
val identify: Option[String],
val url: String,
val pollInterval: Long) {
val pollInterval: Long,
val bitlyUser: String,
val bitlyKey: String) {
}
object Configuration {
......@@ -33,12 +35,15 @@ object Configuration {
val url = xml \ "feed" text
val pollInterval = xml \ "feed" \ "interval" text match {
val pollInterval = xml \ "feed" \ "@interval" text match {
case "" => 60000
case i => i.toLong
}
new Configuration(host, port, channels, nick, identify, url, pollInterval)
val bitlyUser = xml \ "bitly" \ "@user" text
val bitlyKey = xml \ "bitly" \ "@key" text
new Configuration(host, port, channels, nick, identify, url, pollInterval, bitlyUser, bitlyKey)
} catch {
case e => throw new ConfigurationException(e.toString)
}
......
......@@ -4,7 +4,7 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
class PeriodicRssFetcher(url: String, pollInterval: Long, callback: String => Unit) extends Thread {
class PeriodicRssFetcher(url: String, pollInterval: Long, callback: String => Unit, urlFilter: String => String) extends Thread {
val dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US)
var lastMessageTime: Long = 0;
......@@ -39,7 +39,7 @@ class PeriodicRssFetcher(url: String, pollInterval: Long, callback: String => Un
+ " im Thema \""
+ x._1
+ "\": "
+ x._2))
+ urlFilter(x._2)))
} else {
Log.d("First fetch, no announce")
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment