Commit 9ea515b7 authored by Romain Baptiste Dominique Albert's avatar Romain Baptiste Dominique Albert
Browse files

Create the Mattermost bot for the journal club

parent 61acffd0
#!/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" \
"\nPlease 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! ' \
'\nThere 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()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment