|
| 1 | +from playwright.sync_api import sync_playwright |
| 2 | +from playwright_stealth import stealth_sync |
| 3 | + |
| 4 | + |
| 5 | +class ReplitScrapper(): |
| 6 | + user_agent = ( |
| 7 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " |
| 8 | + "AppleWebKit/537.36 (KHTML, like Gecko) " |
| 9 | + "Chrome/116.0.0.0 " |
| 10 | + "Safari/537.36 " |
| 11 | + "Edg/116.0.1938.81" |
| 12 | + ) |
| 13 | + |
| 14 | + def __init__(self, login_name, login_password): |
| 15 | + self.__login_name = login_name |
| 16 | + self.__login_password = login_password |
| 17 | + self._replit_url = None |
| 18 | + self._downloaded_filename = None |
| 19 | + |
| 20 | + def set_replit_url(self, replit_url) -> None: |
| 21 | + if replit_url is None: |
| 22 | + raise ValueError |
| 23 | + self._replit_url = replit_url |
| 24 | + |
| 25 | + def get_replit_url(self) -> str: |
| 26 | + if self._replit_url is None: |
| 27 | + raise ValueError("Missing replit_url") |
| 28 | + return self._replit_url |
| 29 | + |
| 30 | + def _set_downloaded_filename(self, filename) -> None: |
| 31 | + if filename is None: |
| 32 | + raise ValueError("ReplitScrapper._set_downloaded_filename() argument is None") |
| 33 | + self._downloaded_filename = filename |
| 34 | + |
| 35 | + def get_downloaded_filename(self) -> str: |
| 36 | + if self._downloaded_filename is None: |
| 37 | + raise ValueError("Missing downloaded_filename") |
| 38 | + return self._downloaded_filename |
| 39 | + |
| 40 | + def _visit_replit_repo(self, page) -> None: |
| 41 | + response = page.goto(self.get_replit_url(), wait_until="domcontentloaded") |
| 42 | + if response.status != 200: |
| 43 | + if response.status == 404: |
| 44 | + print(f"response.status = {response.status}") |
| 45 | + raise ValueError("Invalid replit_url") |
| 46 | + else: |
| 47 | + print(f"response.status = {response.status}") |
| 48 | + raise ValueError("ReplitScrapper._visit_replit_repo() something other than 404 happened") |
| 49 | + |
| 50 | + def _login_replit(self, page) -> None: |
| 51 | + # Login |
| 52 | + page.goto('https://replit.com/login', wait_until="domcontentloaded") |
| 53 | + page.screenshot(path="./screen-shots/replit.png") |
| 54 | + url_init = "https://identitytoolkit.googleapis.com/v1/accounts" |
| 55 | + with page.expect_response(lambda response: url_init in response.url) as response_info: |
| 56 | + page.locator( |
| 57 | + "xpath=/html/body/div[1]/div/div[2]/div/main/div[2]/div/form/div[1]/input" |
| 58 | + ).fill(self.__login_name) |
| 59 | + page.locator( |
| 60 | + "xpath=/html/body/div[1]/div/div[2]/div/main/div[2]/div/form/div[2]/div/input" |
| 61 | + ).fill(self.__login_password) |
| 62 | + page.locator( |
| 63 | + "xpath=/html/body/div[1]/div/div[2]/div/main/div[2]/div/form/div[3]/button" |
| 64 | + ).click() |
| 65 | + response = response_info.value |
| 66 | + if response.status != 200: |
| 67 | + print(response) |
| 68 | + if response.status == 400: |
| 69 | + print(f"response.status = {response.status}") |
| 70 | + raise ValueError("Invalid login credentials") |
| 71 | + else: |
| 72 | + print(f"response.status = {response.status}") |
| 73 | + raise ValueError("ReplitScrapper._login_replit() something other than 401 happened") |
| 74 | + page.wait_for_url("https://replit.com/~") |
| 75 | + page.screenshot(path="./screen-shots/replit_after_login.png") |
| 76 | + |
| 77 | + def _download_as_zip(self, page) -> None: |
| 78 | + # Wait for page load |
| 79 | + page.locator( |
| 80 | + "xpath=/html/body/div[1]/div[1]/div[1]/div[2]/div/div[1]/div/div[3]/div/div[1]/button/div/span" |
| 81 | + ).wait_for() |
| 82 | + while page.locator( |
| 83 | + "xpath=/html/body/div[1]/div[1]/div[1]/div[2]/header/div[2]/button" |
| 84 | + ).text_content() != "Run": |
| 85 | + print(page.locator( |
| 86 | + "xpath=/html/body/div[1]/div[1]/div[1]/div[2]/header/div[2]/button" |
| 87 | + ).text_content()) |
| 88 | + page.wait_for_timeout(2000) |
| 89 | + page.screenshot(path="./screen-shots/target_page.png") |
| 90 | + |
| 91 | + # Begin downloading |
| 92 | + page.locator( |
| 93 | + "xpath=/html/body/div[1]/div[1]/div[1]/div[2]/div/div[1]/div/div[2]/div[1]/div[1]/div/button[3]" |
| 94 | + ).click() |
| 95 | + with page.expect_download() as download_info: |
| 96 | + page.locator( |
| 97 | + "xpath=/html/body/div[@class='css-1o92kwk']//div[@id='item-4']//div[@class='css-1l2rn59']" |
| 98 | + ).click() |
| 99 | + download = download_info.value |
| 100 | + self._set_downloaded_filename(download.suggested_filename) |
| 101 | + download.save_as(f"./screen-shots/{download.suggested_filename}") |
| 102 | + |
| 103 | + def run(self): |
| 104 | + print("ReplitScrapper: Begin downloading repo files...") |
| 105 | + with sync_playwright() as p: |
| 106 | + # Context setup |
| 107 | + browser = p.chromium.launch(slow_mo=50) |
| 108 | + # browser = p.chromium.launch(headless=False |
| 109 | + # , slow_mo=50 |
| 110 | + # ) |
| 111 | + context = browser.new_context(user_agent=ReplitScrapper.user_agent) |
| 112 | + page = context.new_page() |
| 113 | + stealth_sync(page) |
| 114 | + |
| 115 | + # Login replit |
| 116 | + self._login_replit(page) |
| 117 | + |
| 118 | + # Download repo files as zip |
| 119 | + self._visit_replit_repo(page) |
| 120 | + self._download_as_zip(page) |
| 121 | + |
| 122 | + # Clean-up |
| 123 | + context.close() |
| 124 | + browser.close() |
| 125 | + print("ReplitScrapper: Download complete") |
0 commit comments