Last active
March 29, 2024 15:22
-
-
Save lrhazi/a6b924bb1b89d7a48c3ab0f4705b1e1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
from playwright.sync_api import sync_playwright, expect | |
import logging | |
URL = "https://admin.google.com/" | |
URL_USERS = "https://admin.google.com/ac/users" | |
USERNAME = input("Enter Google Admin username: ") | |
PASSWORD = input("Enter the password: ") | |
DELETION_DATE = input("""Enter the deletion date (e.g. "Fri, Mar 22, 2024"): """) | |
USERS_LIST_FILE = "deleted-files-users.txt" | |
LOG_FILE = "restore-files.log" | |
BROWSER_DATA_DIR = "browser_data" | |
def get_users(fname, log_file): | |
""" | |
Get the list of users from the file and exclude the users that are already restored. | |
Assumes the users file list contains lines in the format: "count username" | |
count is ingored. | |
""" | |
done_users = [] | |
if os.path.exists(log_file): | |
with open(log_file, encoding="utf-8") as f: | |
for line in f: | |
if ": restored." in line: | |
user = line.strip().split()[2].rstrip(":") | |
done_users.append(user) | |
users = [] | |
with open(fname, encoding="utf-8") as f: | |
for line in f: | |
_count, user = line.strip().split() | |
if user not in done_users: | |
users.append(user) | |
users = sorted(set(users),reverse=True) | |
return users | |
def main(): | |
logging.basicConfig( | |
filename=LOG_FILE, | |
filemode="a", | |
level=logging.INFO, | |
format="%(asctime)s %(message)s", | |
) | |
logging.getLogger().addHandler(logging.StreamHandler()) | |
users = get_users(USERS_LIST_FILE, LOG_FILE) | |
with sync_playwright() as p: | |
browser = p.chromium.launch_persistent_context( | |
BROWSER_DATA_DIR, headless=False, args=["--start-maximized"] | |
) | |
page = browser.new_page() | |
page.set_default_timeout(1000 * 60) | |
for user in users: | |
if not user: | |
continue | |
logging.info(f"{user}: processing") | |
try: | |
page.goto(URL) | |
title = page.title() | |
if title == "Sign in - Google Accounts": | |
try: | |
page.fill('input[type="password"]', PASSWORD) | |
page.click("text=Next") | |
expect(page).to_have_title("Admin console") | |
except Exception as e: | |
logging.error(f"{user}: {e}") | |
logging.warning(f"Maybe we are not logged in yet in this browser session. Complete login please.") | |
input("Press Enter after login.") | |
continue | |
page.goto(URL_USERS) | |
expect(page).to_have_title("User List - Admin Console") | |
page.fill('input[type="text"]', user) | |
time.sleep(2) | |
page.get_by_text(user).first.click() | |
time.sleep(5) | |
page.get_by_text("RESTORE DATA").last.click() | |
page.get_by_label("From date").fill(DELETION_DATE) | |
page.get_by_label("To date").fill(DELETION_DATE) | |
page.get_by_text("RESTORE").last.click() | |
logging.info(f"{user}: restored.") | |
time.sleep(10) | |
except Exception as e: | |
logging.error(f"{user}: {e}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script to help automate restoration of user data using admin console.
Should be able to run it repeatedly, if it fails, as it avoids reprocessing users.