Youfeng commited on
Commit
9c58d7b
·
1 Parent(s): b7963ee

update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -9,6 +9,7 @@ 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
@@ -16,7 +17,7 @@ import pandas as pd
16
  from pathlib import Path
17
  import gradio as gr
18
 
19
- # %% slack_selenium_solution.ipynb 2
20
  class SlackAnalytics():
21
  def __init__(self,
22
  signin_url = 'https://thriveprojectgroup.slack.com/sign_in_with_password',
@@ -24,11 +25,13 @@ class SlackAnalytics():
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)
@@ -71,21 +74,25 @@ class SlackAnalytics():
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 3
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()
@@ -103,30 +110,34 @@ class GoogleSheets():
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 7
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 8
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, debug=True)
 
9
  from selenium.webdriver.chrome.options import Options
10
  from webdriver_manager.chrome import ChromeDriverManager
11
  from selenium.webdriver.common.by import By
12
+ from selenium.webdriver import ActionChains
13
  import time
14
  import os
15
  import gspread
 
17
  from pathlib import Path
18
  import gradio as gr
19
 
20
+ # %% slack_selenium_solution.ipynb 1
21
  class SlackAnalytics():
22
  def __init__(self,
23
  signin_url = 'https://thriveprojectgroup.slack.com/sign_in_with_password',
 
25
  slackbot_url= 'https://app.slack.com/client/T01BD3PFRU4/D04GAGYT34Z'):
26
  self.signin_url = signin_url
27
  self.members_stas_url = members_stas_url
28
+ self.slackbot_url = slackbot_url
29
+ # self.path = Path.home()/'Downloads'
30
+ self.path = Path('.')
31
  options = Options()
32
  options.add_argument('--incognito')
33
  options.add_argument("--headless")
34
+ options.add_experimental_option("prefs", {"download.default_directory": str(self.path)})
35
 
36
  service = ChromeService(ChromeDriverManager().install())
37
  self.driver = webdriver.Chrome(options=options, service=service)
 
74
  self.driver.get(url=self.slackbot_url)
75
  time.sleep(10) # wait until the file is loaded
76
  self.driver.find_element(By.XPATH, '//span[contains(text(), "THRIVE Project Member Analytics All time")]').click()
77
+ time.sleep(2)
78
  self.driver.find_element(By.CSS_SELECTOR, "button[data-qa='more_file_actions']").click()
79
+ time.sleep(2)
80
  self.driver.find_element(By.XPATH, '//div[contains(text(), "Delete file")]').click()
81
+ time.sleep(2)
82
  self.driver.find_element(By.XPATH, '//button[contains(text(), "Yes, Delete This File")]').click()
83
+
84
  # Close the browser windows and ends the WebDriver session
85
+ time.sleep(2)
86
  self.driver.quit()
87
 
88
 
89
+ # %% slack_selenium_solution.ipynb 2
90
  class GoogleSheets():
91
  def __init__(self, creds_file='slack-analytics-creds.json', sh_file='gs_info.txt'):
92
  self.creds = creds_file
93
  self.sh = sh_file
94
+ # self.path = Path.home()/'Downloads'
95
+ self.path = Path('.')
96
 
97
  def get_sheet_info(self):
98
  sh_id, wk_name = open(self.sh, 'r').read().splitlines()
 
110
  wk.clear()
111
 
112
  # Read and remove the downoaded csv file and update the data to Google Sheets
113
+ for fn in os.listdir(self.path):
114
  if fn.find('THRIVE Project Member Analytics All time') != -1:
115
+ file = os.path.join(self.path, fn)
116
  df = pd.read_csv(file, low_memory=False)
117
  df = df.fillna('')
118
  os.remove(file)
119
 
120
  # update the new data to Google Sheets
121
  wk.update([df.columns.values.tolist()] + df.values.tolist())
122
+ return df[['Name', 'Messages posted', 'Last active (UTC)']].head()
123
 
124
 
125
+ # %% slack_selenium_solution.ipynb 6
126
  def run(account, creds_file, sh_file):
127
  SlackAnalytics().export_csv(account.name)
128
+ df_sub = GoogleSheets(creds_file.name, sh_file.name).update_worksheet()
129
+ return ('Slack Analytics data update Google Sheets completed!', df_sub)
130
 
131
+ # %% slack_selenium_solution.ipynb 7
132
  iface = gr.Interface(fn=run,
133
  inputs=[gr.File(label='Slack Account File'),
134
  gr.File(label='Google Credentials Json File'),
135
  gr.File(label='Googlesheet Key File')],
136
+ outputs=[gr.Text(),
137
+ gr.DataFrame(headers=['Name', 'Messages posted', 'Last active (UTC)'],
138
+ datatype=['str', 'number', 'str'])
139
+ ],
140
  allow_flagging='never',
141
  title='Slack Analytics Members Data Getter',
142
  description='Download Slack analytics data From Slack and upload it to Google Sheets')
143
+ iface.launch(height=450, width=500, share=True)