forked from cmu-delphi/fault-record-message-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_scraper.py
83 lines (73 loc) · 3.17 KB
/
slack_scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import argparse
from logzero import logger
import os
import sys
import json
import slack
from dotenv import load_dotenv
from utils import (
get_conversation_history,
read_from_file,
post_fault_record,
post_fault_record_updates,
process_conversation_history,
write_to_file,
update_slack_replies,
)
load_dotenv()
SLACK_TOKEN = os.getenv("SLACK_TOKEN")
MESSAGE_LIMIT_PER_REQUEST = os.getenv("MESSAGE_LIMIT_PER_REQUEST")
FAULT_RECORD_API_URL = os.getenv("FAULT_RECORD_API_URL")
FAULT_RECORD_POST_URL = os.path.join(FAULT_RECORD_API_URL, "api/v1/faults")
FAULT_RECORD_UPDATE_POST_URL = os.path.join(FAULT_RECORD_API_URL, "api/v1/updates")
UPDATE_REPLIES_FOR_DAYS = os.getenv("UPDATE_REPLIES_FOR_DAYS")
def parse_args():
parser = argparse.ArgumentParser(
prog="Fault Record Slack message scraper.",
description="The program scrapes Slack messages for Fault records from different Slack channels.",
)
parser.add_argument("-c", "--channel_id", help="Slack channel ID")
parser.add_argument("-o", "--oldest", default=0, help="Slack message oldest timestamp")
args = parser.parse_args()
if not "-c" or "--channel_id" in sys.argv and not args.cronicle_run:
logger.error("\nSlack channel_id should be provided. Please, check input and try again.\n")
parser.print_help()
sys.exit(1)
return args
def main():
args = parse_args()
channel_id = args.channel_id
oldest_timestamp = args.oldest
oldest_timestamp = read_from_file(f"{channel_id}.txt") or oldest_timestamp
logger.info(
f"Start scraping Slack messages.\nChannel ID: {channel_id}.\tOldest message timestamp: {oldest_timestamp}."
)
client = slack.WebClient(token=SLACK_TOKEN)
try:
logger.info("Start updating slack message replies.")
update_slack_replies(
client, channel_id, FAULT_RECORD_API_URL, UPDATE_REPLIES_FOR_DAYS, FAULT_RECORD_UPDATE_POST_URL
)
logger.info("Slack message replies have been successfully updated.")
except Exception as e:
logger.error(f"Something went wrong while updating Slack replies. \n{e}")
logger.info("Pulling conversation history.")
conversation_history = get_conversation_history(
client=client, channel_id=channel_id, msg_limit=MESSAGE_LIMIT_PER_REQUEST, oldest=oldest_timestamp
)
logger.info("Pulling conversation history has been successfully completed.")
logger.info("Processing conversation history.")
processed_messages, oldest_message_ts = process_conversation_history(
conversation_history, client, channel_id, FAULT_RECORD_API_URL
)
logger.info("Processing conversation history has been susscessfully completed.")
logger.info("Posting messages to fault-record API.")
with open(f"{channel_id}.json", "a") as f:
for message in processed_messages:
f.write(json.dumps(message))
# fault_id = post_fault_record(message, FAULT_RECORD_POST_URL)
# if message.get("updates"):
# post_fault_record_updates(message["updates"], fault_id, FAULT_RECORD_UPDATE_POST_URL)
# write_to_file(f"{channel_id}.txt", oldest_message_ts)
if __name__ == "__main__":
main()