import logging import constants import requests from PIL import Image, ImageDraw, ImageFont # Enable logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) logger = logging.getLogger(__name__) httpx_logger = logging.getLogger('httpx') httpx_logger.setLevel(logging.WARNING) def create_image(product_id, price): width = 1280 height = 800 baseheight = int(height * 0.85) # límite de ancho para la parte izquierda (producto) wlimit = int(((width / 3) * 2) - 80) # límite de ancho para los logos (homelabers y amazon) wlogo = int(width * 0.2) # fuente y tamaño font = ImageFont.truetype("/app/data/fonts/Roboto-Bold.ttf", 90) # inicializamos canvas image = Image.new('RGBA', (width, height), (255, 255, 255)) # logo homelabers, redimensionamos y ponemos en la parte derecha arriba logo_image = Image.open("/app/data/images/homelabs_logo.png").convert("RGBA") lpercent = wlogo / float(logo_image.size[0]) hlogo = int((float(logo_image.size[1]) * float(lpercent))) logo_image = logo_image.resize((wlogo, hlogo), Image.Resampling.LANCZOS) image.paste(logo_image, (int((width / 6) * 5 - logo_image.size[0] / 2), int(height * 0.1)), logo_image) # logo amazon, redimensionamos y ponemos en la parte derecha abajo amazon_logo = Image.open("/app/data/images/Amazon_logo.png").convert("RGBA") lpercent = wlogo / float(amazon_logo.size[0]) hlogo = int((float(amazon_logo.size[1]) * float(lpercent))) amazon_logo = amazon_logo.resize((wlogo, hlogo), Image.Resampling.LANCZOS) image.paste(amazon_logo, (int((width / 6) * 5 - amazon_logo.size[0] / 2), int(height - height * 0.2)), amazon_logo) # dibujamos rectángulo verde externo, con un margen externo y ancho determinado draw = ImageDraw.Draw(image) wtext, htext = draw.textsize(price, font=font) draw.text(((width / 6) * 5 - wtext / 2, height / 2 - htext / 2), price, (0, 0, 0), font=font) draw.rectangle([15, 15, width - 15, height - 15], width = 15, outline="#20e163") # ponemos la imagen del producto en la parte izquierda y se redimensiona dependiendo de lo ancho try: product_image = Image.open(f"/app/data/images/products/{product_id}.jpg") except: product_image = Image.open(f"/app/data/images/placeholder.png") hpercent = (baseheight / float(product_image.size[1])) wsize = int((float(product_image.size[0]) * float(hpercent))) if wsize < wlimit: product_image = product_image.resize((wsize, baseheight), Image.Resampling.LANCZOS) else: wpercent = wlimit / float(product_image.size[0]) hsize = int((float(product_image.size[1]) * float(wpercent))) product_image = product_image.resize((wlimit, hsize), Image.Resampling.LANCZOS) image.paste(product_image, (int((width/3)-(product_image.size[0]/2)), int((height/2) - (product_image.size[1]/2)))) # guardamos la imagen con otro nombre image.save(f"/app/data/images/products/{product_id}_composed.png", quality=95) def new_refer_url(pcode, extraparams=None): return constants.baseURL+pcode+"?tag="+constants.affiliate_tag+extraparams def unshort_url(url): session = requests.Session() #resp = session.head("https://"+url, allow_redirects=True) resp = session.get("https://"+url, allow_redirects=True) logging.info(f"Unshorted URL: {resp.url}") return resp.url