-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
70 lines (57 loc) · 1.53 KB
/
Copy pathutils.py
File metadata and controls
70 lines (57 loc) · 1.53 KB
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
import time
import random
import logging
import re
from urllib.parse import urlparse
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger("lead_research")
def rate_limit(min_delay: float = 1.0, max_delay: float = 2.5):
time.sleep(random.uniform(min_delay, max_delay))
def is_valid_url(url: str) -> bool:
try:
result = urlparse(url)
return all([result.scheme in ("http", "https"), result.netloc])
except Exception:
return False
def deduplicate_leads(leads: list) -> list:
seen = set()
unique = []
for lead in leads:
key = (
lead.get("instagram_url")
or lead.get("phone")
or lead.get("source_url")
or lead.get("name", "")
).strip().lower()
if key and key not in seen:
seen.add(key)
unique.append(lead)
return unique
TITLE_SUFFIXES = [
" | Instagram",
" - Instagram",
" • Instagram",
" (@",
" | Facebook",
" - Facebook",
" | LinkedIn",
" - LinkedIn",
" | YouTube",
" - YouTube",
" - Google",
" | Google",
" – ",
]
def clean_name(name: str) -> str:
if not name:
return ""
for suffix in TITLE_SUFFIXES:
if suffix in name:
name = name[: name.index(suffix)]
return name.strip()
def extract_instagram_handle(url: str) -> str:
m = re.search(r"instagram\.com/([A-Za-z0-9._]+)/?", url)
return f"@{m.group(1)}" if m else ""