- Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathdownload_look.py
82 lines (64 loc) · 2.45 KB
/
download_look.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
""" Given a look title, search all looks to retrieve its id, render and export the look to png or jpg.
$ python download_look.py <title> <image_width> <image_height> <image_format>
img_width defaults to 545, img_height defaults to 842, img_format defaults to png
Examples:
$ python download_look.py "A simple look"
$ python download_look.py "A simple look" 545 842 png
Last modified: Feb 27 2024
"""
importsys
importtextwrap
importtime
importlooker_sdk
fromlooker_sdkimportmodels40asmodels
sdk=looker_sdk.init40("../../looker.ini")
defmain():
look_title=sys.argv[1] iflen(sys.argv) >1else""
image_width=int(sys.argv[2]) iflen(sys.argv) >2else545
image_height=int(sys.argv[3]) iflen(sys.argv) >3else842
image_format=sys.argv[4] iflen(sys.argv) >4else"png"
ifnotlook_title:
raiseException(
textwrap.dedent(
"""
Please provide: <lookTitle> [<img_width>] [<img_height>] [<img_format>]
img_width defaults to 545
img_height defaults to 842
img_format defaults to 'png'"""
)
)
look=get_look(look_title)
download_look(look, image_format, image_width, image_height)
defget_look(title: str) ->models.Look:
title=title.lower()
look=next(iter(sdk.search_looks(title=title)), None)
ifnotlook:
raiseException(f"look '{title}' was not found")
returnlook
defdownload_look(look: models.Look, result_format: str, width: int, height: int):
"""Download specified look as png/jpg"""
id=int(look.id)
task=sdk.create_look_render_task(id, result_format, width, height,)
ifnot (taskandtask.id):
raiseException(
f"Could not create a render task for '{look.title}'"
)
# poll the render task until it completes
elapsed=0.0
delay=0.5# wait .5 seconds
whileTrue:
poll=sdk.render_task(task.id)
ifpoll.status=="failure":
print(poll)
raiseException(f"Render failed for '{look.title}'")
elifpoll.status=="success":
break
time.sleep(delay)
elapsed+=delay
print(f"Render task completed in {elapsed} seconds")
result=sdk.render_task_results(task.id)
filename=f"{look.title}.{result_format}"
withopen(filename, "wb") asf:
f.write(result)
print(f"Look saved to '{filename}'")
main()