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
La Quadrature du Net
rpteam
wantzel
Commits
7ead8819
Commit
7ead8819
authored
Sep 15, 2018
by
Mindiell
Browse files
Wantzel can now monitor mediawikis and gitlab groups
parent
cebf2f42
Changes
2
Hide whitespace changes
Inline
Side-by-side
monitor.py
View file @
7ead8819
...
...
@@ -10,25 +10,33 @@ import time
import
config
from
logs
import
Log
class
Wiki
():
class
Monitor
():
def
__init__
(
self
,
name
,
url
):
#
wiki
's name
#
Monitor
's name
self
.
name
=
name
# base url
self
.
url
=
url
# default last_entry_published
self
.
last_entry_updated
=
time
.
strptime
(
"2000-01-01
"
,
"%Y-%m-%d
"
)
self
.
last_entry_updated
=
time
.
strptime
(
"2000-01-01
00:00:00 CET"
,
"%Y-%m-%d %H:%M:%S %Z
"
)
# See if there is a later last_entry_published for wiki
connection
=
sqlite3
.
connect
(
config
.
sqlite_db
)
for
row
in
connection
.
execute
(
"SELECT last_entry_updated FROM
wiki
s WHERE name=?"
,
"SELECT last_entry_updated FROM
monitor
s WHERE name=?"
,
(
self
.
name
,)
):
self
.
last_entry_updated
=
time
.
strptime
(
row
[
0
].
encode
(
"utf-8"
),
"%Y-%m-%d %H:%M:%S %Z"
)
Log
.
debug
(
"Dernière mise à jour du wiki: %s"
%
self
.
last_entry_updated
)
# In case there is no entry for this monitor in database
if
self
.
last_entry_updated
==
time
.
strptime
(
"2000-01-01 00:00:00 CET"
,
"%Y-%m-%d %H:%M:%S %Z"
):
last_entry_updated
=
time
.
strftime
(
"%Y-%m-%d %H:%M:%S %Z"
,
self
.
last_entry_updated
)
connection
.
execute
(
"INSERT INTO monitors VALUES (?,?)"
,
(
last_entry_updated
,
self
.
name
))
connection
.
commit
()
Log
.
debug
(
"Dernière mise à jour de %s: %s"
%
(
self
.
name
,
self
.
last_entry_updated
))
def
set_last_entry_updated
(
self
,
last_entry_updated
):
self
.
last_entry_updated
=
last_entry_updated
...
...
@@ -38,44 +46,81 @@ class Wiki():
)
connection
=
sqlite3
.
connect
(
config
.
sqlite_db
)
connection
.
execute
(
"UPDATE
wiki
s SET last_entry_updated=? WHERE name=?"
,
"UPDATE
monitor
s SET last_entry_updated=? WHERE name=?"
,
(
last_entry_updated
,
self
.
name
)
)
connection
.
commit
()
class
Monitor
():
def
update
(
self
):
pass
class
Mediawiki
(
Monitor
):
def
update
(
self
):
url
=
self
.
url
+
"api.php?days=1&limit=50&translations=filter&action=feedrecentchanges&feedformat=atom"
now
=
time
.
localtime
()
today
=
time
.
strptime
(
"%s-%s-%s %s"
%
(
now
.
tm_year
,
now
.
tm_mon
,
now
.
tm_mday
,
time
.
tzname
[
0
]
),
"%Y-%m-%d %Z"
)
entries
=
feedparser
.
parse
(
url
)[
'entries'
]
for
entry
in
entries
:
# if date of update is greater than today midnight
if
today
<
entry
.
updated_parsed
:
if
self
.
last_entry_updated
<
entry
.
updated_parsed
:
# Save last_entry_published
self
.
set_last_entry_updated
(
entry
.
updated_parsed
)
# Sending monitoring on working chan
return
"""[%s] %s a mis à jour la page %s => %s"""
%
(
self
.
name
,
entry
.
author
.
encode
(
"utf-8"
),
entry
.
title
.
encode
(
"utf-8"
),
entry
.
link
.
encode
(
"utf-8"
),
)
class
GitlabGroup
(
Monitor
):
def
update
(
self
):
url
=
self
.
url
[:
-
1
]
+
".atom"
now
=
time
.
localtime
()
today
=
time
.
strptime
(
"%s-%s-%s %s"
%
(
now
.
tm_year
,
now
.
tm_mon
,
now
.
tm_mday
,
time
.
tzname
[
0
]
),
"%Y-%m-%d %Z"
)
entries
=
feedparser
.
parse
(
url
)[
'entries'
]
for
entry
in
entries
:
# if date of update is greater than today midnight
if
today
<
entry
.
updated_parsed
:
if
self
.
last_entry_updated
<
entry
.
updated_parsed
:
# Save last_entry_published
self
.
set_last_entry_updated
(
entry
.
updated_parsed
)
# Sending monitoring on working chan
return
"""[%s] %s => %s"""
%
(
self
.
name
,
entry
.
title
.
encode
(
"utf-8"
),
entry
.
link
.
encode
(
"utf-8"
),
)
class
Monitoring
():
def
__init__
(
self
):
# List of wikis to monitor
self
.
wikis
=
[]
for
wiki
in
config
.
wikis
[
"mediawiki"
]:
self
.
wikis
.
append
(
Wiki
(
wiki
[
"name"
],
wiki
[
"url"
]))
# List of objects to monitor
self
.
monitors
=
[]
for
monitor
in
config
.
monitors
:
if
monitor
[
"type"
]
==
"mediawiki"
:
self
.
monitors
.
append
(
Mediawiki
(
monitor
[
"name"
],
monitor
[
"url"
]))
elif
monitor
[
"type"
]
==
"gitlab_group"
:
self
.
monitors
.
append
(
GitlabGroup
(
monitor
[
"name"
],
monitor
[
"url"
]))
def
wiki_
update
s
(
self
):
def
update
(
self
):
"""
This method loops over each
wiki to monitor
.
This method loops over each
monitor to see if something has changed
.
"""
messages
=
[]
for
wiki
in
self
.
wikis
:
url
=
wiki
.
url
+
"api.php?days=1&limit=50&translations=filter&action=feedrecentchanges&feedformat=atom"
now
=
time
.
localtime
()
today
=
time
.
strptime
(
"%s-%s-%s %s"
%
(
now
.
tm_year
,
now
.
tm_mon
,
now
.
tm_mday
,
time
.
tzname
[
0
]
),
"%Y-%m-%d %Z"
)
entries
=
feedparser
.
parse
(
url
)[
'entries'
]
for
entry
in
entries
:
# if date of update is greater than today midnight
if
today
<
entry
.
updated_parsed
:
if
wiki
.
last_entry_updated
<
entry
.
updated_parsed
:
# Sending monitoring on working chan
messages
.
append
(
"""[%s] %s a mis à jour la page %s => %s"""
%
(
wiki
.
name
,
entry
.
author
.
encode
(
"utf-8"
),
entry
.
title
.
encode
(
"utf-8"
),
entry
.
link
.
encode
(
"utf-8"
),
))
# Save last_entry_published
wiki
.
set_last_entry_updated
(
entry
.
updated_parsed
)
for
monitor
in
self
.
monitors
:
message
=
monitor
.
update
()
if
message
is
not
None
:
messages
.
append
(
message
)
return
messages
wantzel.py
View file @
7ead8819
...
...
@@ -17,7 +17,7 @@ from logs import Log
# bot modules
from
admin
import
Admin
from
fun
import
fun
from
monitor
import
Monitor
from
monitor
import
Monitor
ing
from
rp
import
Rp
class
Wantzel
(
object
):
...
...
@@ -29,7 +29,7 @@ class Wantzel(object):
Initialization of all utility objects and bot over IRC.
"""
self
.
admin
=
Admin
()
self
.
monitor
=
Monitor
()
self
.
monitor
=
Monitor
ing
()
self
.
rp
=
Rp
()
# Connection to IRC
self
.
irc
=
IrcClientFactory
(
config
)
...
...
@@ -57,7 +57,7 @@ class Wantzel(object):
if
topic
!=
self
.
topic
:
self
.
irc
.
client
.
topic
(
config
.
RP_CHANNEL
,
topic
)
# Tell on channel if a wiki was modified since last time
self
.
send_message
(
"#lqdn-travail"
,
self
.
monitor
.
wiki_
update
s
())
self
.
send_message
(
config
.
MONITOR_CHANNEL
,
self
.
monitor
.
update
())
# Cleaning points of mastering rp
self
.
rp
.
clean_master_rp
()
...
...
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