Spaces:
Sleeping
Sleeping
Add application files
Browse files- app.py +132 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: slack_selenium_solution.ipynb.
|
2 |
+
|
3 |
+
# %% auto 0
|
4 |
+
__all__ = ['iface', 'SlackAnalytics', 'GoogleSheets', 'run']
|
5 |
+
|
6 |
+
# %% slack_selenium_solution.ipynb 0
|
7 |
+
from selenium import webdriver
|
8 |
+
from selenium.webdriver.chrome.service import Service as ChromeService
|
9 |
+
from selenium.webdriver.chrome.options import Options
|
10 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
11 |
+
from selenium.webdriver.common.by import By
|
12 |
+
import time
|
13 |
+
import os
|
14 |
+
import gspread
|
15 |
+
import pandas as pd
|
16 |
+
from pathlib import Path
|
17 |
+
import gradio as gr
|
18 |
+
|
19 |
+
# %% slack_selenium_solution.ipynb 1
|
20 |
+
class SlackAnalytics():
|
21 |
+
def __init__(self,
|
22 |
+
signin_url = 'https://thriveprojectgroup.slack.com/sign_in_with_password',
|
23 |
+
members_stas_url = 'https://thriveprojectgroup.slack.com/admin/stats#members',
|
24 |
+
slackbot_url= 'https://app.slack.com/client/T01BD3PFRU4/D04GAGYT34Z'):
|
25 |
+
self.signin_url = signin_url
|
26 |
+
self.members_stas_url = members_stas_url
|
27 |
+
self.slackbot_url = slackbot_url
|
28 |
+
options = Options()
|
29 |
+
options.add_argument('--incognito')
|
30 |
+
options.add_argument("--headless")
|
31 |
+
options.add_experimental_option("prefs", {"download.default_directory": os.getcwd()})
|
32 |
+
|
33 |
+
service = ChromeService(ChromeDriverManager().install())
|
34 |
+
self.driver = webdriver.Chrome(options=options, service=service)
|
35 |
+
self.driver.maximize_window()
|
36 |
+
|
37 |
+
def export_csv(self, account, last_active=True, all_columns=False, all_time=True):
|
38 |
+
# Login Slack with eamil and password
|
39 |
+
email, password = open(account, 'r').read().splitlines()
|
40 |
+
self.driver.get(url=self.signin_url)
|
41 |
+
self.driver.implicitly_wait(30)
|
42 |
+
|
43 |
+
# input email
|
44 |
+
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='login_email']").send_keys(email)
|
45 |
+
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='login_password']").send_keys(password)
|
46 |
+
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='signin_button']").click()
|
47 |
+
|
48 |
+
# Export csv file from Slack Analytics
|
49 |
+
self.driver.get(url=self.members_stas_url)
|
50 |
+
|
51 |
+
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='data_table_header_edit_columns-header-action']").click()
|
52 |
+
|
53 |
+
# select column 'last active'
|
54 |
+
if last_active:
|
55 |
+
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='data_table_header__data_table_edit_columns_modal__checkbox_date_last_active']").click()
|
56 |
+
|
57 |
+
# select all columns
|
58 |
+
if all_columns:
|
59 |
+
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='data_table_header__data_table_edit_columns_modal__checkbox_date_last_active']").click()
|
60 |
+
|
61 |
+
self.driver.find_element(By.XPATH, '//button[contains(text(), "Close")]').click()
|
62 |
+
|
63 |
+
# select all time
|
64 |
+
if all_time:
|
65 |
+
self.driver.find_element(By.CSS_SELECTOR, "div[id='data_table_header-filter_button-option']").click()
|
66 |
+
self.driver.find_element(By.CSS_SELECTOR, "span[data-qa='all']").click()
|
67 |
+
|
68 |
+
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='analytics_members_csv-header-action']").click()
|
69 |
+
|
70 |
+
# download and delete the csv file
|
71 |
+
self.driver.get(url=self.slackbot_url)
|
72 |
+
time.sleep(10) # wait until the file is loaded
|
73 |
+
self.driver.find_element(By.XPATH, '//span[contains(text(), "THRIVE Project Member Analytics All time")]').click()
|
74 |
+
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='more_file_actions']").click()
|
75 |
+
self.driver.find_element(By.XPATH, '//div[contains(text(), "Delete file")]').click()
|
76 |
+
self.driver.find_element(By.XPATH, '//button[contains(text(), "Yes, Delete This File")]').click()
|
77 |
+
|
78 |
+
# Close the browser windows and ends the WebDriver session
|
79 |
+
time.sleep(1)
|
80 |
+
self.driver.quit()
|
81 |
+
|
82 |
+
|
83 |
+
# %% slack_selenium_solution.ipynb 2
|
84 |
+
class GoogleSheets():
|
85 |
+
def __init__(self, creds_file='slack-analytics-creds.json', sh_file='gs_info.txt'):
|
86 |
+
self.creds = creds_file
|
87 |
+
self.sh = sh_file
|
88 |
+
self.folder = os.getcwd()
|
89 |
+
|
90 |
+
def get_sheet_info(self):
|
91 |
+
sh_id, wk_name = open(self.sh, 'r').read().splitlines()
|
92 |
+
return sh_id, wk_name
|
93 |
+
|
94 |
+
def update_worksheet(self):
|
95 |
+
# Setup service account
|
96 |
+
sh_id, wk_name = self.get_sheet_info()
|
97 |
+
gc = gspread.service_account(filename=self.creds)
|
98 |
+
|
99 |
+
# Open a sheet from a spreadsheet
|
100 |
+
wk = gc.open_by_key(sh_id).worksheet(wk_name)
|
101 |
+
|
102 |
+
# Clear the worksheet
|
103 |
+
wk.clear()
|
104 |
+
|
105 |
+
# Read and remove the downoaded csv file and update the data to Google Sheets
|
106 |
+
for fn in os.listdir(self.folder):
|
107 |
+
if fn.find('THRIVE Project Member Analytics All time') != -1:
|
108 |
+
file = os.path.join(self.folder, fn)
|
109 |
+
df = pd.read_csv(file, low_memory=False)
|
110 |
+
df = df.fillna('')
|
111 |
+
os.remove(file)
|
112 |
+
|
113 |
+
# update the new data to Google Sheets
|
114 |
+
wk.update([df.columns.values.tolist()] + df.values.tolist())
|
115 |
+
|
116 |
+
|
117 |
+
# %% slack_selenium_solution.ipynb 6
|
118 |
+
def run(account, creds_file, sh_file):
|
119 |
+
SlackAnalytics().export_csv(account.name)
|
120 |
+
GoogleSheets(creds_file.name, sh_file.name).update_worksheet()
|
121 |
+
return 'Slack Analytics data update Google Sheets completed!'
|
122 |
+
|
123 |
+
# %% slack_selenium_solution.ipynb 7
|
124 |
+
iface = gr.Interface(fn=run,
|
125 |
+
inputs=[gr.File(label='Slack Account File'),
|
126 |
+
gr.File(label='Google Credentials Json File'),
|
127 |
+
gr.File(label='Googlesheet Key File')],
|
128 |
+
outputs=gr.Text(),
|
129 |
+
allow_flagging='never',
|
130 |
+
title='Slack Analytics Members Data Getter',
|
131 |
+
description='Download Slack analytics data From Slack and upload it to Google Sheets')
|
132 |
+
iface.launch(height=450, width=500)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
selenium
|
2 |
+
webdriver_manager
|
3 |
+
gspread
|
4 |
+
pandas
|