Spaces:
Sleeping
Sleeping
File size: 6,006 Bytes
9c3fb6c |
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# AUTOGENERATED! DO NOT EDIT! File to edit: slack_selenium_solution.ipynb.
# %% auto 0
__all__ = ['iface', 'SlackAnalytics', 'GoogleSheets', 'run']
# %% slack_selenium_solution.ipynb 0
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
import os
import gspread
import pandas as pd
from pathlib import Path
import gradio as gr
# %% slack_selenium_solution.ipynb 1
class SlackAnalytics():
def __init__(self,
signin_url = 'https://thriveprojectgroup.slack.com/sign_in_with_password',
members_stas_url = 'https://thriveprojectgroup.slack.com/admin/stats#members',
slackbot_url= 'https://app.slack.com/client/T01BD3PFRU4/D04GAGYT34Z'):
self.signin_url = signin_url
self.members_stas_url = members_stas_url
self.slackbot_url = slackbot_url
options = Options()
options.add_argument('--incognito')
options.add_argument("--headless")
options.add_experimental_option("prefs", {"download.default_directory": os.getcwd()})
service = ChromeService(ChromeDriverManager().install())
self.driver = webdriver.Chrome(options=options, service=service)
self.driver.maximize_window()
def export_csv(self, account, last_active=True, all_columns=False, all_time=True):
# Login Slack with eamil and password
email, password = open(account, 'r').read().splitlines()
self.driver.get(url=self.signin_url)
self.driver.implicitly_wait(30)
# input email
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='login_email']").send_keys(email)
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='login_password']").send_keys(password)
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='signin_button']").click()
# Export csv file from Slack Analytics
self.driver.get(url=self.members_stas_url)
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='data_table_header_edit_columns-header-action']").click()
# select column 'last active'
if last_active:
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='data_table_header__data_table_edit_columns_modal__checkbox_date_last_active']").click()
# select all columns
if all_columns:
self.driver.find_element(By.CSS_SELECTOR, "input[data-qa='data_table_header__data_table_edit_columns_modal__checkbox_date_last_active']").click()
self.driver.find_element(By.XPATH, '//button[contains(text(), "Close")]').click()
# select all time
if all_time:
self.driver.find_element(By.CSS_SELECTOR, "div[id='data_table_header-filter_button-option']").click()
self.driver.find_element(By.CSS_SELECTOR, "span[data-qa='all']").click()
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='analytics_members_csv-header-action']").click()
# download and delete the csv file
self.driver.get(url=self.slackbot_url)
time.sleep(10) # wait until the file is loaded
self.driver.find_element(By.XPATH, '//span[contains(text(), "THRIVE Project Member Analytics All time")]').click()
self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='more_file_actions']").click()
self.driver.find_element(By.XPATH, '//div[contains(text(), "Delete file")]').click()
self.driver.find_element(By.XPATH, '//button[contains(text(), "Yes, Delete This File")]').click()
# Close the browser windows and ends the WebDriver session
time.sleep(1)
self.driver.quit()
# %% slack_selenium_solution.ipynb 2
class GoogleSheets():
def __init__(self, creds_file='slack-analytics-creds.json', sh_file='gs_info.txt'):
self.creds = creds_file
self.sh = sh_file
self.folder = os.getcwd()
def get_sheet_info(self):
sh_id, wk_name = open(self.sh, 'r').read().splitlines()
return sh_id, wk_name
def update_worksheet(self):
# Setup service account
sh_id, wk_name = self.get_sheet_info()
gc = gspread.service_account(filename=self.creds)
# Open a sheet from a spreadsheet
wk = gc.open_by_key(sh_id).worksheet(wk_name)
# Clear the worksheet
wk.clear()
# Read and remove the downoaded csv file and update the data to Google Sheets
for fn in os.listdir(self.folder):
if fn.find('THRIVE Project Member Analytics All time') != -1:
file = os.path.join(self.folder, fn)
df = pd.read_csv(file, low_memory=False)
df = df.fillna('')
os.remove(file)
# update the new data to Google Sheets
wk.update([df.columns.values.tolist()] + df.values.tolist())
# %% slack_selenium_solution.ipynb 6
def run(account, creds_file, sh_file):
SlackAnalytics().export_csv(account.name)
GoogleSheets(creds_file.name, sh_file.name).update_worksheet()
return 'Slack Analytics data update Google Sheets completed!'
# %% slack_selenium_solution.ipynb 7
iface = gr.Interface(fn=run,
inputs=[gr.File(label='Slack Account File'),
gr.File(label='Google Credentials Json File'),
gr.File(label='Googlesheet Key File')],
outputs=gr.Text(),
allow_flagging='never',
title='Slack Analytics Members Data Getter',
description='Download Slack analytics data From Slack and upload it to Google Sheets')
iface.launch(height=450, width=500)
|