Skip to content

Commit cad5f41

Browse files
Updated wait_for_new_pdf method
1 parent d14d718 commit cad5f41

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

botcity/web/bot.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ def print_pdf(self, path=None, print_options=None):
976976
default_path = os.path.expanduser(os.path.join(self.download_folder_path, f"{title}.pdf"))
977977

978978
if self.browser in [Browser.CHROME, Browser.EDGE] and not self.headless:
979-
pdf_current_count = self.check_file_count(file_extension=".pdf")
979+
pdf_current_count = self.get_file_count(file_extension=".pdf")
980980
# Chrome still does not support headless webdriver print
981981
# but Firefox does.
982982
self.execute_javascript("window.print();")
@@ -986,11 +986,11 @@ def print_pdf(self, path=None, print_options=None):
986986
self.wait_for_file(default_path, timeout=timeout)
987987
else:
988988
# Waiting when the file don't have the page title in path
989-
self.wait_for_new_pdf(pdf_current_count, timeout=timeout)
989+
self.wait_for_new_file(file_extension=".pdf", current_count=pdf_current_count)
990990

991991
# Move the downloaded pdf file if the path is not None
992992
if path:
993-
last_downloaded_pdf = self.return_last_created_file(self.download_folder_path, ".pdf")
993+
last_downloaded_pdf = self.get_last_created_file(self.download_folder_path, ".pdf")
994994
os.rename(last_downloaded_pdf, path)
995995
return path
996996
self.wait(2000)
@@ -1866,7 +1866,7 @@ def wait_for_file(self, path, timeout=60000):
18661866
return True
18671867
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
18681868

1869-
def return_last_created_file(self, path=None, file_extension=""):
1869+
def get_last_created_file(self, path=None, file_extension=""):
18701870
"""Returns the last created file in a specific folder path.
18711871
18721872
Args:
@@ -1883,7 +1883,7 @@ def return_last_created_file(self, path=None, file_extension=""):
18831883
last_created_file = max(files_path, key=os.path.getctime)
18841884
return last_created_file
18851885

1886-
def check_file_count(self, path=None, file_extension=""):
1886+
def get_file_count(self, path=None, file_extension=""):
18871887
"""Get the total number of files of the same type.
18881888
18891889
Args:
@@ -1899,21 +1899,29 @@ def check_file_count(self, path=None, file_extension=""):
18991899
files_path = glob.glob(os.path.expanduser(os.path.join(path, f"*{file_extension}")))
19001900
return len(files_path)
19011901

1902-
def wait_for_new_pdf(self, current_count=0, timeout=60000):
1902+
def wait_for_new_file(self, path=None, file_extension="", current_count=0, timeout=60000):
19031903
"""
1904-
Wait for a new pdf file to be available on download folder path.
1904+
Wait for a new file to be available on disk without the file path.
19051905
19061906
Args:
1907-
current_count (int): The current number of pdf files in the folder. Defaults to 0 files
1907+
path (str, optional): The path of the folder where the file is expected. Defaults to None.
1908+
file_extension (str, optional): The extension of the file to be searched for (e.g., .pdf, .txt).
1909+
current_count (int): The current number of files in the folder of the given type. Defaults to 0 files
19081910
timeout (int, optional): Maximum wait time (ms) to search for a hit.
19091911
Defaults to 60000ms (60s).
1912+
1913+
Returns:
1914+
str: the path of the last created file of the given type
19101915
"""
1911-
pdf_count = 0
1912-
wait_time = 0
1913-
while pdf_count != current_count + 1:
1914-
self.wait(2000)
1915-
pdf_count = self.check_file_count(file_extension=".pdf")
1916+
if not path:
1917+
path = self.download_folder_path
19161918

1917-
wait_time = wait_time + 2000
1918-
if wait_time >= timeout:
1919-
break
1919+
start_time = time.time()
1920+
while True:
1921+
elapsed_time = (time.time() - start_time) * 1000
1922+
if elapsed_time > timeout:
1923+
return None
1924+
pdf_count = self.get_file_count(path, f"*{file_extension}")
1925+
if pdf_count == current_count + 1:
1926+
return self.get_last_created_file(path, f"*{file_extension}")
1927+
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)

0 commit comments

Comments
 (0)