|
| 1 | +from .browser import Browser |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import platform |
| 5 | +import os |
| 6 | +import shutil |
| 7 | + |
| 8 | + |
| 9 | +def _find_chrome(user_given_path=None): |
| 10 | + """ Finds a Chrome executable. |
| 11 | +
|
| 12 | + Search Chrome on a given path. If no path given, |
| 13 | + try to find Chrome or Chromium-browser on a Windows or Unix system. |
| 14 | +
|
| 15 | + Raises |
| 16 | + ------ |
| 17 | + - `FileNotFoundError` |
| 18 | + + If a suitable chrome executable could not be found. |
| 19 | +
|
| 20 | + Returns |
| 21 | + ------- |
| 22 | + - str |
| 23 | + + Path of the chrome executable on the current machine. |
| 24 | + """ |
| 25 | + |
| 26 | + # TODO when other browsers will be available: |
| 27 | + # Ensure that the given executable is a chrome one. |
| 28 | + |
| 29 | + if user_given_path is not None: |
| 30 | + if os.path.isfile(user_given_path): |
| 31 | + return user_given_path |
| 32 | + else: |
| 33 | + raise FileNotFoundError('Could not find chrome in the given path.') |
| 34 | + |
| 35 | + if platform.system() == 'Windows': |
| 36 | + prefixes = [ |
| 37 | + os.getenv('PROGRAMFILES(X86)'), |
| 38 | + os.getenv('PROGRAMFILES'), |
| 39 | + os.getenv('LOCALAPPDATA'), |
| 40 | + ] |
| 41 | + |
| 42 | + suffix = "Google\\Chrome\\Application\\chrome.exe" |
| 43 | + |
| 44 | + for prefix in prefixes: |
| 45 | + path_candidate = os.path.join(prefix, suffix) |
| 46 | + if os.path.isfile(path_candidate): |
| 47 | + return path_candidate |
| 48 | + |
| 49 | + elif platform.system() == "Linux": |
| 50 | + |
| 51 | + # search google-chrome |
| 52 | + version_result = subprocess.check_output( |
| 53 | + ["google-chrome", "--version"] |
| 54 | + ) |
| 55 | + |
| 56 | + if 'Google Chrome' in str(version_result): |
| 57 | + return "google-chrome" |
| 58 | + |
| 59 | + # else search chromium-browser |
| 60 | + |
| 61 | + # snap seems to be a special case? |
| 62 | + # see https://stackoverflow.com/q/63375327/12182226 |
| 63 | + version_result = subprocess.check_output( |
| 64 | + ["chromium-browser", "--version"] |
| 65 | + ) |
| 66 | + if 'snap' in str(version_result): |
| 67 | + chrome_snap = ( |
| 68 | + '/snap/chromium/current/usr/lib/chromium-browser/chrome' |
| 69 | + ) |
| 70 | + if os.path.isfile(chrome_snap): |
| 71 | + return chrome_snap |
| 72 | + else: |
| 73 | + which_result = shutil.which('chromium-browser') |
| 74 | + if which_result is not None and os.path.isfile(which_result): |
| 75 | + return which_result |
| 76 | + |
| 77 | + elif platform.system() == "Darwin": |
| 78 | + # MacOS system |
| 79 | + chrome_app = ( |
| 80 | + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' |
| 81 | + ) |
| 82 | + version_result = subprocess.check_output( |
| 83 | + [chrome_app, "--version"] |
| 84 | + ) |
| 85 | + if "Google Chrome" in str(version_result): |
| 86 | + return chrome_app |
| 87 | + |
| 88 | + raise FileNotFoundError( |
| 89 | + 'Could not find a Chrome executable on this ' |
| 90 | + 'machine, please specify it yourself.' |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +class ChromeHeadless(Browser): |
| 95 | + """ |
| 96 | + Chrome/Chromium browser wrapper. |
| 97 | +
|
| 98 | + Parameters |
| 99 | + ---------- |
| 100 | + - `executable_path` : str, optional |
| 101 | + + Path to a chrome executable. |
| 102 | +
|
| 103 | + - `flags` : list of str |
| 104 | + + Flags to be used by the headless browser. |
| 105 | + + Default flags are : |
| 106 | + - '--default-background-color=0' |
| 107 | + - '--hide-scrollbars' |
| 108 | + - `print_command` : bool |
| 109 | + + Whether or not to print the command used to take a screenshot. |
| 110 | + """ |
| 111 | + |
| 112 | + def __init__(self, executable_path=None, flags=None, print_command=False): |
| 113 | + self.executable_path = executable_path |
| 114 | + if not flags: |
| 115 | + self.flags = [ |
| 116 | + '--default-background-color=0', |
| 117 | + '--hide-scrollbars', |
| 118 | + ] |
| 119 | + else: |
| 120 | + self.flags = [flags] if isinstance(flags, str) else flags |
| 121 | + |
| 122 | + self.print_command = print_command |
| 123 | + |
| 124 | + @property |
| 125 | + def executable_path(self): |
| 126 | + return self._executable_path |
| 127 | + |
| 128 | + @executable_path.setter |
| 129 | + def executable_path(self, value): |
| 130 | + self._executable_path = _find_chrome(value) |
| 131 | + |
| 132 | + def screenshot( |
| 133 | + self, |
| 134 | + input, |
| 135 | + output_path, |
| 136 | + output_file='screenshot.png', |
| 137 | + size=(1920, 1080), |
| 138 | + ): |
| 139 | + """ Calls Chrome or Chromium headless to take a screenshot. |
| 140 | +
|
| 141 | + Parameters |
| 142 | + ---------- |
| 143 | + - `output_file`: str |
| 144 | + + Name as which the screenshot will be saved. |
| 145 | + + File extension (e.g. .png) has to be included. |
| 146 | + + Default is screenshot.png |
| 147 | + - `input`: str |
| 148 | + + File or url that will be screenshotted. |
| 149 | + + Cannot be None |
| 150 | + - `size`: (int, int), optional |
| 151 | + + Two values representing the window size of the headless |
| 152 | + + browser and by extention, the screenshot size. |
| 153 | + + These two values must be greater than 0. |
| 154 | + Raises |
| 155 | + ------ |
| 156 | + - `ValueError` |
| 157 | + + If the value of `size` is incorrect. |
| 158 | + + If `input` is empty. |
| 159 | + """ |
| 160 | + |
| 161 | + if not input: |
| 162 | + raise ValueError('The `input` parameter is empty.') |
| 163 | + |
| 164 | + if size[0] < 1 or size[1] < 1: |
| 165 | + raise ValueError( |
| 166 | + f'Could not screenshot "{output_file}" ' |
| 167 | + f'with a size of {size}:\n' |
| 168 | + 'A valid size consists of two integers greater than 0.' |
| 169 | + ) |
| 170 | + |
| 171 | + # command used to launch chrome in |
| 172 | + # headless mode and take a screenshot |
| 173 | + command = [ |
| 174 | + f'{self.executable_path}', |
| 175 | + '--headless', |
| 176 | + f'--screenshot={os.path.join(output_path, output_file)}', |
| 177 | + f'--window-size={size[0]},{size[1]}', |
| 178 | + *self.flags, |
| 179 | + f'{input}', |
| 180 | + ] |
| 181 | + |
| 182 | + if self.print_command: |
| 183 | + print(' '.join(command)) |
| 184 | + |
| 185 | + subprocess.run(command) |
0 commit comments