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
c485e96a
Commit
c485e96a
authored
Aug 28, 2012
by
Sven Greiner
Browse files
Add url shortener (bitly)
parent
a8cc8ddf
Changes
5
Hide whitespace changes
Inline
Side-by-side
config.xml.skel
View file @
c485e96a
...
...
@@ -9,4 +9,6 @@
<channel>#example</channel>
<channel>#anotherexample</channel>
</channels>
<bitly user="" key="" />
</configuration>
src/main/scala/de/sammyshp/announcebot/AnnounceBot.scala
View file @
c485e96a
...
...
@@ -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
()
{
...
...
src/main/scala/de/sammyshp/announcebot/BitlyUrlShortener.scala
0 → 100644
View file @
c485e96a
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
}
}
}
src/main/scala/de/sammyshp/announcebot/Configuration.scala
View file @
c485e96a
...
...
@@ -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
)
}
...
...
src/main/scala/de/sammyshp/announcebot/PeriodicRssFetcher.scala
View file @
c485e96a
...
...
@@ -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"
)
}
...
...
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