@rankcli/agent-runtime 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/README.md +242 -0
  2. package/dist/analyzer-2CSWIQGD.mjs +6 -0
  3. package/dist/chunk-YNZYHEYM.mjs +774 -0
  4. package/dist/index.d.mts +4012 -0
  5. package/dist/index.d.ts +4012 -0
  6. package/dist/index.js +29672 -0
  7. package/dist/index.mjs +28602 -0
  8. package/package.json +53 -0
  9. package/scripts/build-deno.ts +134 -0
  10. package/src/audit/ai/analyzer.ts +347 -0
  11. package/src/audit/ai/index.ts +29 -0
  12. package/src/audit/ai/prompts/content-analysis.ts +271 -0
  13. package/src/audit/ai/types.ts +179 -0
  14. package/src/audit/checks/additional-checks.ts +439 -0
  15. package/src/audit/checks/ai-citation-worthiness.ts +399 -0
  16. package/src/audit/checks/ai-content-structure.ts +325 -0
  17. package/src/audit/checks/ai-readiness.ts +339 -0
  18. package/src/audit/checks/anchor-text.ts +179 -0
  19. package/src/audit/checks/answer-conciseness.ts +322 -0
  20. package/src/audit/checks/asset-minification.ts +270 -0
  21. package/src/audit/checks/bing-optimization.ts +206 -0
  22. package/src/audit/checks/brand-mention-optimization.ts +349 -0
  23. package/src/audit/checks/caching-headers.ts +305 -0
  24. package/src/audit/checks/canonical-advanced.ts +150 -0
  25. package/src/audit/checks/canonical-domain.ts +196 -0
  26. package/src/audit/checks/citation-quality.ts +358 -0
  27. package/src/audit/checks/client-rendering.ts +542 -0
  28. package/src/audit/checks/color-contrast.ts +342 -0
  29. package/src/audit/checks/content-freshness.ts +170 -0
  30. package/src/audit/checks/content-science.ts +589 -0
  31. package/src/audit/checks/conversion-elements.ts +526 -0
  32. package/src/audit/checks/crawlability.ts +220 -0
  33. package/src/audit/checks/directory-listing.ts +172 -0
  34. package/src/audit/checks/dom-analysis.ts +191 -0
  35. package/src/audit/checks/dom-size.ts +246 -0
  36. package/src/audit/checks/duplicate-content.ts +194 -0
  37. package/src/audit/checks/eeat-signals.ts +990 -0
  38. package/src/audit/checks/entity-seo.ts +396 -0
  39. package/src/audit/checks/featured-snippet.ts +473 -0
  40. package/src/audit/checks/freshness-signals.ts +443 -0
  41. package/src/audit/checks/funnel-intent.ts +463 -0
  42. package/src/audit/checks/hreflang.ts +174 -0
  43. package/src/audit/checks/html-compliance.ts +302 -0
  44. package/src/audit/checks/image-dimensions.ts +167 -0
  45. package/src/audit/checks/images.ts +160 -0
  46. package/src/audit/checks/indexnow.ts +275 -0
  47. package/src/audit/checks/interactive-tools.ts +475 -0
  48. package/src/audit/checks/internal-link-graph.ts +436 -0
  49. package/src/audit/checks/keyword-analysis.ts +239 -0
  50. package/src/audit/checks/keyword-cannibalization.ts +385 -0
  51. package/src/audit/checks/keyword-placement.ts +471 -0
  52. package/src/audit/checks/links.ts +203 -0
  53. package/src/audit/checks/llms-txt.ts +224 -0
  54. package/src/audit/checks/local-seo.ts +296 -0
  55. package/src/audit/checks/mobile.ts +167 -0
  56. package/src/audit/checks/modern-images.ts +226 -0
  57. package/src/audit/checks/navboost-signals.ts +395 -0
  58. package/src/audit/checks/on-page.ts +209 -0
  59. package/src/audit/checks/page-resources.ts +285 -0
  60. package/src/audit/checks/pagination.ts +180 -0
  61. package/src/audit/checks/performance.ts +153 -0
  62. package/src/audit/checks/platform-presence.ts +580 -0
  63. package/src/audit/checks/redirect-analysis.ts +153 -0
  64. package/src/audit/checks/redirect-chain.ts +389 -0
  65. package/src/audit/checks/resource-hints.ts +420 -0
  66. package/src/audit/checks/responsive-css.ts +247 -0
  67. package/src/audit/checks/responsive-images.ts +396 -0
  68. package/src/audit/checks/review-ecosystem.ts +415 -0
  69. package/src/audit/checks/robots-validation.ts +373 -0
  70. package/src/audit/checks/security-headers.ts +172 -0
  71. package/src/audit/checks/security.ts +144 -0
  72. package/src/audit/checks/serp-preview.ts +251 -0
  73. package/src/audit/checks/site-maturity.ts +444 -0
  74. package/src/audit/checks/social-meta.test.ts +275 -0
  75. package/src/audit/checks/social-meta.ts +134 -0
  76. package/src/audit/checks/soft-404.ts +151 -0
  77. package/src/audit/checks/structured-data.ts +238 -0
  78. package/src/audit/checks/tech-detection.ts +496 -0
  79. package/src/audit/checks/topical-clusters.ts +435 -0
  80. package/src/audit/checks/tracker-bloat.ts +462 -0
  81. package/src/audit/checks/tracking-verification.test.ts +371 -0
  82. package/src/audit/checks/tracking-verification.ts +636 -0
  83. package/src/audit/checks/url-safety.ts +682 -0
  84. package/src/audit/deno-entry.ts +66 -0
  85. package/src/audit/discovery/index.ts +15 -0
  86. package/src/audit/discovery/link-crawler.ts +232 -0
  87. package/src/audit/discovery/repo-routes.ts +347 -0
  88. package/src/audit/engine.ts +620 -0
  89. package/src/audit/fixes/index.ts +209 -0
  90. package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
  91. package/src/audit/fixes/social-meta-fixes.ts +463 -0
  92. package/src/audit/index.ts +74 -0
  93. package/src/audit/runner.test.ts +299 -0
  94. package/src/audit/runner.ts +130 -0
  95. package/src/audit/types.ts +1953 -0
  96. package/src/content/featured-snippet.ts +367 -0
  97. package/src/content/generator.test.ts +534 -0
  98. package/src/content/generator.ts +501 -0
  99. package/src/content/headline.ts +317 -0
  100. package/src/content/index.ts +62 -0
  101. package/src/content/intent.ts +258 -0
  102. package/src/content/keyword-density.ts +349 -0
  103. package/src/content/readability.ts +262 -0
  104. package/src/executor.ts +336 -0
  105. package/src/fixer.ts +416 -0
  106. package/src/frameworks/detector.test.ts +248 -0
  107. package/src/frameworks/detector.ts +371 -0
  108. package/src/frameworks/index.ts +68 -0
  109. package/src/frameworks/recipes/angular.yaml +171 -0
  110. package/src/frameworks/recipes/astro.yaml +206 -0
  111. package/src/frameworks/recipes/django.yaml +180 -0
  112. package/src/frameworks/recipes/laravel.yaml +137 -0
  113. package/src/frameworks/recipes/nextjs.yaml +268 -0
  114. package/src/frameworks/recipes/nuxt.yaml +175 -0
  115. package/src/frameworks/recipes/rails.yaml +188 -0
  116. package/src/frameworks/recipes/react.yaml +202 -0
  117. package/src/frameworks/recipes/sveltekit.yaml +154 -0
  118. package/src/frameworks/recipes/vue.yaml +137 -0
  119. package/src/frameworks/recipes/wordpress.yaml +209 -0
  120. package/src/frameworks/suggestion-engine.ts +320 -0
  121. package/src/geo/geo-content.test.ts +305 -0
  122. package/src/geo/geo-content.ts +266 -0
  123. package/src/geo/geo-history.test.ts +473 -0
  124. package/src/geo/geo-history.ts +433 -0
  125. package/src/geo/geo-tracker.test.ts +359 -0
  126. package/src/geo/geo-tracker.ts +411 -0
  127. package/src/geo/index.ts +10 -0
  128. package/src/git/commit-helper.test.ts +261 -0
  129. package/src/git/commit-helper.ts +329 -0
  130. package/src/git/index.ts +12 -0
  131. package/src/git/pr-helper.test.ts +284 -0
  132. package/src/git/pr-helper.ts +307 -0
  133. package/src/index.ts +66 -0
  134. package/src/keywords/ai-keyword-engine.ts +1062 -0
  135. package/src/keywords/ai-summarizer.ts +387 -0
  136. package/src/keywords/ci-mode.ts +555 -0
  137. package/src/keywords/engine.ts +359 -0
  138. package/src/keywords/index.ts +151 -0
  139. package/src/keywords/llm-judge.ts +357 -0
  140. package/src/keywords/nlp-analysis.ts +706 -0
  141. package/src/keywords/prioritizer.ts +295 -0
  142. package/src/keywords/site-crawler.ts +342 -0
  143. package/src/keywords/sources/autocomplete.ts +139 -0
  144. package/src/keywords/sources/competitive-search.ts +450 -0
  145. package/src/keywords/sources/competitor-analysis.ts +374 -0
  146. package/src/keywords/sources/dataforseo.ts +206 -0
  147. package/src/keywords/sources/free-sources.ts +294 -0
  148. package/src/keywords/sources/gsc.ts +123 -0
  149. package/src/keywords/topic-grouping.ts +327 -0
  150. package/src/keywords/types.ts +144 -0
  151. package/src/keywords/wizard.ts +457 -0
  152. package/src/loader.ts +40 -0
  153. package/src/reports/index.ts +7 -0
  154. package/src/reports/report-generator.test.ts +293 -0
  155. package/src/reports/report-generator.ts +713 -0
  156. package/src/scheduler/alerts.test.ts +458 -0
  157. package/src/scheduler/alerts.ts +328 -0
  158. package/src/scheduler/index.ts +8 -0
  159. package/src/scheduler/scheduled-audit.test.ts +377 -0
  160. package/src/scheduler/scheduled-audit.ts +149 -0
  161. package/src/test/integration-test.ts +325 -0
  162. package/src/tools/analyzer.ts +373 -0
  163. package/src/tools/crawl.ts +293 -0
  164. package/src/tools/files.ts +301 -0
  165. package/src/tools/h1-fixer.ts +249 -0
  166. package/src/tools/index.ts +67 -0
  167. package/src/tracking/github-action.ts +326 -0
  168. package/src/tracking/google-analytics.ts +265 -0
  169. package/src/tracking/index.ts +45 -0
  170. package/src/tracking/report-generator.ts +386 -0
  171. package/src/tracking/search-console.ts +335 -0
  172. package/src/types.ts +134 -0
  173. package/src/utils/http.ts +302 -0
  174. package/src/wasm-adapter.ts +297 -0
  175. package/src/wasm-entry.ts +14 -0
  176. package/tsconfig.json +17 -0
  177. package/tsup.wasm.config.ts +26 -0
  178. package/vitest.config.ts +15 -0
@@ -0,0 +1,374 @@
1
+ // Competitor Keyword Analysis (SpyFu Kombat-style)
2
+ // Analyze competitor rankings to find keyword gaps and opportunities
3
+
4
+ import { httpGet } from '../../utils/http.js';
5
+ import * as cheerio from 'cheerio';
6
+ import type { KeywordData } from '../types.js';
7
+
8
+ const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
9
+
10
+ export interface CompetitorKeywordResult {
11
+ // Keywords all competitors rank for (high priority - proven market)
12
+ coreKeywords: KeywordData[];
13
+ // Keywords competitors rank for but you don't (opportunities)
14
+ missingKeywords: KeywordData[];
15
+ // Keywords only you rank for (potential advantages)
16
+ uniqueKeywords: KeywordData[];
17
+ // All keywords any competitor ranks for
18
+ keywordUniverse: KeywordData[];
19
+ // Overlap analysis
20
+ competitorOverlap: CompetitorOverlap[];
21
+ }
22
+
23
+ export interface CompetitorOverlap {
24
+ competitor: string;
25
+ sharedKeywords: number;
26
+ uniqueToThem: number;
27
+ uniqueToYou: number;
28
+ }
29
+
30
+ export interface CompetitorData {
31
+ domain: string;
32
+ keywords: Set<string>;
33
+ rankings: Map<string, number>; // keyword -> position
34
+ }
35
+
36
+ /**
37
+ * Scrape competitor rankings from Google SERP for seed keywords
38
+ * This discovers what keywords competitors appear for
39
+ */
40
+ export async function discoverCompetitorKeywords(
41
+ yourDomain: string,
42
+ seedKeywords: string[],
43
+ competitors?: string[]
44
+ ): Promise<CompetitorKeywordResult> {
45
+ const yourKeywords = new Set<string>();
46
+ const competitorKeywordsMap = new Map<string, Set<string>>();
47
+ const allKeywords = new Map<string, KeywordData>();
48
+
49
+ // Initialize competitor tracking
50
+ const discoveredCompetitors = new Set<string>(competitors || []);
51
+
52
+ console.log(' → Analyzing SERP results for competitor keywords...');
53
+
54
+ // For each seed keyword, check who ranks and for what related terms
55
+ for (const seed of seedKeywords) {
56
+ try {
57
+ // Get SERP results
58
+ const serpData = await analyzeSERP(seed);
59
+
60
+ // Track who ranks for this keyword
61
+ for (const result of serpData.results) {
62
+ const domain = extractDomain(result.url);
63
+
64
+ if (domain === yourDomain || domain.includes(yourDomain) || yourDomain.includes(domain)) {
65
+ yourKeywords.add(seed);
66
+ } else {
67
+ // Discovered competitor
68
+ discoveredCompetitors.add(domain);
69
+ if (!competitorKeywordsMap.has(domain)) {
70
+ competitorKeywordsMap.set(domain, new Set());
71
+ }
72
+ competitorKeywordsMap.get(domain)!.add(seed);
73
+ }
74
+
75
+ // Extract keywords from competitor titles/descriptions
76
+ const extractedKeywords = extractKeywordsFromText(result.title + ' ' + result.description);
77
+ for (const kw of extractedKeywords) {
78
+ if (!allKeywords.has(kw)) {
79
+ allKeywords.set(kw, {
80
+ keyword: kw,
81
+ searchVolume: 0,
82
+ keywordDifficulty: 0,
83
+ source: 'competitor' as const,
84
+ });
85
+ }
86
+
87
+ // Track which domains have this keyword
88
+ if (domain === yourDomain || domain.includes(yourDomain)) {
89
+ yourKeywords.add(kw);
90
+ } else {
91
+ competitorKeywordsMap.get(domain)?.add(kw);
92
+ }
93
+ }
94
+ }
95
+
96
+ // Also get related searches for more keyword ideas
97
+ for (const related of serpData.relatedSearches) {
98
+ if (!allKeywords.has(related)) {
99
+ allKeywords.set(related, {
100
+ keyword: related,
101
+ searchVolume: 0,
102
+ keywordDifficulty: 0,
103
+ source: 'competitor' as const,
104
+ });
105
+ }
106
+ }
107
+
108
+ // Avoid rate limiting
109
+ await sleep(500);
110
+ } catch (error) {
111
+ // Continue with other seeds
112
+ }
113
+ }
114
+
115
+ // Calculate keyword categories
116
+ const coreKeywords: KeywordData[] = [];
117
+ const missingKeywords: KeywordData[] = [];
118
+ const uniqueKeywords: KeywordData[] = [];
119
+ const keywordUniverse: KeywordData[] = [];
120
+
121
+ // Analyze each keyword
122
+ for (const [keyword, data] of allKeywords) {
123
+ const competitorCount = countCompetitorsWithKeyword(keyword, competitorKeywordsMap);
124
+ const youHaveIt = yourKeywords.has(keyword);
125
+
126
+ keywordUniverse.push(data);
127
+
128
+ if (competitorCount >= 2 && youHaveIt) {
129
+ // Core keyword - everyone ranks
130
+ coreKeywords.push(data);
131
+ } else if (competitorCount >= 1 && !youHaveIt) {
132
+ // Missing keyword - competitors have it, you don't
133
+ missingKeywords.push(data);
134
+ } else if (competitorCount === 0 && youHaveIt) {
135
+ // Unique keyword - only you have it
136
+ uniqueKeywords.push(data);
137
+ }
138
+ }
139
+
140
+ // Calculate competitor overlap
141
+ const competitorOverlap: CompetitorOverlap[] = [];
142
+ for (const [domain, keywords] of competitorKeywordsMap) {
143
+ const shared = [...keywords].filter(k => yourKeywords.has(k)).length;
144
+ const uniqueToThem = [...keywords].filter(k => !yourKeywords.has(k)).length;
145
+ const uniqueToYou = [...yourKeywords].filter(k => !keywords.has(k)).length;
146
+
147
+ competitorOverlap.push({
148
+ competitor: domain,
149
+ sharedKeywords: shared,
150
+ uniqueToThem,
151
+ uniqueToYou,
152
+ });
153
+ }
154
+
155
+ // Sort by number of keywords they have that you don't
156
+ competitorOverlap.sort((a, b) => b.uniqueToThem - a.uniqueToThem);
157
+
158
+ return {
159
+ coreKeywords,
160
+ missingKeywords,
161
+ uniqueKeywords,
162
+ keywordUniverse,
163
+ competitorOverlap,
164
+ };
165
+ }
166
+
167
+ interface SERPResult {
168
+ url: string;
169
+ title: string;
170
+ description: string;
171
+ position: number;
172
+ }
173
+
174
+ interface SERPData {
175
+ results: SERPResult[];
176
+ relatedSearches: string[];
177
+ peopleAlsoAsk: string[];
178
+ }
179
+
180
+ async function analyzeSERP(query: string): Promise<SERPData> {
181
+ try {
182
+ const response = await httpGet<string>('https://www.google.com/search', {
183
+ params: { q: query, num: 20 },
184
+ headers: { 'User-Agent': USER_AGENT },
185
+ timeout: 10000,
186
+ });
187
+
188
+ const $ = cheerio.load(response.data);
189
+ const results: SERPResult[] = [];
190
+ const relatedSearches: string[] = [];
191
+ const peopleAlsoAsk: string[] = [];
192
+
193
+ // Extract search results
194
+ let position = 1;
195
+ $('div.g').each((_, el) => {
196
+ const link = $(el).find('a').first();
197
+ const url = link.attr('href');
198
+ const title = $(el).find('h3').text().trim();
199
+ const description = $(el).find('.VwiC3b').text().trim() ||
200
+ $(el).find('[data-content-feature]').text().trim();
201
+
202
+ if (url && url.startsWith('http') && title) {
203
+ results.push({ url, title, description, position });
204
+ position++;
205
+ }
206
+ });
207
+
208
+ // Extract related searches
209
+ $('div[data-ved] a').each((_, el) => {
210
+ const href = $(el).attr('href');
211
+ if (href?.includes('/search?q=')) {
212
+ const text = $(el).text().trim();
213
+ if (text.length > 3 && text.length < 100 && !text.includes('http')) {
214
+ relatedSearches.push(text);
215
+ }
216
+ }
217
+ });
218
+
219
+ // Extract People Also Ask
220
+ $('[data-sgrd]').each((_, el) => {
221
+ const text = $(el).text().trim();
222
+ if (text.endsWith('?') && text.length > 10) {
223
+ peopleAlsoAsk.push(text);
224
+ }
225
+ });
226
+
227
+ return {
228
+ results,
229
+ relatedSearches: [...new Set(relatedSearches)],
230
+ peopleAlsoAsk: [...new Set(peopleAlsoAsk)],
231
+ };
232
+ } catch {
233
+ return { results: [], relatedSearches: [], peopleAlsoAsk: [] };
234
+ }
235
+ }
236
+
237
+ function extractDomain(url: string): string {
238
+ try {
239
+ const parsed = new URL(url);
240
+ return parsed.hostname.replace(/^www\./, '');
241
+ } catch {
242
+ return '';
243
+ }
244
+ }
245
+
246
+ function extractKeywordsFromText(text: string): string[] {
247
+ // Extract 2-4 word phrases that could be keywords
248
+ const words = text.toLowerCase()
249
+ .replace(/[^\w\s]/g, ' ')
250
+ .split(/\s+/)
251
+ .filter(w => w.length > 2);
252
+
253
+ const keywords: string[] = [];
254
+
255
+ // Single words (4+ chars)
256
+ words.filter(w => w.length >= 4).forEach(w => keywords.push(w));
257
+
258
+ // 2-word phrases
259
+ for (let i = 0; i < words.length - 1; i++) {
260
+ const phrase = `${words[i]} ${words[i + 1]}`;
261
+ if (phrase.length >= 5 && phrase.length <= 50) {
262
+ keywords.push(phrase);
263
+ }
264
+ }
265
+
266
+ // 3-word phrases
267
+ for (let i = 0; i < words.length - 2; i++) {
268
+ const phrase = `${words[i]} ${words[i + 1]} ${words[i + 2]}`;
269
+ if (phrase.length >= 8 && phrase.length <= 60) {
270
+ keywords.push(phrase);
271
+ }
272
+ }
273
+
274
+ return [...new Set(keywords)];
275
+ }
276
+
277
+ function countCompetitorsWithKeyword(
278
+ keyword: string,
279
+ competitorKeywordsMap: Map<string, Set<string>>
280
+ ): number {
281
+ let count = 0;
282
+ for (const [, keywords] of competitorKeywordsMap) {
283
+ if (keywords.has(keyword)) {
284
+ count++;
285
+ }
286
+ }
287
+ return count;
288
+ }
289
+
290
+ function sleep(ms: number): Promise<void> {
291
+ return new Promise(resolve => setTimeout(resolve, ms));
292
+ }
293
+
294
+ /**
295
+ * Format competitor analysis report
296
+ */
297
+ export function formatCompetitorReport(result: CompetitorKeywordResult): string {
298
+ const lines: string[] = [];
299
+
300
+ lines.push('');
301
+ lines.push('═'.repeat(70));
302
+ lines.push(' COMPETITOR KEYWORD ANALYSIS');
303
+ lines.push('═'.repeat(70));
304
+ lines.push('');
305
+
306
+ // Competitor overlap summary
307
+ if (result.competitorOverlap.length > 0) {
308
+ lines.push('📊 COMPETITOR OVERLAP');
309
+ lines.push('─'.repeat(70));
310
+ lines.push(' Domain Shared Gaps Your Wins');
311
+ lines.push(' ' + '─'.repeat(60));
312
+
313
+ for (const comp of result.competitorOverlap.slice(0, 5)) {
314
+ const domain = comp.competitor.substring(0, 30).padEnd(32);
315
+ const shared = String(comp.sharedKeywords).padStart(6);
316
+ const gaps = String(comp.uniqueToThem).padStart(6);
317
+ const wins = String(comp.uniqueToYou).padStart(8);
318
+ lines.push(` ${domain} ${shared} ${gaps} ${wins}`);
319
+ }
320
+ lines.push('');
321
+ }
322
+
323
+ // Missing keywords (opportunities)
324
+ if (result.missingKeywords.length > 0) {
325
+ lines.push('🔴 KEYWORD GAPS (Competitors Rank, You Don\'t)');
326
+ lines.push('─'.repeat(70));
327
+ lines.push(' These are high-priority opportunities - competitors have proven');
328
+ lines.push(' these keywords work in your market.');
329
+ lines.push('');
330
+
331
+ for (const kw of result.missingKeywords.slice(0, 10)) {
332
+ lines.push(` • ${kw.keyword}`);
333
+ }
334
+ lines.push('');
335
+ }
336
+
337
+ // Core keywords
338
+ if (result.coreKeywords.length > 0) {
339
+ lines.push('🟢 CORE KEYWORDS (All Competitors Rank)');
340
+ lines.push('─'.repeat(70));
341
+ lines.push(' These keywords define your market. You should rank for these.');
342
+ lines.push('');
343
+
344
+ for (const kw of result.coreKeywords.slice(0, 10)) {
345
+ lines.push(` • ${kw.keyword}`);
346
+ }
347
+ lines.push('');
348
+ }
349
+
350
+ // Unique keywords
351
+ if (result.uniqueKeywords.length > 0) {
352
+ lines.push('🔵 YOUR UNIQUE KEYWORDS (Only You Rank)');
353
+ lines.push('─'.repeat(70));
354
+ lines.push(' Potential competitive advantages or niche opportunities.');
355
+ lines.push('');
356
+
357
+ for (const kw of result.uniqueKeywords.slice(0, 10)) {
358
+ lines.push(` • ${kw.keyword}`);
359
+ }
360
+ lines.push('');
361
+ }
362
+
363
+ // Summary stats
364
+ lines.push('📈 SUMMARY');
365
+ lines.push('─'.repeat(70));
366
+ lines.push(` Total Keywords Analyzed: ${result.keywordUniverse.length}`);
367
+ lines.push(` Core Keywords: ${result.coreKeywords.length}`);
368
+ lines.push(` Keyword Gaps: ${result.missingKeywords.length}`);
369
+ lines.push(` Your Unique Keywords: ${result.uniqueKeywords.length}`);
370
+ lines.push('');
371
+ lines.push('═'.repeat(70));
372
+
373
+ return lines.join('\n');
374
+ }
@@ -0,0 +1,206 @@
1
+ // DataForSEO API Integration (Budget-Friendly Paid Option)
2
+ // Pricing: ~$0.002 per request, $50 minimum deposit
3
+
4
+ import { httpGet, httpPost } from '../../utils/http.js';
5
+ import type { KeywordData } from '../types.js';
6
+
7
+ const DATAFORSEO_BASE_URL = 'https://api.dataforseo.com/v3';
8
+
9
+ export interface DataForSEOCredentials {
10
+ login: string;
11
+ password: string;
12
+ }
13
+
14
+ export interface DataForSEOKeywordResult {
15
+ keyword: string;
16
+ search_volume: number;
17
+ keyword_difficulty: number;
18
+ cpc: number;
19
+ competition: number;
20
+ competition_level: string;
21
+ monthly_searches?: Array<{ month: string; search_volume: number }>;
22
+ }
23
+
24
+ // Get keyword data with search volume and difficulty
25
+ export async function getKeywordData(
26
+ keywords: string[],
27
+ credentials: DataForSEOCredentials,
28
+ location: number = 2840 // US
29
+ ): Promise<KeywordData[]> {
30
+ const auth = Buffer.from(`${credentials.login}:${credentials.password}`).toString('base64');
31
+
32
+ try {
33
+ const response = await httpPost(
34
+ `${DATAFORSEO_BASE_URL}/keywords_data/google_ads/search_volume/live`,
35
+ [
36
+ {
37
+ keywords,
38
+ location_code: location,
39
+ language_code: 'en',
40
+ },
41
+ ],
42
+ {
43
+ headers: {
44
+ Authorization: `Basic ${auth}`,
45
+ 'Content-Type': 'application/json',
46
+ },
47
+ timeout: 30000,
48
+ }
49
+ );
50
+
51
+ const data = response.data as { tasks?: Array<{ result?: DataForSEOKeywordResult[] }> };
52
+ const results: DataForSEOKeywordResult[] = data?.tasks?.[0]?.result || [];
53
+
54
+ return results.map((r) => ({
55
+ keyword: r.keyword,
56
+ searchVolume: r.search_volume || 0,
57
+ keywordDifficulty: r.keyword_difficulty || estimateKdFromCompetition(r.competition),
58
+ cpc: r.cpc,
59
+ source: 'dataforseo' as const,
60
+ }));
61
+ } catch (error) {
62
+ console.error('DataForSEO API error:', error);
63
+ throw new Error(`DataForSEO API failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
64
+ }
65
+ }
66
+
67
+ // Get keyword suggestions from seed keyword
68
+ export async function getKeywordSuggestions(
69
+ seedKeyword: string,
70
+ credentials: DataForSEOCredentials,
71
+ location: number = 2840,
72
+ limit: number = 100
73
+ ): Promise<KeywordData[]> {
74
+ const auth = Buffer.from(`${credentials.login}:${credentials.password}`).toString('base64');
75
+
76
+ try {
77
+ const response = await httpPost(
78
+ `${DATAFORSEO_BASE_URL}/dataforseo_labs/google/keyword_suggestions/live`,
79
+ [
80
+ {
81
+ keyword: seedKeyword,
82
+ location_code: location,
83
+ language_code: 'en',
84
+ include_seed_keyword: true,
85
+ limit,
86
+ },
87
+ ],
88
+ {
89
+ headers: {
90
+ Authorization: `Basic ${auth}`,
91
+ 'Content-Type': 'application/json',
92
+ },
93
+ timeout: 30000,
94
+ }
95
+ );
96
+
97
+ const data = response.data as { tasks?: Array<{ result?: Array<{ items?: unknown[] }> }> };
98
+ const results = data?.tasks?.[0]?.result?.[0]?.items || [];
99
+
100
+ return results.map((item) => {
101
+ const i = item as { keyword_data: DataForSEOKeywordResult };
102
+ return {
103
+ keyword: i.keyword_data.keyword,
104
+ searchVolume: i.keyword_data.search_volume || 0,
105
+ keywordDifficulty: i.keyword_data.keyword_difficulty || 0,
106
+ cpc: i.keyword_data.cpc,
107
+ source: 'dataforseo' as const,
108
+ };
109
+ });
110
+ } catch (error) {
111
+ console.error('DataForSEO suggestions error:', error);
112
+ throw new Error(`DataForSEO API failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
113
+ }
114
+ }
115
+
116
+ // Get related keywords
117
+ export async function getRelatedKeywords(
118
+ seedKeyword: string,
119
+ credentials: DataForSEOCredentials,
120
+ location: number = 2840,
121
+ limit: number = 50
122
+ ): Promise<KeywordData[]> {
123
+ const auth = Buffer.from(`${credentials.login}:${credentials.password}`).toString('base64');
124
+
125
+ try {
126
+ const response = await httpPost(
127
+ `${DATAFORSEO_BASE_URL}/dataforseo_labs/google/related_keywords/live`,
128
+ [
129
+ {
130
+ keyword: seedKeyword,
131
+ location_code: location,
132
+ language_code: 'en',
133
+ limit,
134
+ },
135
+ ],
136
+ {
137
+ headers: {
138
+ Authorization: `Basic ${auth}`,
139
+ 'Content-Type': 'application/json',
140
+ },
141
+ timeout: 30000,
142
+ }
143
+ );
144
+
145
+ const data = response.data as { tasks?: Array<{ result?: Array<{ items?: unknown[] }> }> };
146
+ const results = data?.tasks?.[0]?.result?.[0]?.items || [];
147
+
148
+ return results.map((item) => {
149
+ const i = item as { keyword_data: DataForSEOKeywordResult };
150
+ return {
151
+ keyword: i.keyword_data.keyword,
152
+ searchVolume: i.keyword_data.search_volume || 0,
153
+ keywordDifficulty: i.keyword_data.keyword_difficulty || 0,
154
+ cpc: i.keyword_data.cpc,
155
+ source: 'dataforseo' as const,
156
+ };
157
+ });
158
+ } catch (error) {
159
+ console.error('DataForSEO related keywords error:', error);
160
+ return [];
161
+ }
162
+ }
163
+
164
+ // Estimate KD from competition value (0-1)
165
+ function estimateKdFromCompetition(competition: number): number {
166
+ // Competition is Google Ads competition (0-1)
167
+ // Map to KD scale (0-100)
168
+ return Math.round(competition * 100);
169
+ }
170
+
171
+ // Location codes reference
172
+ export const LOCATION_CODES = {
173
+ US: 2840,
174
+ UK: 2826,
175
+ CA: 2124,
176
+ AU: 2036,
177
+ DE: 2276,
178
+ FR: 2250,
179
+ ES: 2724,
180
+ IT: 2380,
181
+ BR: 2076,
182
+ IN: 2356,
183
+ JP: 2392,
184
+ };
185
+
186
+ // Check API balance
187
+ export async function checkBalance(credentials: DataForSEOCredentials): Promise<number> {
188
+ const auth = Buffer.from(`${credentials.login}:${credentials.password}`).toString('base64');
189
+
190
+ try {
191
+ const response = await httpGet<string>(
192
+ `${DATAFORSEO_BASE_URL}/appendix/user_data`,
193
+ {
194
+ headers: {
195
+ Authorization: `Basic ${auth}`,
196
+ },
197
+ }
198
+ );
199
+
200
+ const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
201
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
202
+ return (data as any)?.tasks?.[0]?.result?.[0]?.money?.balance || 0;
203
+ } catch {
204
+ return 0;
205
+ }
206
+ }