1
2
3
4
5
6
7
8import http from 'node:http';
9import { Actor, log } from 'apify';
10import { preprocess } from './preprocessor.js';
11
12
13
14
15
16
17function normalizeItems(items) {
18 return items.map((item) => {
19 if (typeof item === 'string') {
20 return item;
21 }
22 if (item && typeof item === 'object' && 'input' in item) {
23 return String(item.input ?? '');
24 }
25 return String(item ?? '');
26 });
27}
28
29await Actor.init();
30
31if (Actor.config.get('metaOrigin') === 'STANDBY') {
32 startStandbyServer();
33} else {
34 await runBatchMode();
35 await Actor.exit();
36}
37
38
39
40
41
42function startStandbyServer() {
43 const port = Actor.config.get('containerPort');
44
45 const server = http.createServer(async (req, res) => {
46
47 if (req.headers['x-apify-container-server-readiness-probe']) {
48 res.writeHead(200, { 'Content-Type': 'application/json' });
49 res.end(JSON.stringify({ status: 'ready' }));
50 return;
51 }
52
53
54 if (req.method !== 'GET') {
55 res.writeHead(405, { 'Content-Type': 'application/json' });
56 res.end(JSON.stringify({ error: 'Method not allowed. Use GET.' }));
57 return;
58 }
59
60
61 const reqUrl = new URL(req.url, `http://${req.headers.host}`);
62 if (!reqUrl.searchParams.get('input')) {
63 res.writeHead(200, { 'Content-Type': 'text/html' });
64 res.end(getLandingPageHtml());
65 return;
66 }
67
68 try {
69 const result = handleStandbyRequest(reqUrl);
70 await Actor.pushData(result);
71
72 res.writeHead(200, { 'Content-Type': 'application/json' });
73 res.end(JSON.stringify(result));
74 } catch (err) {
75 const statusCode = err.statusCode || 500;
76 res.writeHead(statusCode, { 'Content-Type': 'application/json' });
77 res.end(JSON.stringify({ error: err.message }));
78 }
79 });
80
81 server.listen(port, () => {
82 console.log(`Superclean Places Standby server listening on port ${port}`);
83 });
84}
85
86function handleStandbyRequest(reqUrl) {
87 const input = reqUrl.searchParams.get('input');
88
89 if (!input) {
90 const err = new Error("Provide an 'input' query parameter with the location to clean.");
91 err.statusCode = 400;
92 throw err;
93 }
94
95 const style = reqUrl.searchParams.get('style') || 'short';
96
97 const validStyles = ['short', 'long', 'components'];
98 if (!validStyles.includes(style)) {
99 const err = new Error(`Invalid style "${style}". Must be one of: ${validStyles.join(', ')}`);
100 err.statusCode = 400;
101 throw err;
102 }
103
104 const result = preprocess(input, style);
105
106 return {
107 id: 1,
108 input,
109 output: result.value,
110 city: result.city,
111 stateOrProvince: result.stateOrProvince,
112 country: result.country,
113 confidence: Math.round(result.confidence * 100) / 100,
114 };
115}
116
117
118
119
120
121async function runBatchMode() {
122 const input = await Actor.getInput();
123
124 if (!input) {
125 await Actor.fail('No input provided. Example: {"items": ["New York, NY"]}');
126 return;
127 }
128
129 let { items: rawItems = [], item, style = 'short' } = input;
130
131
132 if (typeof item === 'string' && item.trim()) {
133 if (!Array.isArray(rawItems)) rawItems = [];
134 rawItems = [item.trim(), ...rawItems];
135 }
136
137
138 if (typeof rawItems === 'string') rawItems = [rawItems];
139
140 if (!Array.isArray(rawItems) || rawItems.length === 0) {
141 await Actor.fail('Input required: provide "items" (array) or "item" (single string). Example: {"items": ["New York, NY"]}');
142 return;
143 }
144
145 const validStyles = ['short', 'long', 'components'];
146 if (!validStyles.includes(style)) {
147 throw new Error(`Invalid style "${style}". Must be one of: ${validStyles.join(', ')}`);
148 }
149
150
151 const items = normalizeItems(rawItems);
152
153 log.info(`Processing ${items.length} location(s) with style "${style}"`);
154
155 const results = [];
156 let processed = 0;
157
158 for (let i = 0; i < items.length; i++) {
159 const original = items[i];
160 const id = i + 1;
161
162 try {
163 const result = preprocess(original, style);
164
165 results.push({
166 id,
167 input: original,
168 output: result.value,
169 city: result.city,
170 stateOrProvince: result.stateOrProvince,
171 country: result.country,
172 confidence: Math.round(result.confidence * 100) / 100,
173 });
174
175 processed++;
176
177 if (processed % 100 === 0) {
178 log.info(`Processed ${processed}/${items.length} locations`);
179 }
180 } catch (err) {
181 log.warning(`Failed to process item ${id}: ${err.message}`);
182 results.push({
183 id,
184 input: original,
185 output: original,
186 city: null,
187 stateOrProvince: null,
188 country: null,
189 confidence: 0,
190 error: err.message,
191 });
192 }
193 }
194
195
196
197 await Actor.pushData(results);
198
199 log.info(`Completed processing ${results.length} location(s)`);
200
201
202 const kvStore = await Actor.openKeyValueStore();
203 await kvStore.setValue('summary', {
204 totalItems: results.length,
205 processedAt: new Date().toISOString(),
206 style,
207 sampleResults: results.slice(0, 5),
208 });
209}
210
211
212
213
214
215function getLandingPageHtml() {
216 return `<!DOCTYPE html>
217<html lang="en">
218<head>
219<meta charset="utf-8">
220<meta name="viewport" content="width=device-width, initial-scale=1">
221<title>Superclean Places — Superlative</title>
222<link rel="preconnect" href="https://fonts.googleapis.com">
223<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
224<link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;700&family=Space+Mono:wght@400;700&display=swap" rel="stylesheet">
225<style>
226:root{--color-bg:#0a0e14;--color-surface:#12171f;--color-border:#1e2632;--color-text:#e8eaed;--color-text-muted:#8b95a5;--color-accent:#4ade80;--color-accent-dim:rgba(74,222,128,0.15);--font-display:'Space Mono',monospace;--font-body:'DM Sans',sans-serif}
227*{margin:0;padding:0;box-sizing:border-box}
228body{font-family:var(--font-body);background:var(--color-bg);color:var(--color-text);line-height:1.6;min-height:100vh;overflow-x:hidden}
229a{color:var(--color-accent);text-decoration:none;transition:color .2s ease}
230a:hover{color:#6ee7a0}
231.grid-bg{position:fixed;top:0;left:0;width:100%;height:100%;background-image:linear-gradient(var(--color-border) 1px,transparent 1px),linear-gradient(90deg,var(--color-border) 1px,transparent 1px);background-size:60px 60px;opacity:.3;pointer-events:none;z-index:0}
232.accent-line{position:fixed;top:0;left:0;width:100%;height:2px;background:linear-gradient(90deg,transparent 0%,var(--color-accent) 50%,transparent 100%);animation:shimmer 3s ease-in-out infinite;z-index:10}
233@keyframes shimmer{0%,100%{transform:translateX(-100%)}50%{transform:translateX(100%)}}
234@keyframes fadeUp{from{opacity:0;transform:translateY(20px)}to{opacity:1;transform:translateY(0)}}
235.container{position:relative;z-index:1;max-width:900px;margin:0 auto;padding:0 24px}
236header{padding:32px 0}
237.logo{font-family:var(--font-display);font-size:14px;font-weight:700;letter-spacing:.1em;text-transform:uppercase;color:var(--color-text);display:flex;align-items:center;gap:10px;text-decoration:none}
238.logo:hover{color:var(--color-text)}
239.logo-mark{width:28px;height:28px;background:var(--color-accent);display:flex;align-items:center;justify-content:center}
240.logo-mark svg{width:16px;height:16px}
241.hero{padding:80px 0 60px;animation:fadeUp .8s ease-out}
242.hero-label{font-family:var(--font-display);font-size:12px;font-weight:400;letter-spacing:.15em;text-transform:uppercase;color:var(--color-accent);margin-bottom:24px;display:flex;align-items:center;gap:12px}
243.hero-label::before{content:'';width:24px;height:1px;background:var(--color-accent)}
244h1{font-family:var(--font-display);font-size:clamp(28px,5vw,44px);font-weight:700;line-height:1.15;margin-bottom:20px;letter-spacing:-.02em}
245h1 .highlight{color:var(--color-accent)}
246.hero-desc{font-size:18px;color:var(--color-text-muted);max-width:540px;margin-bottom:28px;line-height:1.7}
247.hero-link{font-family:var(--font-display);font-size:13px;letter-spacing:.05em;color:var(--color-accent);display:inline-flex;align-items:center;gap:6px}
248.section{padding:60px 0;border-top:1px solid var(--color-border);animation:fadeUp .8s ease-out .2s backwards}
249.section-label{font-family:var(--font-display);font-size:11px;font-weight:400;letter-spacing:.2em;text-transform:uppercase;color:var(--color-text-muted);margin-bottom:24px}
250h2{font-family:var(--font-display);font-size:20px;font-weight:700;margin-bottom:16px;letter-spacing:-.01em}
251p,li{font-size:15px;color:var(--color-text-muted);line-height:1.7}
252li{margin-bottom:6px}
253ul{margin:.5rem 0 1rem 1.25rem}
254strong{color:var(--color-text);font-weight:600}
255pre{background:var(--color-surface);color:var(--color-text);padding:16px 20px;overflow-x:auto;margin:12px 0 16px;font-family:var(--font-display);font-size:13px;line-height:1.6;border:1px solid var(--color-border)}
256code{font-family:var(--font-display);font-size:13px}
257p code,li code,td code{background:var(--color-accent-dim);color:var(--color-accent);padding:2px 6px}
258table{width:100%;border-collapse:collapse;margin:12px 0 16px;font-size:14px}
259th,td{border:1px solid var(--color-border);padding:10px 14px;text-align:left}
260th{background:var(--color-surface);color:var(--color-text-muted);font-family:var(--font-display);font-size:12px;font-weight:400;letter-spacing:.05em;text-transform:uppercase}
261td{color:var(--color-text-muted)}
262.badge{display:inline-block;background:var(--color-accent-dim);color:var(--color-accent);padding:3px 8px;font-family:var(--font-display);font-size:11px;font-weight:400;letter-spacing:.05em}
263.note{font-size:13px;color:var(--color-text-muted);margin-top:8px}
264.product-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:16px;margin-top:24px}
265.product-card{background:var(--color-surface);border:1px solid var(--color-border);padding:24px;transition:all .3s ease;text-decoration:none;color:inherit;display:block}
266.product-card:hover{border-color:var(--color-accent);transform:translateY(-2px)}
267.product-name{font-family:var(--font-display);font-size:14px;font-weight:700;margin-bottom:8px;color:var(--color-text);display:flex;align-items:center;gap:8px}
268.product-name .status{font-size:9px;font-weight:400;padding:3px 6px;background:var(--color-accent-dim);color:var(--color-accent);letter-spacing:.1em}
269.product-desc{font-size:14px;color:var(--color-text-muted);line-height:1.5}
270footer{padding:48px 0;border-top:1px solid var(--color-border)}
271.footer-content{display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:24px}
272.footer-text{font-size:13px;color:var(--color-text-muted)}
273.footer-links{display:flex;gap:24px}
274.footer-links a{font-family:var(--font-display);font-size:12px;color:var(--color-text-muted);letter-spacing:.05em}
275.footer-links a:hover{color:var(--color-accent)}
276@media(max-width:600px){.hero{padding:60px 0 40px}.section{padding:40px 0}.footer-content{flex-direction:column;align-items:flex-start}pre{font-size:12px;padding:12px 14px}}
277</style>
278</head>
279<body>
280<div class="grid-bg"></div>
281<div class="accent-line"></div>
282
283<div class="container">
284 <header>
285 <a href="https://apify.com/superlativetech?fpr=8e9l1" class="logo">
286 <div class="logo-mark">
287 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
288 <polyline points="4 17 10 11 4 5"></polyline>
289 <line x1="12" y1="19" x2="20" y2="19"></line>
290 </svg>
291 </div>
292 Superlative
293 </a>
294 </header>
295
296 <section class="hero">
297 <div class="hero-label">Superclean</div>
298 <h1>Superclean <span class="highlight">Places</span></h1>
299 <p class="hero-desc">Instant location parsing and normalization. Extract city, state, and country from messy location strings. Sub-second responses via Standby API.</p>
300 <a href="https://apify.com/superlativetech/superclean-places?fpr=8e9l1" class="hero-link">View on Apify Store →</a>
301 </section>
302
303 <section class="section">
304 <div class="section-label">Features</div>
305 <h2>What this does</h2>
306 <ul>
307 <li><strong>Parses locations</strong> — Extract city, state/province, and country from free-text</li>
308 <li><strong>Normalizes format</strong> — Standardize abbreviations, fix capitalization</li>
309 <li><strong>Multiple styles</strong> — Short, long, or component output formats</li>
310 <li><strong>Batch mode</strong> — Process hundreds of locations via the standard Actor run</li>
311 </ul>
312 </section>
313
314 <section class="section">
315 <div class="section-label">Getting Started</div>
316 <h2>Quick start</h2>
317 <p>Replace <code>YOUR_TOKEN</code> with your Apify API token.</p>
318
319 <p style="margin-top:16px"><strong>Parse a location (short format):</strong></p>
320 <pre>curl "https://superlativetech--superclean-places.apify.actor?token=YOUR_TOKEN&input=san+francisco,+ca"</pre>
321
322 <p><strong>Long format:</strong></p>
323 <pre>curl "https://superlativetech--superclean-places.apify.actor?token=YOUR_TOKEN&input=san+francisco,+ca&style=long"</pre>
324
325 <p><strong>Components only:</strong></p>
326 <pre>curl "https://superlativetech--superclean-places.apify.actor?token=YOUR_TOKEN&input=new+york,+ny,+usa&style=components"</pre>
327 </section>
328
329 <section class="section">
330 <div class="section-label">Reference</div>
331 <h2>Query parameters</h2>
332 <table>
333 <tr><th>Parameter</th><th>Required</th><th>Description</th></tr>
334 <tr><td><code>input</code></td><td>Yes</td><td>Location string to parse</td></tr>
335 <tr><td><code>style</code></td><td>No</td><td>Output format: <code>short</code> (default), <code>long</code>, or <code>components</code></td></tr>
336 <tr><td><code>token</code></td><td>Yes</td><td>Your Apify API token</td></tr>
337 </table>
338 </section>
339
340 <section class="section">
341 <div class="section-label">Response</div>
342 <h2>Response format</h2>
343 <pre>{
344 "id": 1,
345 "input": "san francisco, ca",
346 "output": "San Francisco, CA",
347 "city": "San Francisco",
348 "stateOrProvince": "CA",
349 "country": "US",
350 "confidence": 0.9
351}</pre>
352 </section>
353
354 <section class="section">
355 <div class="section-label">Cost</div>
356 <h2>Pricing</h2>
357 <p>Free to use — you only pay standard Apify platform usage.</p>
358 </section>
359
360 <section class="section">
361 <div class="section-label">Auth</div>
362 <h2>Authentication</h2>
363 <p>Authenticate using either method:</p>
364 <ul>
365 <li>Query parameter: <code>?token=YOUR_APIFY_TOKEN</code></li>
366 <li>Header: <code>Authorization: Bearer YOUR_APIFY_TOKEN</code></li>
367 </ul>
368 <p style="margin-top:8px">Get your token from <a href="https://console.apify.com/settings/integrations">Apify Console → Settings → Integrations</a>.</p>
369 </section>
370
371 <section class="section">
372 <div class="section-label">Superlative</div>
373 <h2>More from Superlative</h2>
374 <div class="product-grid">
375 <a href="https://apify.com/superlativetech/superclean-company-names?fpr=8e9l1" class="product-card">
376 <div class="product-name">Company Names <span class="status">Live</span></div>
377 <p class="product-desc">Normalize company names for CRM and cold email outreach.</p>
378 </a>
379 <a href="https://apify.com/superlativetech/superclean-job-titles?fpr=8e9l1" class="product-card">
380 <div class="product-name">Job Titles <span class="status">Live</span></div>
381 <p class="product-desc">Standardize job titles for outreach sequences.</p>
382 </a>
383 <a href="https://apify.com/superlativetech/superclean-person-names?fpr=8e9l1" class="product-card">
384 <div class="product-name">Person Names <span class="status">Live</span></div>
385 <p class="product-desc">Clean and format person names for personalization.</p>
386 </a>
387 <a href="https://apify.com/superlativetech/superclean-product-names?fpr=8e9l1" class="product-card">
388 <div class="product-name">Product Names <span class="status">Live</span></div>
389 <p class="product-desc">Normalize product and brand names from exports.</p>
390 </a>
391 <a href="https://apify.com/superlativetech/superclean-urls?fpr=8e9l1" class="product-card">
392 <div class="product-name">URLs <span class="status">Live</span></div>
393 <p class="product-desc">Clean and normalize URLs from lead data.</p>
394 </a>
395 <a href="https://apify.com/superlativetech/superclean-phone-numbers?fpr=8e9l1" class="product-card">
396 <div class="product-name">Phone Numbers <span class="status">Live</span></div>
397 <p class="product-desc">Format and validate phone numbers.</p>
398 </a>
399 <a href="https://apify.com/superlativetech/dns-lookup?fpr=8e9l1" class="product-card">
400 <div class="product-name">Supernet DNS Lookup <span class="status">Live</span></div>
401 <p class="product-desc">Look up DNS records for any domain.</p>
402 </a>
403 <a href="https://apify.com/superlativetech/http-api?fpr=8e9l1" class="product-card">
404 <div class="product-name">HTTP API <span class="status">Live</span></div>
405 <p class="product-desc">General-purpose HTTP request utility.</p>
406 </a>
407 </div>
408 </section>
409
410 <footer>
411 <div class="footer-content">
412 <p class="footer-text">© 2026 Superlative</p>
413 <div class="footer-links">
414 <a href="https://apify.com/superlativetech?fpr=8e9l1">Apify Store</a>
415 </div>
416 </div>
417 </footer>
418</div>
419
420</body>
421</html>`;
422}