Skip to content
Snippets Groups Projects
Commit 0cd9f456 authored by Johann Jacobsohn's avatar Johann Jacobsohn
Browse files

initial import

parent b4e4c1f0
No related branches found
No related tags found
No related merge requests found
# papersurfer # Papersurfer
Automatically download paper list from Mattermost channel and display.
![](screencast.gif)
""""Paper surfer - browse papers posted on the mattermost channel.
UI:
[____(filter)______]
1. paper (open discussion) (open paper)
2. paper (open discussion) (open paper)
3. paper (open discussion) (open paper)
4. paper (open discussion) (open paper)
"""
from dataclasses import dataclass
from mattermostdriver import Driver
import urwid
import configargparse
URL = "mattermost.cen.uni-hamburg.de"
CHANNEL = "n5myem9yc7fyzb9am7ym5o41ry"
@dataclass
class PostDTO:
""""Encapsulate Mattermost Posts."""
message: str
class Mattermost:
"""Provide a simplified interaction w/ mattermost api."""
def __init__(self, username, password):
self.mattermost = Driver({
'url': URL,
'login_id': username,
'password': password,
'port': 443
})
self.mattermost.login()
def retrieve_all_messages(self):
"""Retrieve all messages from mattermost, unfiltered for papers."""
posts = self.mattermost.posts.get_posts_for_channel(CHANNEL)
return [PostDTO(m['message']) for m in posts['posts'].values()]
def filter_incoming(self, posts):
"""Filter messages from mattermost to only papers."""
return [p for p in posts if "doi" in p.message]
def retrieve(self):
"""Retrieve papers from mattermost channel."""
msgs = self.retrieve_all_messages()
self.msgs = self.filter_incoming(msgs)
return self.msgs
def get_filtered(self, needle):
"""Filter posts by needle."""
return [m for m in self.msgs if needle.lower() in m.message.lower()]
class Papersurfer:
"""Provide UI and interface with mattermost class."""
def __init__(self, username, password):
palette = [('I say', 'default,bold', 'default', 'bold')]
ask = urwid.Edit(('I say', u"Filter?\n"))
exitbutton = urwid.Button(u'Exit')
div = urwid.Divider(u'-')
self.mtm = Mattermost(username, password)
papers = [msg.message for msg in self.mtm.retrieve()]
body = []
for paper in papers:
button = urwid.Button(paper)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
self.listcontent = urwid.SimpleFocusListWalker(body)
paperlist = urwid.BoxAdapter(urwid.ListBox(self.listcontent), 40)
pile = urwid.Pile([ask, div, paperlist, div, exitbutton])
top = urwid.Filler(pile, valign='middle')
urwid.connect_signal(ask, 'change', self.onchange)
urwid.connect_signal(exitbutton, 'click', self.on_exit_clicked)
urwid.MainLoop(top, palette).run()
def onchange(self, _, new_edit_text):
"""Handle filter change."""
papers = [msg.message for msg in self.mtm.get_filtered(new_edit_text)]
self.listcontent.clear()
for paper in papers:
button = urwid.Button(paper)
self.listcontent.append(urwid.AttrMap(button, None,
focus_map='reversed'))
def on_exit_clicked(self, button):
"""Handle exitbutton click and exit."""
raise urwid.ExitMainLoop()
def parse_args():
"""Parse command line arguments and config file."""
parser = configargparse.ArgParser()
parser.add("-w", "--write-out-config-file",
help="takes the current command line args and writes them out "
"to a config file at the given path",
is_write_out_config_file_arg=True)
parser.add('-c', '--my-config', required=False, is_config_file=True,
help='config file path', default='papersurfer.conf')
parser.add('-u', '--username', required=True, help='Mattermost username',
default="USERNAME")
parser.add('-p', '--password', required=True, help='Mattermost password',
default="PASSWORD")
options = parser.parse_args()
return options.username, options.password
if __name__ == "__main__":
USERNAME, PASSWORD = parse_args()
Papersurfer(USERNAME, PASSWORD)
screencast.gif

437 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment