Add arguments to restrict to Channel and between timestamps

- Add Optional CHANNEL_ID env
This commit is contained in:
mcdamo 2021-11-30 17:58:51 +11:00
parent a67bed98b1
commit eaf59320e9
2 changed files with 42 additions and 18 deletions

3
.env.example Normal file
View file

@ -0,0 +1,3 @@
SLACK_USER_TOKEN = xoxp-
# Uncomment Channel ID to use for all exports
#CHANNEL_ID =

53
exporter.py Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/env python3
import os import os
import sys import sys
import requests import requests
@ -110,11 +111,13 @@ def channel_list(team_id=None, response_url=None):
) )
def channel_history(channel_id, response_url=None): def channel_history(channel_id, response_url=None, oldest=None, latest=None):
params = { params = {
# "token": os.environ["SLACK_USER_TOKEN"], # "token": os.environ["SLACK_USER_TOKEN"],
"channel": channel_id, "channel": channel_id,
"limit": 200, "limit": 200,
"oldest": oldest,
"latest": latest,
} }
return paginated_get( return paginated_get(
@ -344,6 +347,15 @@ if __name__ == "__main__":
parser.add_argument( parser.add_argument(
"-c", action="store_true", help="Get history for all accessible conversations" "-c", action="store_true", help="Get history for all accessible conversations"
) )
parser.add_argument(
"--ch", help="Restrict to given Channel ID"
)
parser.add_argument(
"--fr", help="Unix timestamp for earliest message"
)
parser.add_argument(
"--to", help="Unix timestamp for latest message"
)
parser.add_argument( parser.add_argument(
"-r", "-r",
action="store_true", action="store_true",
@ -389,21 +401,10 @@ if __name__ == "__main__":
data_replies = "%s\n%s\n\n%s" % (header_str, sep, data_replies) data_replies = "%s\n%s\n\n%s" % (header_str, sep, data_replies)
save(data_replies, "channel-replies_%s" % channel_id) save(data_replies, "channel-replies_%s" % channel_id)
if a.lc: def save_channel(ch_id, ch_list, users):
data = ( ts_fr = a.fr
channel_list() ts_to = a.to
if a.json ch_hist = channel_history(ch_id, oldest=ts_fr, latest=ts_to)
else parse_channel_list(channel_list(), user_list())
)
save(data, "channel_list")
if a.lu:
data = user_list() if a.json else parse_user_list(user_list())
save(data, "user_list")
if a.c:
ch_list = channel_list()
users = user_list()
for ch_id in [x["id"] for x in ch_list]:
ch_hist = channel_history(ch_id)
if a.json: if a.json:
data_ch = ch_hist data_ch = ch_hist
else: else:
@ -419,6 +420,26 @@ if __name__ == "__main__":
save(data_ch, "channel_%s" % ch_id) save(data_ch, "channel_%s" % ch_id)
if a.r: if a.r:
save_replies(ch_hist, ch_id, users) save_replies(ch_hist, ch_id, users)
if a.lc:
data = (
channel_list()
if a.json
else parse_channel_list(channel_list(), user_list())
)
save(data, "channel_list")
if a.lu:
data = user_list() if a.json else parse_user_list(user_list())
save(data, "user_list")
if a.c:
ch_id = a.ch if a.ch else (os.environ["CHANNEL_ID"] if "CHANNEL_ID" in os.environ else None)
ch_list = channel_list()
users = user_list()
if ch_id:
save_channel(ch_id, ch_list, users)
else:
for ch_id in [x["id"] for x in ch_list]:
save_channel(ch_id, ch_list, users)
# elif, since we want to avoid asking for channel_history twice # elif, since we want to avoid asking for channel_history twice
elif a.r: elif a.r:
for ch_id in [x["id"] for x in channel_list()]: for ch_id in [x["id"] for x in channel_list()]: