-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_parse.py
executable file
·110 lines (95 loc) · 3.75 KB
/
slack_parse.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
#
# Copyright (C) 2015 Elana Hashman
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
import json
import re
import sys
import unicodedata
# Import the user id <-> nick dict
users = {}
try:
with open('users.json', 'r') as user_file:
data = json.load(user_file)
for user in data: # preface with '@'
users[user["id"]] = "@%s" % user["name"]
except IOError:
print "Couldn't find the users.json file, terminating."
sys.exit(1)
# Import the channel id <-> channel name dict
channels = {}
try:
with open('channels.json', 'r') as channels_file:
data = json.load(channels_file)
for channel in data: # preface with '#'
channels[channel["id"]] = "#%s" % channel["name"]
except IOError:
print "Couldn't find the channels.json file, terminating."
sys.exit(1)
# Usage info
if len(sys.argv) <= 1 or sys.argv[1] == "--help":
print "Usage: slack_export filename_to_parse.json"
sys.exit(0)
# Helpers
def timestamp(value):
"Turns a decimal UNIX timestamp into an 'HH:MM' string"
# "- 18000" <-- sketchy timezone conversion to EST
ts = datetime.fromtimestamp(int(float(value)) - 18000)
return datetime.strftime(ts, "%H:%M")
def parse_line(line):
"Parses a line of JSON into a line of text"
line_type = line["type"]
if line_type == "message":
subtype = line.get("subtype")
if subtype == "bot_message":
# TODO: Not implemented
return
elif subtype == "channel_purpose":
print "%s -!- Channel Purpose: %s" % \
(timestamp(line["ts"]), replace_with_special(line["text"]))
elif subtype == "channel_join":
print "%s -!- %s has joined the channel" % (timestamp(line["ts"]),
users[line["user"]])
elif subtype == "channel_topic" or subtype == "file_share":
print "%s -!- %s" % (timestamp(line["ts"]),
replace_with_special(line["text"]))
else:
print "%s <%s> %s" % (timestamp(line["ts"]),
users[line["user"]],
replace_with_special(line["text"]))
def replace_with_special(text):
"Substitutes user ids with nicks, channel ids with channels, escaped chars"
unicode_pass = \
unicodedata.normalize("NFKD", text).encode("ascii", "ignore")
users_pass = re.sub(r"<@(U[0-9A-Z]{8})(\|[^>]*)?>",
lambda x: users[x.group(1)],
unicode_pass)
channels_pass = re.sub(r"<#(C[0-9A-Z]{8})>",
lambda x: channels[x.group(1)],
users_pass)
amp_pass = re.sub(r"&", "&", channels_pass)
lt_pass = re.sub(r"<", "<", amp_pass)
gt_pass = re.sub(r">", ">", lt_pass)
return re.sub("\n", " ", gt_pass)
# "main": Load the file and give it a go, print to stdout
try:
raw_log = open(sys.argv[1], 'r')
data = json.load(raw_log)
for line in data:
parse_line(line)
except IOError:
print "Failed to open file " + sys.argv[1] + ", terminating."
sys.exit(1)