Spaces:
Sleeping
Sleeping
File size: 7,300 Bytes
408009a 0bdfbcd 46982d8 408009a 8f2a796 408009a 2987310 408009a 2987310 408009a 2987310 408009a 2987310 408009a 2987310 408009a 2987310 408009a 2987310 408009a 2987310 408009a 395877e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
import re
from functools import lru_cache
import gradio as gr
import requests
import os
TOKEN_VALUE=os.getenv("TOKEN_VALUE")
LOGIN = os.getenv("LOGIN")
TOKEN = os.getenv("TOKEN")
BASE_URL = 'https://nethunt.com/api/v1/zapier'
FIELDS = {
'company_code': 'ЄДРПОУ',
'company_codes': 'Всі ЄДРПОУ',
'domain': 'Корпоративний домен',
'linkedin': 'LinkedIn',
'email': 'Email',
'phone': 'Телефон',
'company_name': 'Name',
'first_name': 'Имя',
'last_name': 'Фамилия',
'manager': 'Відповідальний'}
FOLDERS = {
'companies': 'Companies',
'contacts': 'Contacts'}
OUTPUT_LIMIT = 5
def _get_query(
endpoint: str,
params: dict[str, str] = None,
json: dict[str, str] = None,
login: str = LOGIN,
token: str = TOKEN,
base_url: str = BASE_URL
) -> list:
url = f'{base_url}{endpoint}'
responce = requests.get(
url=url, params=params, json=json, auth=(login, token))
responce.raise_for_status()
return responce.json()
@lru_cache(maxsize=None)
def _get_readable_folders(**kwargs) -> list[dict]:
endpoint = '/triggers/writable-folder'
return _get_query(endpoint, **kwargs)
@lru_cache(maxsize=None)
def _get_folder_id(folder_name: str, **kwargs) -> str:
readable_folders = _get_readable_folders(**kwargs)
for folder in readable_folders:
if folder['name'] == folder_name:
return folder['id']
raise ValueError('Wrong folder name.')
def _get_nethunt_records(
folder_name: str,
query: str,
limit: int = OUTPUT_LIMIT,
**kwargs
) -> list[dict]:
folder_id = _get_folder_id(folder_name)
endpoint = f'/searches/find-record/{folder_id}'
params = {
'query': query,
'limit': limit}
return _get_query(endpoint, params, **kwargs)
def _clean_found_records(
folder_name: str,
found_records: str,
check_type: str
) -> list:
result_list = []
for v in found_records:
record = {
'folder_name': folder_name,
'duplicate_type': check_type,
'record_id': v.get('id'),
'link': v.get('link'),
'manager': v.get('fields', {}).get(FIELDS['manager'], '')}
if folder_name == FOLDERS['companies']:
company_name = v.get('fields', {}) \
.get(FIELDS['company_name'], '')
record['name'] = company_name
if folder_name == FOLDERS['contacts']:
first_name = v.get('fields', {}) \
.get(FIELDS['first_name'], '')
last_name = v.get('fields', {}) \
.get(FIELDS['last_name'], '')
record['name'] = (first_name + ' ' + last_name).strip()
result_list.append(record)
return result_list
def _check_field(
folder_name: str,
query: str,
check_type: str
) -> list:
found_records = _get_nethunt_records(folder_name, query=query)
if len(found_records) == 0:
return []
return _clean_found_records(folder_name, found_records, check_type)
def _check_company_code(code: str) -> list:
code = code.strip()
folder = FOLDERS['companies']
query = f'''
("{FIELDS['company_code']}":"{code}")
OR ("{FIELDS['company_codes']}":"{code}")'''
return _check_field(folder, query, 'company_code')
def _check_company_domain(domain: str) -> list:
domain = domain.replace('@', '').strip()
folder = FOLDERS['companies']
query = f'''"{FIELDS['domain']}":"{domain}"'''
return _check_field(folder, query, 'domain')
def _check_company_linkedin(linkedin: str) -> list:
linkedin = linkedin.strip()
folder = FOLDERS['companies']
query = f'''"{FIELDS['linkedin']}":"{linkedin}"'''
return _check_field(folder, query, 'company_linkedin')
def _check_contact_email(email: str) -> list:
email = email.lower().strip()
folder = FOLDERS['contacts']
query = f'''"{FIELDS['email']}":"{email}"'''
return _check_field(folder, query, 'email')
def _check_contact_phone(phone: str) -> list:
phone = re.sub(r'[^\d+]', '', phone)
folder = FOLDERS['contacts']
query = f'''"{FIELDS['phone']}":"{phone}"'''
return _check_field(folder, query, 'phone')
def _check_contact_linkedin(linkedin: str) -> list:
linkedin = linkedin.strip()
folder = FOLDERS['contacts']
query = f'''"{FIELDS['linkedin']}":"{linkedin}"'''
return _check_field(folder, query, 'contact_linkedin')
def check_record(
company_code: str = None,
domain: str = None,
company_linkedin: str = None,
email: str = None,
phone: str = None,
contact_linkedin: str = None
) -> list:
found_records = []
checks = {
_check_company_code: company_code,
_check_company_domain: domain,
_check_company_linkedin: company_linkedin,
_check_contact_email: email,
_check_contact_phone: phone,
_check_contact_linkedin: contact_linkedin}
for check, value in checks.items():
if value:
found_records.extend(check(value))
# Апгрейд для вывода
result = '<span>'
count = 0
for item in found_records:
if item['folder_name'] == 'Companies':
icon = '🏢'
elif item['folder_name'] == 'Contacts':
icon = '👨💼'
count = count + 1
result += (
f"{icon} "
f"<b>{item['name']}</b> | "
f"Manager: {item['manager']} | "
f"Found by: {item['duplicate_type']} | "
f"Link: <a href=\"{str(item['link'])}\">"
f"{item['record_id']}</a><br><br>")
if count == OUTPUT_LIMIT:
break
if count == 0:
result += "<h2>Nothing found</h2>"
result += "</span>"
return result
def generate(
company_code, domain, company_linkedin,
email, phone, contact_linkedin, token):
if (TOKEN_VALUE == token): return check_record(
company_code=company_code,
domain=domain,
company_linkedin=company_linkedin,
email=email,
phone=phone,
contact_linkedin=contact_linkedin)
else: return "Token error"
title = "NetHunt search"
css = """
footer {visibility: hidden}
"""
with gr.Blocks(css=css, title=title) as demo:
gr.Markdown(
"""
# NetHuntCrm Search
"""
)
with gr.Row():
with gr.Column():
input_7 = gr.Text(label="Token")
input_1 = gr.Text(label="Сompany Code")
input_2 = gr.Textbox(label="Corp Domain")
gr.Markdown("""
""")
greet_btn = gr.Button("Search")
with gr.Column():
input_4 = gr.Textbox(label="Email")
input_5 = gr.Textbox(label="Phone")
input_6 = gr.Textbox(label="Contact LinkedIn")
input_3 = gr.Textbox(label="Company LinkedIn")
output = gr.outputs.HTML()
input = [input_1, input_2, input_3, input_4, input_5, input_6, input_7]
greet_btn.click(fn=generate, inputs=input, outputs=output)
demo.launch(debug=True, share=False)
|