File size: 5,147 Bytes
21ce962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf20169
21ce962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf20169
21ce962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf20169
21ce962
 
 
 
 
 
8f9b3da
21ce962
 
cf20169
21ce962
 
 
 
 
 
 
 
8f9b3da
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
134
135
136
137
138
139
140
# AUTOGENERATED! DO NOT EDIT! File to edit: Appointlet2GS.ipynb.

# %% auto 0
__all__ = ['iface', 'Appointlet', 'GoogleSheets', 'run']

# %% Appointlet2GS.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

# %% Appointlet2GS.ipynb 1
class Appointlet():
    def __init__(self, url = 'https://app.appointlet.com/unified_login'):
        self.url = url
        
    def setup(self):
        options = Options()
        options.add_argument('--incognito')
#         options.add_argument("--headless")
        service = ChromeService(ChromeDriverManager().install())
        driver = webdriver.Chrome(options=options, service=service)
        driver.maximize_window()
        return driver
    
        
    def download(self, account):
        email, password = open(account, 'r').read().splitlines()
        driver = self.setup()
        driver.get(url=self.url)
        driver.implicitly_wait(30)
        
        # input email
        driver.find_element(By.CSS_SELECTOR, "input[name='email']").send_keys(email)
        driver.find_element(By.CSS_SELECTOR, 'button.mt-16').click()
        
        # input password
        driver.find_element(By.CSS_SELECTOR, "input[name='password']").send_keys(password)
        driver.find_element(By.CSS_SELECTOR, 'button.mt-16').click()
        
        # remove welcome message
        try:
            driver.find_element(By.CSS_SELECTOR, 'button.btn-block').click()
        except:
            pass
        
        # remove introduction
        try:
            driver.find_element(By.CSS_SELECTOR, 'button.shepherd-cancel-icon').click()
        except:
            pass
        
        # Meeting filter
        # Launch meeting filter
        driver.find_element(By.ID, 'MeetingFilters').click()
#         # Filter by meeting types
#         driver.find_element(By.CSS_SELECTOR, 'svg.css-8mmkcg').click()
#         driver.find_element(By.XPATH, '//div[contains(text(), "Example Meeting")]').click()
        # Filter by status
        driver.find_element(By.CSS_SELECTOR, "input[name='isPending']").click()
        # Apply filters
        driver.find_element(By.XPATH, '//span[contains(text(), "Apply Filters")]').click()
        
        # Download csv file
        driver.find_element(By.CSS_SELECTOR, "div.MoreButton.dropdown > button[class='dropdown-toggle btn btn-outline-secondary']").click()
        driver.find_element(By.XPATH, '//span[contains(text(), "Export Attendees (CSV)")]').click()
        
        # Close the browser windows and ends the WebDriver session
        time.sleep(5)
        driver.quit()
        

# %% Appointlet2GS.ipynb 3
class GoogleSheets():
#     def __init__(self, creds_file = 'credentials.json', sh_file = 'googlesheet_info.txt'):
    def __init__(self, creds_file, sh_file):
        self.creds = creds_file
        self.sh = sh_file
        self.folder = '.'
        
    def get_sheet_info(self):
        sh_id, wk_name = open(self.sh, 'r').read().splitlines()
        return sh_id, wk_name
    
    def read_csv(self):
        # read the downoaded csv file into dataframe and remove the file
        for fn in os.listdir(self.folder):
            if fn.find('Meeting Attendees') != -1:
                file = os.path.join(self.folder, fn)
                df = pd.read_csv(file, low_memory=False)
                df = df.fillna('')
                os.remove(file)
        return df
        
    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()
        
        # Get the new data
        df = self.read_csv()
        
        # Writing the new data to Google Sheets
        wk.update([df.columns.values.tolist()] + df.values.tolist())


# %% Appointlet2GS.ipynb 4
def run(account, creds_file, sh_file):
    if not account:
        return 'Please upload an account file'
    else:
        Appointlet().download(account.name)
#         Appointlet().download(account)
#         GoogleSheets(creds_file.name, sh_file.name).update_worksheet()
        return 'Update Google Sheets completed!'

# %% Appointlet2GS.ipynb 7
iface = gr.Interface(fn=run, 
                     inputs=[gr.File(label='Account File'), 
                             gr.File(label='Credentials Json File'), 
                             gr.File(label='Googlesheet Info')], 
                     outputs=gr.Text(),
                     allow_flagging='never', 
                     title='Appointlet2GS Application', 
                     description='Download Meeting Attendees file From Appointlet and upload it to Google Sheets')
iface.launch(height=450, width=500, debug=True, enable_queue=True)