o
    sg[                      @   s8  d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlZej	
d d dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ ejd
 Zdd Zdd Zdd Zdedee fddZdee defddZ	d2dededefddZdejfdd Z d3d"d#Z!d$ed%edefd&d'Z"e#d(krd d)l$m%Z% d*g d+iZ&d,Z'e&( D ]p\Z)Z*d-e) Z+e'd.e)  Z,ej-e,d/d0 e.e, e*D ]JZ/z/e"e/e+Z0e%1e0Z0ee0j0Z2ej	3e,d. se-e' e,d. e0j4 d e2j5 Z6ee2e6 W q e7y Z8 ze9d1e0j0 W Y dZ8[8qdZ8[8ww e.e' qdS dS )4    NCounter)DictList.)svg2png)Image)remove)pic_file_extension)LogoBRAND_FETCH_API_KEYc                 C   s   ddt  d}d| dd  }ztj||dd}|jd	kr!W d
S W n tjjy.   Y d
S w | }d|v r?t|d }|S d
}|S )a  
    Fetches brand information from a given URL using the brandfetch API

    Args:
        url (str): The URL to fetch the text from.

    Returns:
        brand (dict):
            company_name (str): The name of the company
            logos (dict):
                logo_url (str): The url of the logo
                logo_extension (int): The file extension of the logo
                logo_dark (bool): If the logo is dark or not
            linkedin (str): The linkedin url of the company
    zapplication/jsonzBearer )acceptAuthorizationz$https://api.brandfetch.io/v2/brands/z//      )headerstimeout   Nlogos)	BRAND_FETCH_APIsplitrequestsgetstatus_code
exceptionsReadTimeoutjsonfind_logo_from_logos)urlrequest_headerrequest_urlreqlogo r#   D/var/www/html/XCapMarket/services/company_profile/logo_standalone.pyget_brand_from_url   s"   
r%   c                 C   s   t | dkrtd dS d}d}d}d}| D ]?}|d D ]8}|d }|d dkr0t|d	  d nd
}|d	 }	|d dk}
t |dksF||k rT|d dkrT|}|}|
}|	}qqt||||dS )z%Find the logo from the logos returnedr   zNO LOGO FOUNDF formatssrcthemedarkformat
   typer"   )logo_urllogo_extension	logo_darklogo_format)lenprintr
   r   )r   final_logo_urlfinal_logo_extensionfinal_logo_darkfinal_logo_formatr"   fr.   r/   r1   r0   r#   r#   r$   r   <   s8   r   c                 C   sz   t |d}|tj| jddj W d   n1 sw   Y  | jds3| jds3| jdr;t|dd	 dS dS )
zSave the logo to a filewbr,   )r   Nz.pngz.jpgz.jpegT)
save_image)openwriter   r   r.   contentendswithremove_background)r"   filenamer8   r#   r#   r$   	save_logoa   s   


rA   	file_pathreturnc                 C   s:   t | d}t|}W d    |S 1 sw   Y  |S )Nr)r;   r   load)rB   r8   datar#   r#   r$   load_json_datao   s   
rG   rF   c                 C   s@   t |d}tj| |dd W d    d S 1 sw   Y  d S )Nw   )indent)r;   r   dump)rF   rB   r8   r#   r#   r$   save_json_datau   s   "rL   F
image_pathoutput_pathr:   c                 C   sv   | }|}t |d}t|}|d}| }||}|}|r9|du r4d| ddd d }|| |S )z!remove the background of an imageRGBALNr&   r   z
_no_bg.png)	r   r;   convertremove_background_colorgetbboxcropjoinr   save)rM   rN   r:   
input_path_input_input_gray_scaleoutputr#   r#   r$   r?   z   s   


r?   imagec           	      C   s"  | j \}}g }t|D ]}|| |df qt|D ]}|| ||d f qt|D ]}|| d|f q/t|D ]}|| |d |f q@t|dd d }td||f}t|D ](}t|D ]!}| ||f}t||ddr|	||fd ql|	||f| qlqf|S )Nr   r   rO   g?	tolerance)r   r   r   r   )
sizerangeappendgetpixelr   most_commonr   newis_color_within_toleranceputpixel)	r\   widthheightborder_pixelsxymost_common_color	new_imagecurrent_colorr#   r#   r$   rS      s,   

rS   皙?c                    s   t  fddt| |D S )Nc                 3   s(    | ]\}}t ||  d  kV  qdS )   N)abs).0ctr]   r#   r$   	<genexpr>   s   & z,is_color_within_tolerance.<locals>.<genexpr>)allzip)colortarget_colorr^   r#   r]   r$   re      s   re   company_nameindustryc                 C   s   ddl m} ddlm} | ds| dr| S | }||  d| dd}t|dkr/d	S |d
d |D }t|dkr@d	S t||jdS )z.Get the url from the company name and industryr   r   )GoogleSearchzhttp://zhttps:// z logosearchFc                 S   s   g | ]
}t |d  jqS )link)
url_parser
parsed_urlr   )rr   resultr#   r#   r$   
<listcomp>   s    z%get_url_from_name.<locals>.<listcomp>)key)	collectionsr   utils.search_googler|   
startswith_search_googler2   maxr   )rz   r{   r   r|   search_googleresultsdomain_countr#   r#   r$   get_url_from_name   s   r   __main__)r   z	Law firms)MintzzMorgan LewisCooleyzKirkland & Ellisz	Jones DayzCovington & BurlingzWilliams and ConnelyzHogan Lovellsz\C:\Users\sagar\X Cap Market\X Cap Market - Customers\Kenyon\projects\Barrister Digital\logoszAI \T)exist_okzError with: )NF)ro   ):r   shutilsysr   r   typingr   r   r   pathra   oscairosvgr   PILr   rembgr	   configs.configr
   2services.company_profile.data_classes.company_infor   environr   r%   r   rA   strrG   rL   boolr?   rS   re   r   __name__utilsr   	companieslocationitemssectorcompanies_listr{   sector_locationmakedirschdircompanyr   r   r   existsdomainr1   location_new	Exceptioner3   r#   r#   r#   r$   <module>   st    
%%

-
	





