Skip to content
Snippets Groups Projects
Commit ffca6e99 authored by Sven Greiner's avatar Sven Greiner
Browse files

Better shortener integration

parent c485e96a
Branches master
No related tags found
No related merge requests found
......@@ -15,11 +15,16 @@ class AnnounceBot(config: Configuration) extends PircBot {
onDisconnect()
val urlShortener = new BitlyUrlShortener(config.bitlyUser, config.bitlyKey)
val urlFilter = (config.bitlyUser, config.bitlyKey) match {
case (Some(user), Some(key)) => {
val urlShortener = new BitlyUrlShortener(user, key)
(s: String) => { urlShortener.shorten(s) }
}
case _ => (s: String) => s
}
new PeriodicRssFetcher(config.url, config.pollInterval,
(s: String) => { stats.incAnnounces(); config.channels.map(sendMessage(_, s)) },
(s: String) => { urlShortener.shorten(s) }) start
(s: String) => { stats.incAnnounces(); config.channels.map(sendMessage(_, s)) }, urlFilter) start
}
override def onConnect() {
......@@ -36,7 +41,7 @@ class AnnounceBot(config: Configuration) extends PircBot {
override def onDisconnect() {
if (wasConnected)
Log.e("Lost connection to server")
Log.w("Lost connection to server")
Log.d("Connect to server")
......@@ -51,6 +56,7 @@ class AnnounceBot(config: Configuration) extends PircBot {
Log.e("Cannot connect to server:\n " + e)
Log.d("Try reconnect in 1 minute.")
Thread sleep 60 * 1000
wasConnected = false
onDisconnect()
}
}
......
......@@ -8,8 +8,14 @@ class BitlyUrlShortener(user: String, key: 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
case Some(s) => {
Log.w("Shortened " + url + " to " + s)
s
}
case None => {
Log.w("Cannot shorten url")
url
}
}
}
......
......@@ -8,8 +8,8 @@ class Configuration(
val identify: Option[String],
val url: String,
val pollInterval: Long,
val bitlyUser: String,
val bitlyKey: String) {
val bitlyUser: Option[String],
val bitlyKey: Option[String]) {
}
object Configuration {
......@@ -40,8 +40,14 @@ object Configuration {
case i => i.toLong
}
val bitlyUser = xml \ "bitly" \ "@user" text
val bitlyKey = xml \ "bitly" \ "@key" text
val bitlyUser = xml \ "bitly" \ "@user" text match {
case "" => None
case s => Some(s)
}
val bitlyKey = xml \ "bitly" \ "@key" text match {
case "" => None
case s => Some(s)
}
new Configuration(host, port, channels, nick, identify, url, pollInterval, bitlyUser, bitlyKey)
} catch {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment