Skip to content

Commit c574a62

Browse files
akash5100nabobalis
andauthored
Add createScreenshot function (#77)
* Add createScreenshot function * Suggested changes for the filename and testing * Apply suggestions from code review Co-authored-by: Nabil Freij <[email protected]> * Applying suggested changes * Applying suggested changes Co-authored-by: Nabil Freij <[email protected]>
1 parent 7698bea commit c574a62

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

hvpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
from .datasource import *
33
from .event import *
44
from .facade import *
5-
from .helpers import createMovie
5+
from .helpers import createMovie, createScreenshot
66
from .utils import create_events, create_layers, save_file
77
from .version import __version__

hvpy/helpers.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
from datetime import datetime
55

66
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
89
from hvpy.utils import _add_shared_docstring, save_file
910

1011
__all__ = [
1112
"createMovie",
13+
"createScreenshot",
1214
]
1315

1416

@@ -57,7 +59,7 @@ def createMovie(
5759
Default is `False`.
5860
filename
5961
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}"``.
6163
hq
6264
Download a higher-quality movie file (valid for "mp4" movies only, ignored otherwise).
6365
Default is `False`, optional.
@@ -124,3 +126,84 @@ def createMovie(
124126
overwrite=overwrite,
125127
)
126128
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)

hvpy/tests/test_helper.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from hvpy.datasource import DataSource
7-
from hvpy.helpers import createMovie
7+
from hvpy.helpers import createMovie, createScreenshot
88
from hvpy.utils import create_events, create_layers
99

1010

@@ -93,3 +93,37 @@ def test_error_handling(tmp_path):
9393
imageScale=1,
9494
filename=f1,
9595
)
96+
97+
98+
def test_createScreenshot(date, tmp_path):
99+
f1 = tmp_path / "screenshot"
100+
result = createScreenshot(
101+
date=date,
102+
layers=create_layers([(DataSource.AIA_171, 100)]),
103+
events=create_events(["AR"]),
104+
imageScale=2.5,
105+
x0=0,
106+
y0=0,
107+
width=4000,
108+
height=4000,
109+
filename=f1,
110+
)
111+
assert isinstance(result, Path)
112+
assert result.exists()
113+
assert result == tmp_path / "screenshot.png"
114+
115+
116+
def test_createScreenshot_with_none_filename(date):
117+
result = createScreenshot(
118+
date=date,
119+
layers=create_layers([(DataSource.AIA_171, 100)]),
120+
events=create_events(["AR"]),
121+
imageScale=2.5,
122+
x0=0,
123+
y0=0,
124+
width=4000,
125+
height=4000,
126+
)
127+
assert isinstance(result, Path)
128+
assert result.exists()
129+
result.unlink() # clean up

0 commit comments

Comments
 (0)