Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ conf/**/*credentials*
# ignore everything in the following folders
data/**
logs/**

anonymized/
# except their sub-folders
!data/**/
!logs/**/
Expand Down
26 changes: 19 additions & 7 deletions notebooks/parse_slack_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -33,17 +33,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<src.loader.SlackDataLoader object at 0x000001ABB280D210>\n"
]
}
],
"source": [
"# Add parent directory to path to import modules from src\n",
"rpath = os.path.abspath('..')\n",
"if rpath not in sys.path:\n",
" sys.path.insert(0, rpath)\n",
"\n",
"from src.loader import SlackDataLoader\n",
"import src.utils as utils"
"import src.utils as utils\n",
"\n",
"\n",
"data_loader = SlackDataLoader('C:/Users/zzz/Desktop/week0/week0_starter_network_analysis/anonymized')\n",
"print(data_loader)"
]
},
{
Expand Down Expand Up @@ -430,7 +442,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -444,7 +456,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.10.6"
}
},
"nbformat": 4,
Expand Down
22 changes: 11 additions & 11 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import print_function
import argparse
parser = argparse.ArgumentParser(description='cmdArgs')
parser.add_argument('--output', type=str, default='slack_data.csv',
help='filename to write analysis output in CSV format')
parser.add_argument('--path', required=True, type=str, help='directory where slack data reside')
parser.add_argument('--channel', type=str, default='', help='which channel we parsing')
parser.add_argument('--userfile', type=str, default='users.json', help='users profile information')
cfg = parser.parse_args()
from __future__ import print_function
import argparse

parser = argparse.ArgumentParser(description='cmdArgs')
parser.add_argument('slack_data.csv', type=str, default='slack_data.csv',
help='filename to write analysis output in CSV format')
parser.add_argument('anonymized', required=True, type=str, help='directory where slack data reside')
parser.add_argument('--channel', type=str, default='', help='which channel we parsing')
parser.add_argument('--userfile', type=str, default='users.json', help='users profile information')

cfg = parser.parse_args()
# print(cfg)
168 changes: 84 additions & 84 deletions src/loader.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
import json
import argparse
import os
import io
import shutil
import copy
from datetime import datetime
from pick import pick
from time import sleep
# Create wrapper classes for using slack_sdk in place of slacker
class SlackDataLoader:
'''
Slack exported data IO class.
When you open slack exported ZIP file, each channel or direct message
will have its own folder. Each folder will contain messages from the
conversation, organised by date in separate JSON files.
You'll see reference files for different kinds of conversations:
users.json files for all types of users that exist in the slack workspace
channels.json files for public channels,
These files contain metadata about the conversations, including their names and IDs.
For secruity reason, we have annonymized names - the names you will see are generated using faker library.
'''
def __init__(self, path):
'''
path: path to the slack exported data folder
'''
self.path = path
self.channels = self.get_channels()
self.users = self.get_ussers()
def get_users(self):
'''
write a function to get all the users from the json file
'''
with open(os.path.join(self.path, 'users.json'), 'r') as f:
users = json.load(f)
return users
def get_channels(self):
'''
write a function to get all the channels from the json file
'''
with open(os.path.join(self.path, 'channels.json'), 'r') as f:
channels = json.load(f)
return channels
def get_channel_messages(self, channel_name):
'''
write a function to get all the messages from a channel
'''
#
def get_user_map(self):
'''
write a function to get a map between user id and user name
'''
userNamesById = {}
userIdsByName = {}
for user in self.users:
userNamesById[user['id']] = user['name']
userIdsByName[user['name']] = user['id']
return userNamesById, userIdsByName
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Export Slack history')
parser.add_argument('--zip', help="Name of a zip file to import")
args = parser.parse_args()
import json
import argparse
import os
import io
import shutil
import copy
from datetime import datetime
from pick import pick
from time import sleep



# Create wrapper classes for using slack_sdk in place of slacker
class SlackDataLoader:
'''
Slack exported data IO class.

When you open slack exported ZIP file, each channel or direct message
will have its own folder. Each folder will contain messages from the
conversation, organised by date in separate JSON files.

You'll see reference files for different kinds of conversations:
users.json files for all types of users that exist in the slack workspace
channels.json files for public channels,

These files contain metadata about the conversations, including their names and IDs.

For secruity reason, we have annonymized names - the names you will see are generated using faker library.

'''
def __init__(self, path):
'''
path: path to the slack exported data folder
'''
self.path = path
self.channels = self.get_channels()
self.users = self.get_users()


def get_users(self):
'''
write a function to get all the users from the json file
'''
with open(os.path.join(self.path, 'users.json'), 'r') as f:
users = json.load(f)

return users

def get_channels(self):
'''
write a function to get all the channels from the json file
'''
with open(os.path.join(self.path, 'channels.json'), 'r') as f:
channels = json.load(f)

return channels

def get_channel_messages(self, channel_name):
'''
write a function to get all the messages from a channel

'''

#
def get_user_map(self):
'''
write a function to get a map between user id and user name
'''
userNamesById = {}
userIdsByName = {}
for user in self.users:
userNamesById[user['id']] = user['name']
userIdsByName[user['name']] = user['id']
return userNamesById, userIdsByName




if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Export Slack history')


parser.add_argument('--zip', help="Name of a zip file to import")
args = parser.parse_args()
26 changes: 26 additions & 0 deletions tests/test1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# File: tests/test_slack_data_loader.py

import pytest
from your_module import SlackDataLoader
import pandas as pd

@pytest.fixture
def slack_data_loader():
# Set up SlackDataLoader instance if needed
return SlackDataLoader()

def test_load_data_columns(slack_data_loader):

expected_columns = ['col1', 'col2', 'col3']
data_frame = slack_data_loader.load_data()
assert isinstance(data_frame, pd.DataFrame)
assert all(column in data_frame.columns for column in expected_columns), "Columns do not match the expected columns"

def test_process_data_columns(slack_data_loader):
expected_columns = ['processed_column1', 'processed_column2']

data_frame = slack_data_loader.load_data()
processed_data_frame = slack_data_loader.process_data(data_frame)

assert isinstance(processed_data_frame, pd.DataFrame)
assert all(column in processed_data_frame.columns for column in expected_columns), "Processed columns do not match the expected columns"