|
4 | 4 | from datetime import datetime |
5 | 5 |
|
6 | 6 | from hvpy.api_groups.movies.queue_movie import queueMovieInputParameters |
7 | | -from hvpy.facade import downloadMovie, getMovieStatus, queueMovie |
| 7 | +from hvpy.api_groups.screenshots.take_screenshot import takeScreenshotInputParameters |
| 8 | +from hvpy.facade import downloadMovie, downloadScreenshot, getMovieStatus, queueMovie, takeScreenshot |
8 | 9 | from hvpy.utils import _add_shared_docstring, save_file |
9 | 10 |
|
10 | 11 | __all__ = [ |
11 | 12 | "createMovie", |
| 13 | + "createScreenshot", |
12 | 14 | ] |
13 | 15 |
|
14 | 16 |
|
@@ -57,7 +59,7 @@ def createMovie( |
57 | 59 | Default is `False`. |
58 | 60 | filename |
59 | 61 | The path to save the file to. |
60 | | - Optional, will default to ``f"{starttime}_{endtime}.{format}"``. |
| 62 | + Optional, will default to ``f"{res['id']}_{startTime.date()}_{endTime.date()}.{format}"``. |
61 | 63 | hq |
62 | 64 | Download a higher-quality movie file (valid for "mp4" movies only, ignored otherwise). |
63 | 65 | Default is `False`, optional. |
@@ -124,3 +126,84 @@ def createMovie( |
124 | 126 | overwrite=overwrite, |
125 | 127 | ) |
126 | 128 | return Path(filename) |
| 129 | + |
| 130 | + |
| 131 | +@_add_shared_docstring(takeScreenshotInputParameters) |
| 132 | +def createScreenshot( |
| 133 | + date: datetime, |
| 134 | + imageScale: float, |
| 135 | + layers: str, |
| 136 | + events: Optional[str] = None, |
| 137 | + eventLabels: bool = False, |
| 138 | + scale: bool = False, |
| 139 | + scaleType: Optional[str] = None, |
| 140 | + scaleX: Optional[int] = None, |
| 141 | + scaleY: Optional[int] = None, |
| 142 | + width: Optional[str] = None, |
| 143 | + height: Optional[str] = None, |
| 144 | + x0: Optional[str] = None, |
| 145 | + y0: Optional[str] = None, |
| 146 | + x1: Optional[str] = None, |
| 147 | + y1: Optional[str] = None, |
| 148 | + x2: Optional[str] = None, |
| 149 | + y2: Optional[str] = None, |
| 150 | + watermark: bool = False, |
| 151 | + overwrite: bool = False, |
| 152 | + filename: Optional[Union[str, Path]] = None, |
| 153 | +) -> Path: |
| 154 | + """ |
| 155 | + Automatically creates a screenshot using `takeScreenshot`, |
| 156 | + `downloadScreenshot` functions. |
| 157 | +
|
| 158 | + Parameters |
| 159 | + ---------- |
| 160 | + overwrite |
| 161 | + Whether to overwrite the file if it already exists. |
| 162 | + Default is `False`. |
| 163 | + filename |
| 164 | + The path to save the file to. |
| 165 | + Optional, will default to ``f"{res['id']}_{date.date()}.png"``. |
| 166 | + {Insert} |
| 167 | +
|
| 168 | + Examples |
| 169 | + -------- |
| 170 | + >>> from hvpy import createScreenshot, DataSource, create_events, create_layers, EventType |
| 171 | + >>> from datetime import datetime, timedelta |
| 172 | + >>> screenshot_location = createScreenshot( |
| 173 | + ... date=datetime.today() - timedelta(days=15), |
| 174 | + ... layers=create_layers([(DataSource.AIA_171, 100)]), |
| 175 | + ... events=create_events([EventType.ACTIVE_REGION]), |
| 176 | + ... eventLabels=True, |
| 177 | + ... imageScale=1, |
| 178 | + ... x0=0, |
| 179 | + ... y0=0, |
| 180 | + ... width=100, |
| 181 | + ... height=100, |
| 182 | + ... filename="my_screenshot", |
| 183 | + ... ) |
| 184 | + >>> # This is to cleanup the file created from the example |
| 185 | + >>> # you don't need to do this |
| 186 | + >>> from pathlib import Path |
| 187 | + >>> Path('my_screenshot.png').unlink() |
| 188 | + """ |
| 189 | + input_params = locals() |
| 190 | + # These are used later on but we want to avoid passing |
| 191 | + # them into takeScreenshot. |
| 192 | + input_params.pop("overwrite") |
| 193 | + input_params.pop("filename") |
| 194 | + res = takeScreenshot(**input_params) |
| 195 | + if res.get("error"): |
| 196 | + raise RuntimeError(res["error"]) |
| 197 | + binary_data = downloadScreenshot( |
| 198 | + id=res["id"], |
| 199 | + ) |
| 200 | + if filename is None: |
| 201 | + filename = f"{res['id']}_{date.date()}.png" |
| 202 | + else: |
| 203 | + filename = f"{filename}.png" |
| 204 | + save_file( |
| 205 | + data=binary_data, |
| 206 | + filename=filename, |
| 207 | + overwrite=overwrite, |
| 208 | + ) |
| 209 | + return Path(filename) |
0 commit comments