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
|
|
|
|
'''
|
|
|
|
|
2025-02-17 00:34:25 +09:00
|
|
|
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
|
2025-02-17 00:34:25 +09:00
|
|
|
import pillow_avif
|
2025-02-02 06:54:31 +09:00
|
|
|
import argparse
|
2025-02-17 00:34:25 +09:00
|
|
|
from magika import Magika
|
2025-02-02 06:54:31 +09:00
|
|
|
|
2025-02-17 00:34:25 +09:00
|
|
|
def getSuffix(byts):
|
2025-02-02 06:54:31 +09:00
|
|
|
'''
|
2025-02-17 00:34:25 +09:00
|
|
|
get the filetype in the byts, and change sufifix
|
2025-02-02 06:54:31 +09:00
|
|
|
|
|
|
|
'''
|
2025-02-17 00:34:25 +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'
|
2025-02-17 00:34:25 +09:00
|
|
|
case 'jpeg':
|
2025-02-02 06:54:31 +09:00
|
|
|
return '.jpeg'
|
2025-02-17 00:34:25 +09:00
|
|
|
case 'webp':
|
2025-02-02 06:54:31 +09:00
|
|
|
return '.webp'
|
2025-02-17 00:34:25 +09:00
|
|
|
case 'mp4':
|
2025-02-02 06:54:31 +09:00
|
|
|
return '.avif'
|
2025-02-17 00:34:25 +09:00
|
|
|
case 'gif':
|
2025-02-02 06:54:31 +09:00
|
|
|
return '.gif'
|
|
|
|
case _:
|
2025-02-17 00:34:25 +09:00
|
|
|
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.
|
|
|
|
|
2025-02-17 00:34:25 +09:00
|
|
|
>>> import aecpic
|
2025-02-17 01:03:54 +09:00
|
|
|
>>> r = aecpic.download('https://moli-green.xyz/img.webp')
|
|
|
|
>>> assert r != ''
|
2025-02-02 06:54:31 +09:00
|
|
|
'''
|
2025-02-17 00:34:25 +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:
|
2025-02-17 00:34:25 +09:00
|
|
|
f.write(cr.stdout)
|
2025-02-02 06:54:31 +09:00
|
|
|
filename = f.name
|
|
|
|
return filename
|
|
|
|
|
|
|
|
def getPNG(url):
|
|
|
|
'''
|
2025-02-17 00:34:25 +09:00
|
|
|
Download the file from the URL, convert and save to PNG format.
|
2025-02-02 06:54:31 +09:00
|
|
|
|
2025-02-17 01:03:54 +09:00
|
|
|
>>> import aecpic
|
|
|
|
>>> r = aecpic.getPNG('https://moli-green.xyz/img.webp')
|
|
|
|
>>> assert r != ''
|
2025-02-02 06:54:31 +09:00
|
|
|
'''
|
2025-02-17 01:03:54 +09:00
|
|
|
|
2025-02-02 06:54:31 +09:00
|
|
|
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
|
|
|
|
|
2025-02-17 01:03:54 +09:00
|
|
|
def get_anvil_window(host='localhost'):
|
2025-02-17 00:34:25 +09:00
|
|
|
'''
|
|
|
|
Get or create Anvil's window.
|
|
|
|
'''
|
|
|
|
|
2025-02-17 01:03:54 +09:00
|
|
|
ANVIL_API_PORT = os.environ['ANVIL_API_PORT']
|
|
|
|
ANVIL_API_SESS = os.environ['ANVIL_API_SESS']
|
|
|
|
ANVIL_WIN_ID = os.environ['ANVIL_WIN_ID']
|
2025-02-17 00:34:25 +09:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2025-02-17 01:03:54 +09:00
|
|
|
ANVIL_API_PORT = os.environ['ANVIL_API_PORT']
|
|
|
|
ANVIL_API_SESS = os.environ['ANVIL_API_SESS']
|
|
|
|
ANVIL_WIN_ID = get_anvil_window()
|
2025-02-02 06:54:31 +09:00
|
|
|
|
|
|
|
h = {
|
2025-02-17 00:34:25 +09:00
|
|
|
'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()
|
2025-02-17 00:34:25 +09:00
|
|
|
host = arg.host
|
2025-02-17 01:03:54 +09:00
|
|
|
|
|
|
|
if 'ANVIL_API_PORT' not in os.environ:
|
|
|
|
print('not in Anvil.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2025-02-17 00:34:25 +09:00
|
|
|
anvil_Pic(host, arg.url)
|
2025-02-02 06:54:31 +09:00
|
|
|
|