aecpic/aecpic.py

163 lines
3.6 KiB
Python
Raw Normal View History

2025-02-02 06:54:31 +09:00
#!/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
2025-02-02 06:54:31 +09:00
import os
import subprocess
import tempfile
import requests
import json
import mimetypes
from PIL import Image
import pillow_avif
2025-02-02 06:54:31 +09:00
import argparse
from magika import Magika
2025-02-02 06:54:31 +09:00
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):
2025-02-02 06:54:31 +09:00
'''
get the filetype in the byts, and change sufifix
2025-02-02 06:54:31 +09:00
'''
m = Magika()
mr = m.identify_bytes(byts)
filetype = str(mr.output.label)
match filetype:
case 'png':
2025-02-02 06:54:31 +09:00
return '.png'
case 'jpeg':
2025-02-02 06:54:31 +09:00
return '.jpeg'
case 'webp':
2025-02-02 06:54:31 +09:00
return '.webp'
case 'mp4':
2025-02-02 06:54:31 +09:00
return '.avif'
case 'gif':
2025-02-02 06:54:31 +09:00
return '.gif'
case _:
raise RuntimeError(f'this image type not supported. {filetype}')
2025-02-02 06:54:31 +09:00
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'
2025-02-02 06:54:31 +09:00
'''
cr = subprocess.run(['curl', url], capture_output = True)
if cr.returncode != 0:
raise RuntimeError('Downlad failed.')
suffix = getSuffix(cr.stdout)
filename = None
2025-02-02 06:54:31 +09:00
with tempfile.NamedTemporaryFile(
mode='wb',
delete=False,
suffix=suffix) as f:
f.write(cr.stdout)
2025-02-02 06:54:31 +09:00
filename = f.name
return filename
def getPNG(url):
'''
Download the file from the URL, convert and save to PNG format.
2025-02-02 06:54:31 +09:00
>>> 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
2025-02-02 06:54:31 +09:00
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()
2025-02-02 06:54:31 +09:00
h = {
'Anvil-Sess': ANVIL_API_SESS,
2025-02-02 06:54:31 +09:00
'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)
2025-02-02 06:54:31 +09:00