Appointlet2GS / app.py
Youfeng's picture
Add app.py
8f9b3da
raw
history blame
5.15 kB
# 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)