Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Sven Greiner
finf-ScalaAnnounceBot
Commits
ffca6e99
Commit
ffca6e99
authored
Aug 29, 2012
by
Sven Greiner
Browse files
Better shortener integration
parent
c485e96a
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/scala/de/sammyshp/announcebot/AnnounceBot.scala
View file @
ffca6e99
...
...
@@ -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
()
}
}
...
...
src/main/scala/de/sammyshp/announcebot/BitlyUrlShortener.scala
View file @
ffca6e99
...
...
@@ -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
}
}
}
...
...
src/main/scala/de/sammyshp/announcebot/Configuration.scala
View file @
ffca6e99
...
...
@@ -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
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment