Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Institute of Experimental Physics
Quantum Circuits
python-repo
Commits
9ea515b7
Commit
9ea515b7
authored
Jun 29, 2020
by
Romain Baptiste Dominique Albert
Browse files
Create the Mattermost bot for the journal club
parent
61acffd0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
138 additions
and
0 deletions
+138
-0
FridgeWatch/mattermostBot.py
FridgeWatch/mattermostBot.py
+138
-0
No files found.
FridgeWatch/mattermostBot.py
0 → 100644
View file @
9ea515b7
#!/opt/anaconda3/bin/python
# Libraries ###################################################################
import
time
import
os
import
pandas
as
pd
import
feedparser
import
requests
import
pickle
import
datetime
import
threading
from
numpy
import
random
###############################################################################
# Channel List ################################################################
journalclub_channel
=
'journal-club'
###############################################################################
# Zotero and Mattermost parameters #################################################
zotero_feed
=
"https://api.zotero.org/groups/571933/collections/7Z7PM7ZV/"
\
"items/top?start=0&limit=25&format=atom&v=1&key=S1e75wqNDksBTiE"
\
"VoCJb5wQU"
journal_club_webhook
=
'https://mm.iqoqi.at/hooks/8gci4zt5hfgyxpiax5gfa8ruew'
name_file
=
'/home/pi/Logging/GroupMembers'
name_list
=
[
line
.
rstrip
()
for
line
in
open
(
name_file
)]
###############################################################################
# Bots ########################################################################
class
JournalClubHost
(
object
):
"""
Bot who announces papers for Journal Club
Parameters:
channel: ['String'] Channel name in which the bot should post
zotero_feed: ['String (url)'] RSS Feed of the Papers for current
week
webhook: ['String (url)'] URL for the mattermost webhook
"""
def
__init__
(
self
,
channel
,
zotero_feed
,
webhook
):
self
.
_channel
=
channel
self
.
_webhook
=
webhook
self
.
_zfeed
=
zotero_feed
self
.
_running
=
True
self
.
_known_paper_file
=
'/home/pi/Logging/JournalClub/presented_papers.pickle'
def
start
(
self
):
t
=
threading
.
Thread
(
target
=
self
.
run
)
t
.
start
()
def
terminate
(
self
):
self
.
_running
=
False
def
announce
(
self
,
name
,
title
,
author
,
group
):
msg
=
"It is my pleasure to announce that *{name}* will talk in the "
\
"next Journal Club about the paper from the {group} group: "
\
"
\n
_{author}_:
\"
{title}
\"\n
"
\
"
\n
Please have a look at the paper. You can find it in Zotero "
\
"in the folder <Journal Club/Papers for Current Week>"
json_data
=
{
"channel"
:
self
.
_channel
,
"username"
:
"JournalClubHost"
,
"text"
:
msg
.
format
(
name
=
name
,
title
=
title
,
author
=
author
,
group
=
group
)
}
requests
.
post
(
self
.
_webhook
,
json
=
json_data
)
def
remind
(
self
):
name1
=
name_list
[
random
.
randint
(
0
,
len
(
name_list
))]
res_names
=
[
x
for
x
in
name_list
if
x
!=
name1
]
name2
=
res_names
[
random
.
randint
(
0
,
len
(
res_names
))]
msg
=
'Hello Group. Unfortunately, there are no papers for this weeks'
\
' Journal Club. Everyone, please have a look at interesting '
\
'papers and add at least one! '
\
'
\n
There is so much to discover! :)
\n
'
\
'How about you {name1}? Or {name2}?'
.
format
(
name1
=
name1
,
name2
=
name2
)
json_data
=
{
"channel"
:
self
.
_channel
,
"username"
:
"JournalClubHost"
,
"icon_emoji"
:
":squirrel"
,
"text"
:
msg
}
requests
.
post
(
self
.
_webhook
,
json
=
json_data
)
def
run
(
self
):
while
self
.
_running
:
# Request Paper List every hour
feed
=
feedparser
.
parse
(
self
.
_zfeed
)
# Go through entries and check if paper is already known
if
os
.
path
.
exists
(
self
.
_known_paper_file
):
with
open
(
self
.
_known_paper_file
,
'rb'
)
as
f
:
known_papers
=
pickle
.
load
(
f
)
else
:
known_papers
=
[]
# Remind if it is Thursday and there are still no papers
now
=
datetime
.
datetime
.
now
()
if
not
feed
.
entries
and
now
.
weekday
()
==
2
and
now
.
hour
==
10
:
self
.
remind
()
for
e
in
feed
.
entries
:
name
=
e
.
authors
[
0
].
name
# Name of the person who added it
title
=
e
.
title
# Title of the paper
try
:
author
=
e
.
zapi_creatorsummary
# Name of the author of the
# paper (bug in feed)
# Parse html to list
html_list
=
pd
.
read_html
(
e
.
summary
)
authors
=
list
(
html_list
[
0
][
1
][
html_list
[
0
][
0
]
==
'Author'
])
group
=
authors
[
-
1
]
except
:
author
=
'None'
group
=
'None'
paper_key
=
e
.
zapi_key
# Key of the paper
# Announce paper if it is new
if
paper_key
not
in
known_papers
:
self
.
announce
(
name
,
title
,
author
,
group
)
known_papers
.
append
(
paper_key
)
# Else do nothing
# Save known papers
with
open
(
self
.
_known_paper_file
,
'wb'
)
as
f
:
pickle
.
dump
(
known_papers
,
f
)
# Sleep for one hour
time
.
sleep
(
60
*
60
)
jch
=
JournalClubHost
(
journalclub_channel
,
zotero_feed
,
journal_club_webhook
)
if
__name__
==
'__main__'
:
jch
.
start
()
Write
Preview
Markdown
is supported
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