#!/usr/bin/env PYTHONUTF8=1 python3 ''' get a picture at some url in use curl, then open new anvil window and set the picture to background picture on to the window. author: moli ''' import sys import os import subprocess import tempfile import requests import json import mimetypes from PIL import Image import pillow_avif import argparse from magika import Magika if 'ANVIL_API_PORT' not in os.environ: print('not in Anvil.') sys.exit(1) host = 'localhost' ANVIL_API_PORT = os.environ['ANVIL_API_PORT'] ANVIL_API_SESS = os.environ['ANVIL_API_SESS'] ANVIL_WIN_ID = os.environ['ANVIL_WIN_ID'] def getSuffix(byts): ''' get the filetype in the byts, and change sufifix ''' m = Magika() mr = m.identify_bytes(byts) filetype = str(mr.output.label) match filetype: case 'png': return '.png' case 'jpeg': return '.jpeg' case 'webp': return '.webp' case 'mp4': return '.avif' case 'gif': return '.gif' case _: raise RuntimeError(f'this image type not supported. {filetype}') def download(url): ''' download the iamge file to the temporary file. >>> import aecpic >>> n, t = aecpic.download('https://moli-green.xyz/img.webp') >>> assert n != None and t == 'webp' ''' cr = subprocess.run(['curl', url], capture_output = True) if cr.returncode != 0: raise RuntimeError('Downlad failed.') suffix = getSuffix(cr.stdout) filename = None with tempfile.NamedTemporaryFile( mode='wb', delete=False, suffix=suffix) as f: f.write(cr.stdout) filename = f.name return filename def getPNG(url): ''' Download the file from the URL, convert and save to PNG format. >>> import apcpic >>> r = apcpic.getPNG('https://moli-green.xyz/img.webp') >>> if r != '': ... pass ''' png_filename = '' with tempfile.NamedTemporaryFile( mode='wb', delete=False, suffix='.png') as f: png_filename = f.name filename = download(url) # print(filename) img = Image.open(filename) img.save(png_filename) return png_filename def get_anvil_window(): ''' Get or create Anvil's window. ''' global ANVIL_WIN_ID if ANVIL_WIN_ID != '': return ANVIL_WIN_ID h = { 'Anvil-Sess': ANVIL_API_SESS, 'Accept': 'application/json', } s = f'http://{host}:{int(ANVIL_API_PORT)}/wins' r = requests.post(s, headers=h) if r.status_code != requests.codes.ok: raise RuntimeError(f'status code: {r.status_code}') ANVIL_WIN_ID = r.json()['Id'] return ANVIL_WIN_ID def anvil_Pic(host='localhost', url=None): ''' Pic command send to Anvil. ''' if not url: raise RuntimeError('not have url.') filename = getPNG(url) anvil_win_id = get_anvil_window() h = { 'Anvil-Sess': ANVIL_API_SESS, 'Accept': 'application/json', } d = { 'Winid': int(ANVIL_WIN_ID), 'Cmd': 'Pic', 'Args': [filename, 'fit'], } s = f'http://{host}:{int(ANVIL_API_PORT)}/execute' r = requests.post(s, headers=h, data=json.dumps(d)) if r.status_code != requests.codes.ok: raise RuntimeError(f'status code: {r.status_code}') if __name__ == '__main__': p = argparse.ArgumentParser( description='Downlowad Url Image and Set Anvil Picture.') p.add_argument('--host', default='localhost', help='Hostname') p.add_argument('url', help='Image URL') arg = p.parse_args() host = arg.host anvil_Pic(host, arg.url)