Skip to content

Commit

Permalink
Merge pull request #13 from ScilifelabDataCentre/slack-daily
Browse files Browse the repository at this point in the history
Add a task for posting new jobs to #jobs on Slack
  • Loading branch information
talavis authored Nov 28, 2022
2 parents e22c37d + cbe79d7 commit c3e7fe8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
certifi==2022.6.15
beautifulsoup4==4.11.1
certifi==2022.9.24
chardet==5.0.0
charset-normalizer==2.1.0
cycler==0.11.0
Expand All @@ -7,6 +8,7 @@ et-xmlfile==1.1.0
fonttools==4.35.0
idna==3.3
kiwisolver==1.4.4
lxml==4.9.1
matplotlib==3.5.3
numpy==1.23.2
openpyxl==3.0.10
Expand All @@ -19,7 +21,8 @@ python-dateutil==2.8.2
pytz==2022.2.1
requests==2.28.1
six==1.16.0
soupsieve==2.3.2.post1
tenacity==8.0.1
urllib3==1.26.11
urllib3==1.26.13
wordcloud==1.8.2.2
zope.interface==5.4.0
2 changes: 2 additions & 0 deletions runner_daily.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ PYTHONPATH="$CODE_PATH"/covid-portal-visualisations/map python "$CODE_PATH"/covi
for filename in $(ls $CODE_PATH/output) ; do
curl "https://blobserver.dckube.scilifelab.se/blob/$filename" -H "x-accesskey: $ACCESS_KEY" --upload-file "output/$filename"
done

python "$CODE_PATH"/slack_daily.py
105 changes: 105 additions & 0 deletions slack_daily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python3

# Expects a bot token in SLACK_BOT_TOKEN
# Expects a user token in SLACK_USER_TOKEN

import datetime
import os

import lxml
import requests
from bs4 import BeautifulSoup

SLL_FEED = "https://www.scilifelab.se/feed/"
FEED_DATE_FORMAT = "%a, %d %b %Y %H:%M:%S %z"


def gen_feed_payload(start_msg: str, entries: list, channel: str) -> dict:
"""
Generate a payload with blocks formatting and text.
Args:
start_msg (str): Message to show before listing entries.
entries (list): The entries to include (str).
channel (str): The ID of the channel to post in.
Returns:
dict: The generated payload.
"""
payload = {
"channel": channel,
"unfurl_links": False,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*{start_msg}*",
},
},
{"type": "divider"},
],
}

for entry in entries:
payload["blocks"].append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": entry,
},
}
)

return payload


def post_to_slack(payload: dict):
"""
Post a message to Slack.
Args:
payload (dict): Data to send.
"""
API_URL = "https://slack.com/api/chat.postMessage"
res = requests.post(
API_URL, headers={"Authorization": f"Bearer {os.environ.get('SLACK_TOKEN')}"}, json=payload
)
print(res)
print(res.json())


def check_scilifelab_jobs(max_age=24):
"""
Check for new jobs in the SciLifeLab jobs event feed.
Args:
max_age (int): Maximum time since the job was posted (hours).
"""
feed_req = requests.get(SLL_FEED)
soup = BeautifulSoup(feed_req.text, features="xml")
time_limit = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=max_age)

entries = []
for entry in soup.find_all("item"):
timestamp = datetime.datetime.strptime(entry.pubDate.text, FEED_DATE_FORMAT)
if timestamp > time_limit:
# the feed is a mix of everything, looking for /career/ in the link seems to work
if "/career/" in entry.link.text:
entries.append(
f":scilife: *{entry.title.text}*\n <{entry.link.text}|More information>\n"
)
if entries:
if len(entries) > 1:
start_msg = "New jobs posted on the <https://www.scilifelab.se/careers/|careers page>!"
else:
start_msg = "New job posted on the <https://www.scilifelab.se/careers/|careers page>!"
# G019SN15M1T = #dc-dev-experimentation
# C01LM5A7RUN = #jobs
msg = gen_feed_payload(start_msg, entries, "C01LM5A7RUN")

post_to_slack(msg)


check_scilifelab_jobs(24)

0 comments on commit c3e7fe8

Please sign in to comment.