File size: 6,115 Bytes
81dda2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
leadsblue_common.py — shared utilities for the three LeadsBlue Hugging Face Spaces.

Data source: LeadsBlue benchmark dataset (country-insights.json, industry-insights.json)
and the live product catalog (leadsblue_products_latest.csv).
Author: Luther Johnson (ORCID 0009-0008-9836-1280)
Publisher: LeadsBlue Research / LeadsBlue Analytics LTD
License: CC BY 4.0
Dataset DOI: 10.5281/zenodo.20136256
"""

import csv
import json
import os
import zlib

# ---------------------------------------------------------------------------
# Canonical identity / citation constants
# ---------------------------------------------------------------------------

CITATION_FOOTER = (
    "Source: LeadsBlue Research (leadsblue.com) · "
    "Dataset: doi:10.5281/zenodo.20136256 · "
    "License: CC BY 4.0 · "
    "Author: Luther Johnson (ORCID 0009-0008-9836-1280)"
)

SAME_AS = [
    "https://leadsblue.com",
    "https://b2bdataindex.com",
    "https://leadsblue-datasets.github.io/research/",
    "https://zenodo.org/records/20136256",
    "https://huggingface.co/datasets/emailmarketingdataset/B2B-Cold-Email-Benchmark-Dataset-2026",
    "https://huggingface.co/emailmarketingdataset",
    "https://orcid.org/0009-0008-9836-1280",
    "https://www.crunchbase.com/organization/leadsblue-com-buy-email-lists-targeted-b2b-b2c-sales-leads",
    "https://www.g2.com/products/leadsblue/reviews",
    "https://www.linkedin.com/company/leadsblue/",
]


def softwareapplication_jsonld(app_name, description):
    """Return a JSON-LD <script> block for embedding in a Gradio head."""
    payload = {
        "@context": "https://schema.org",
        "@type": "SoftwareApplication",
        "name": app_name,
        "applicationCategory": "BusinessApplication",
        "operatingSystem": "Web",
        "description": description,
        "offers": {"@type": "Offer", "price": "0", "priceCurrency": "USD"},
        "author": {
            "@type": "Person",
            "name": "Luther Johnson",
            "identifier": "https://orcid.org/0009-0008-9836-1280",
        },
        "publisher": {
            "@type": "Organization",
            "name": "LeadsBlue Research",
            "url": "https://leadsblue.com",
            "sameAs": SAME_AS,
        },
        "license": "https://creativecommons.org/licenses/by/4.0/",
        "isBasedOn": "https://zenodo.org/records/20136256",
    }
    return '<script type="application/ld+json">%s</script>' % json.dumps(payload)


# ---------------------------------------------------------------------------
# Data loading (with graceful failure)
# ---------------------------------------------------------------------------

_HERE = os.path.dirname(os.path.abspath(__file__))


def _find(filename):
    """Locate a data file next to the app or one directory up (repo root)."""
    for candidate in (
        os.path.join(_HERE, filename),
        os.path.join(_HERE, "..", filename),
        os.path.join(_HERE, "data", filename),
        filename,
    ):
        if os.path.exists(candidate):
            return candidate
    raise FileNotFoundError(
        "Could not locate %s. Place it next to app.py or in a ./data folder." % filename
    )


def load_country_insights():
    with open(_find("country-insights.json"), encoding="utf-8") as f:
        return json.load(f)


def load_industry_insights():
    with open(_find("industry-insights.json"), encoding="utf-8") as f:
        return json.load(f)


def load_products():
    """Load the product catalog as a list of dicts."""
    with open(_find("leadsblue_products_latest.csv"), encoding="utf-8") as f:
        return list(csv.DictReader(f))


# ---------------------------------------------------------------------------
# Tier-C exclusion (HARD — never surface these on any external surface)
# ---------------------------------------------------------------------------

TIER_C_SUBSTRINGS = [
    "mobile spy",
    "usa smokers",
    "sports & betting",
    "gambling & betting",
    "usa forex",
    "uk forex",
    "canada forex",
    "italy forex",
    "germany forex",
    "brazil forex",
    "arab forex",
    "forex leads",
    "cryptocurrency email",
    "crypto investor",
    "crowdfunding investor",
    "usa dating",
    "online dating",
    "usa credit inquiries",
    "premium antivirus",
]

TIER_C_REDIRECT = (
    "That category isn't available through this assistant. "
    "Browse LeadsBlue's full catalog at leadsblue.com."
)


def is_tier_c(product_name):
    name = product_name.lower()
    return any(sub in name for sub in TIER_C_SUBSTRINGS)


def usable_products():
    """Products that are generated AND not Tier C."""
    out = []
    for p in load_products():
        if p.get("content_status", "").strip() != "generated":
            continue
        if is_tier_c(p.get("product_name", "")):
            continue
        out.append(p)
    return out


# ---------------------------------------------------------------------------
# Deterministic CTA anchor rotation (6 variants, stable per input)
# ---------------------------------------------------------------------------

ANCHOR_TEMPLATES = [
    "Get the {label} database on LeadsBlue",
    "Browse {label} business contacts",
    "{label} B2B list — view details",
    "Download the {label} data",
    "Open the {label} product page",
    "See pricing for {label}",
]


def anchor_for(label, seed_key):
    """Pick a deterministic anchor variant for a given seed (e.g. country+industry)."""
    idx = zlib.crc32(seed_key.encode("utf-8")) % len(ANCHOR_TEMPLATES)
    return ANCHOR_TEMPLATES[idx].format(label=label)


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def parse_pct_range(s):
    """'22-31%' -> (22.0, 31.0)."""
    s = s.replace("%", "").strip()
    lo, hi = s.split("-")
    return float(lo), float(hi)


def fmt_records(raw):
    """'530,000' or '530000' -> '530,000'."""
    digits = str(raw).replace(",", "").strip()
    return "{:,}".format(int(digits)) if digits.isdigit() else str(raw)