1+ """
2+ Test: get_pictures and download/decrypt the first picture found
3+ Usage:
4+ python test_scripts/test_pictures.py
5+ """
6+ import asyncio
7+ import base64
8+ import sys
9+ from pathlib import Path
10+ sys .path .insert (0 , str (Path (__file__ ).parent .parent ))
11+ from utils .read_credentials import read_credentials
12+
13+ async def main ():
14+ creds = read_credentials ()
15+ if not creds .get ("BASE_URL" ) or not creds .get ("FMD_ID" ) or not creds .get ("PASSWORD" ):
16+ print ("Missing credentials." )
17+ return
18+
19+ from fmd_api import FmdClient
20+ from fmd_api .device import Device
21+ client = await FmdClient .create (creds ["BASE_URL" ], creds ["FMD_ID" ], creds ["PASSWORD" ])
22+ try :
23+ pics = await client .get_pictures (10 )
24+ print ("Pictures returned:" , len (pics ))
25+ if not pics :
26+ print ("No pictures available." )
27+ return
28+
29+ # Server sometimes returns list of dicts or list of base64 strings.
30+ # Try to extract a blob string:
31+ first = pics [0 ]
32+ blob = None
33+ if isinstance (first , dict ):
34+ # try common keys
35+ for k in ("Data" , "blob" , "Blob" , "data" ):
36+ if k in first :
37+ blob = first [k ]
38+ break
39+ # if picture metadata contains an encoded blob in a nested field, adjust as needed
40+ elif isinstance (first , str ):
41+ blob = first
42+
43+ if not blob :
44+ print ("Could not find picture blob inside first picture entry. Showing entry:" )
45+ print (first )
46+ return
47+
48+ # decrypt to get inner base64 image string or bytes
49+ decrypted = client .decrypt_data_blob (blob )
50+ try :
51+ inner_b64 = decrypted .decode ("utf-8" ).strip ()
52+ img = base64 .b64decode (inner_b64 + "=" * (- len (inner_b64 ) % 4 ))
53+ out = "picture_0.jpg"
54+ with open (out , "wb" ) as f :
55+ f .write (img )
56+ print ("Saved picture to" , out )
57+ except Exception as e :
58+ print ("Decrypted payload not a base64 image string; saving raw bytes as picture_0.bin" )
59+ with open ("picture_0.bin" , "wb" ) as f :
60+ f .write (decrypted )
61+ finally :
62+ await client .close ()
63+
64+ if __name__ == "__main__" :
65+ asyncio .run (main ())
0 commit comments