-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathserp_clustering_at_scale.py
More file actions
802 lines (649 loc) · 27.7 KB
/
Copy pathserp_clustering_at_scale.py
File metadata and controls
802 lines (649 loc) · 27.7 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# Author: Lee Foot
# Website: https://leefoot.com
####################################################################################
# #
# SERP Clustering at Scale #
# #
# Clusters keywords based on common SERP URLs. #
# Supports CSV imports (SERP API etc.) and live DataForSEO SERP fetching. #
# Multiple clustering strategies with consolidation scoring. #
# #
####################################################################################
# Contact : https://www.leefoot.com/contact #
# Email : hello@leefoot.com #
# LinkedIn : https://www.linkedin.com/in/lee-foot/ #
# Bluesky : https://bsky.app/profile/leefootseo.bsky.social #
####################################################################################
"""
SERP Clustering Script
Version: 4.0
A script to identify content consolidation opportunities based on shared URLs in
search results. Supports overlapping clusters and multiple clustering strategies.
Features:
- Multi-strategy clustering (connected components, cliques, core-based)
- Consolidation scoring (0-100) to prioritise opportunities
- Overlap detection between clusters
- Detailed cluster metrics
- Live SERP fetching via DataForSEO API
- CSV import from SERP API or similar tools
Usage (CSV mode):
1. Export SERP data from SERP API (or similar) in CSV format
2. Place CSV files in a folder
3. Run: python serp_clustering_at_scale.py
Usage (Live DataForSEO mode):
1. Create a text file with one keyword per line
2. Run: python serp_clustering_at_scale.py --keywords-file keywords.txt
3. Credentials via --login/--password or DATAFORSEO_LOGIN/DATAFORSEO_PASSWORD env vars
Requirements:
pip install pandas tqdm requests
"""
import argparse
import glob
import os
import sys
import time
import logging
import requests
from base64 import b64encode
from collections import defaultdict
from itertools import combinations
import pandas as pd
from tqdm import tqdm
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# ----------------
# DataForSEO Configuration
# ----------------
DATAFORSEO_ENDPOINT = "https://api.dataforseo.com/v3/serp/google/organic/live/advanced"
COST_PER_KEYWORD = 0.002 # USD per keyword (10 results)
LOCATION_CODES = {
"United Kingdom": 2826,
"United States": 2840,
"Australia": 2036,
"Canada": 2124,
"Germany": 2276,
"France": 2250,
"Spain": 2724,
"Italy": 2380,
"Netherlands": 2528,
"India": 2356,
}
# ----------------
# Configuration Variables (CSV mode defaults)
# ----------------
COMMON_URLS = 4 # minimum number of common URLs to match on. Default = 4
CLUSTERING_STRATEGY = 'all' # Options: 'connected', 'cliques', 'core', 'all'
CORE_THRESHOLD = 0.7 # For core clustering: minimum connectivity percentage
FOLDER_LOCATION = os.path.join(os.getcwd(), 'serp_exports') # folder containing exported CSV files
FILE_PREFIX = "/Batch_Results_*.csv" # file prefix for SERP export CSVs
EXPORT_CSV_FILE_PATH = os.path.join(os.getcwd(), 'serp_cluster_results.csv') # output file path
# ----------------
# DataForSEO Live Fetching
# ----------------
def fetch_serps_dataforseo(keywords, login, password, location_code, device="desktop"):
"""
Fetch live SERP results from DataForSEO for a list of keywords.
Returns a DataFrame with columns 'query' and 'link', matching the format
produced by CSV mode.
"""
cred = b64encode(f"{login}:{password}".encode()).decode()
headers = {
"Authorization": f"Basic {cred}",
"Content-Type": "application/json",
}
rows = []
failed = []
for i, keyword in enumerate(tqdm(keywords, desc="Fetching SERPs from DataForSEO")):
keyword = keyword.strip()
if not keyword:
continue
payload = [{
"keyword": keyword,
"location_code": location_code,
"language_code": "en",
"device": device,
"depth": 10,
}]
try:
response = requests.post(
DATAFORSEO_ENDPOINT,
headers=headers,
json=payload,
timeout=30,
)
response.raise_for_status()
data = response.json()
tasks = data.get("tasks", [])
if not tasks or tasks[0].get("status_code") != 20000:
error_msg = tasks[0].get("status_message", "Unknown error") if tasks else "No tasks returned"
logger.warning(f"API error for '{keyword}': {error_msg}")
failed.append(keyword)
continue
result = tasks[0].get("result", [])
if not result:
logger.warning(f"No results for '{keyword}'")
failed.append(keyword)
continue
items = result[0].get("items", [])
organic_results = [item for item in items if item.get("type") == "organic"]
for item in organic_results:
url = item.get("url", "")
if url:
rows.append({"query": keyword, "link": url})
except requests.exceptions.RequestException as e:
logger.error(f"Request failed for '{keyword}': {e}")
failed.append(keyword)
# Rate limit: 0.5s between requests
if i < len(keywords) - 1:
time.sleep(0.5)
if failed:
logger.warning(f"{len(failed)} keyword(s) failed: {', '.join(failed[:10])}")
if len(failed) > 10:
logger.warning(f" ... and {len(failed) - 10} more")
if not rows:
raise ValueError("No SERP data retrieved from DataForSEO. Check credentials and keywords.")
df = pd.DataFrame(rows)
logger.info(f"Retrieved {len(df)} query-URL pairs for {df['query'].nunique()} keywords")
return df
# ----------------
# Read and Clean Data (CSV mode)
# ----------------
def validate_folder_and_files(folder_location, file_prefix):
"""Validates folder exists and contains required files."""
if not os.path.exists(folder_location):
raise ValueError(f"Folder not found: {folder_location}")
file_pattern = f"{folder_location}{file_prefix}"
files = glob.glob(file_pattern)
if not files:
raise ValueError(
f"No CSV files found matching pattern '{file_prefix}' in folder: {folder_location}\n"
f"Please ensure CSV files are present and match the pattern: Batch_Results_*.csv"
)
logger.info(f"Found {len(files)} CSV files to process")
return files
def read_csv_files(file_paths):
"""Reads and concatenates multiple CSV files."""
dataframes = []
for file in tqdm(file_paths, desc="Reading CSV files"):
try:
df = pd.read_csv(file, usecols=["search.q", "result.organic_results.link"],
dtype="str", index_col=None, header=0)
if not df.empty:
dataframes.append(df)
else:
logger.warning(f"Empty CSV file: {file}")
except Exception as e:
logger.error(f"Error reading file {file}: {str(e)}")
continue
if not dataframes:
raise ValueError("No valid data found in any of the CSV files")
return pd.concat(dataframes, axis=0, ignore_index=True)
def prepare_data(df):
"""Prepares and cleans the data for clustering."""
# Rename columns (only if CSV mode columns are present)
if "search.q" in df.columns and "result.organic_results.link" in df.columns:
df = df.rename(columns={"search.q": "query", "result.organic_results.link": "link"})
# Convert queries to lowercase
df['query'] = df['query'].str.lower()
# Remove duplicates but keep all queries even if they don't share links
return df.drop_duplicates(subset=["query", "link"])
def create_query_map(df):
"""Creates a mapping of queries to their sets of URLs."""
return df.groupby('query')['link'].apply(set).to_dict()
# ----------------
# Clustering Strategies
# ----------------
def build_similarity_matrix(query_map, common_urls_threshold):
"""Build similarity matrix between queries based on shared URLs."""
similarity_matrix = defaultdict(dict)
queries = list(query_map.keys())
for i in range(len(queries)):
for j in range(i + 1, len(queries)):
query1, query2 = queries[i], queries[j]
shared_urls = len(query_map[query1] & query_map[query2])
if shared_urls >= common_urls_threshold:
similarity_matrix[query1][query2] = shared_urls
similarity_matrix[query2][query1] = shared_urls
return similarity_matrix, queries
def find_connected_components(similarity_matrix, queries):
"""Strategy 1: Find connected components (non-overlapping base clusters)."""
visited = set()
components = []
def dfs(query, component):
if query in visited:
return
visited.add(query)
component.add(query)
for neighbor in similarity_matrix[query]:
dfs(neighbor, component)
for query in queries:
if query not in visited and query in similarity_matrix:
component = set()
dfs(query, component)
if len(component) > 1:
components.append(component)
return components
def find_cliques(similarity_matrix, queries, min_clique_size=2):
"""Strategy 2: Find cliques (all queries must be connected to each other)."""
cliques = []
def is_clique(candidate_set):
candidate_list = list(candidate_set)
for i in range(len(candidate_list)):
for j in range(i + 1, len(candidate_list)):
if candidate_list[j] not in similarity_matrix[candidate_list[i]]:
return False
return True
# Find maximal cliques using a simplified approach
for query in queries:
if query not in similarity_matrix:
continue
candidates = {query}
candidates.update(similarity_matrix[query].keys())
# Try to build maximal clique
for size in range(len(candidates), min_clique_size - 1, -1):
for subset in combinations(candidates, size):
if is_clique(set(subset)):
clique = set(subset)
# Check if this clique is already found or is a subset
is_new = True
for existing_clique in cliques:
if clique.issubset(existing_clique):
is_new = False
break
if is_new:
cliques.append(clique)
break
# Remove subsets
final_cliques = []
for clique in cliques:
is_subset = False
for other_clique in cliques:
if clique != other_clique and clique.issubset(other_clique):
is_subset = True
break
if not is_subset:
final_cliques.append(clique)
return final_cliques
def find_core_clusters(similarity_matrix, queries, core_threshold=0.7):
"""Strategy 3: Core-based clustering (queries must share URLs with core set)."""
core_clusters = []
for seed_query in queries:
if seed_query not in similarity_matrix:
continue
cluster = {seed_query}
candidates = set(similarity_matrix[seed_query].keys())
for candidate in candidates:
# Check if candidate is connected to enough existing cluster members
connections = sum(1 for member in cluster if candidate in similarity_matrix[member])
if connections >= len(cluster) * core_threshold:
cluster.add(candidate)
if len(cluster) > 1 and cluster not in core_clusters:
core_clusters.append(cluster)
return core_clusters
# ----------------
# Cluster Analysis
# ----------------
def get_shortest_query_in_cluster(queries):
"""Returns the shortest query from a list of queries."""
return min(queries, key=len)
def analyze_cluster_details(cluster_queries, query_map, similarity_matrix):
"""Analyse detailed metrics for a cluster."""
queries = list(cluster_queries)
# Find URLs shared by all queries
if queries:
shared_urls = set(query_map[queries[0]])
for query in queries[1:]:
shared_urls &= query_map[query]
else:
shared_urls = set()
# Calculate pairwise metrics
metrics = {
'min_shared_urls': float('inf'),
'max_shared_urls': 0,
'avg_shared_urls': 0,
'total_comparisons': 0,
'connectivity_score': 0
}
possible_connections = len(queries) * (len(queries) - 1) / 2
actual_connections = 0
for i in range(len(queries)):
for j in range(i + 1, len(queries)):
if queries[j] in similarity_matrix[queries[i]]:
shared = similarity_matrix[queries[i]][queries[j]]
metrics['min_shared_urls'] = min(metrics['min_shared_urls'], shared)
metrics['max_shared_urls'] = max(metrics['max_shared_urls'], shared)
metrics['avg_shared_urls'] += shared
metrics['total_comparisons'] += 1
actual_connections += 1
if metrics['total_comparisons'] > 0:
metrics['avg_shared_urls'] /= metrics['total_comparisons']
else:
metrics['min_shared_urls'] = 0
if possible_connections > 0:
metrics['connectivity_score'] = actual_connections / possible_connections
return {
'queries': queries,
'shared_urls': list(shared_urls),
'shared_url_count': len(shared_urls),
'cluster_metrics': metrics,
'cluster_size': len(queries)
}
def calculate_consolidation_score(metrics, cluster_size, overlapping_count):
"""
Calculate a score (0-100) indicating how strong the consolidation opportunity is.
Higher scores mean stronger consolidation candidates.
"""
# Base score from average shared URLs (normalise to 0-40 range)
base_score = min(40, metrics['avg_shared_urls'] * 4)
# Connectivity bonus (0-30 range)
connectivity_bonus = metrics['connectivity_score'] * 30
# Cluster size bonus (0-20 range)
size_bonus = min(20, (cluster_size - 2) * 5)
# Overlap penalty (0-10 range)
overlap_penalty = min(10, overlapping_count * 5)
total_score = base_score + connectivity_bonus + size_bonus - overlap_penalty
return max(0, min(100, round(total_score)))
def get_consolidation_recommendation(score):
"""Get a recommendation based on the consolidation score."""
if score >= 80:
return "Strong consolidation candidate"
elif score >= 60:
return "Good consolidation candidate"
elif score >= 40:
return "Possible consolidation"
elif score >= 20:
return "Weak consolidation candidate"
else:
return "Keep separate"
# ----------------
# Main Clustering Function
# ----------------
def find_consolidation_clusters(query_map, common_urls_threshold, strategy='all', core_threshold=0.7):
"""
Main clustering function supporting multiple strategies.
Returns clusters with metadata and consolidation scores.
"""
similarity_matrix, queries = build_similarity_matrix(query_map, common_urls_threshold)
all_clusters = []
# Get clusters based on selected strategy
if strategy in ['connected', 'all']:
components = find_connected_components(similarity_matrix, queries)
for comp in components:
cluster_data = analyze_cluster_details(comp, query_map, similarity_matrix)
cluster_data['cluster_type'] = 'connected_component'
all_clusters.append(cluster_data)
if strategy in ['cliques', 'all']:
cliques = find_cliques(similarity_matrix, queries)
for clique in cliques:
cluster_data = analyze_cluster_details(clique, query_map, similarity_matrix)
cluster_data['cluster_type'] = 'clique'
all_clusters.append(cluster_data)
if strategy in ['core', 'all']:
core_clusters = find_core_clusters(similarity_matrix, queries, core_threshold)
for core_cluster in core_clusters:
cluster_data = analyze_cluster_details(core_cluster, query_map, similarity_matrix)
cluster_data['cluster_type'] = 'core_cluster'
all_clusters.append(cluster_data)
# Mark which queries appear in multiple clusters
query_cluster_count = defaultdict(int)
for cluster in all_clusters:
for query in cluster['queries']:
query_cluster_count[query] += 1
# Add overlap information
for cluster in all_clusters:
cluster['overlapping_queries'] = [q for q in cluster['queries'] if query_cluster_count[q] > 1]
return all_clusters, similarity_matrix
def create_cluster_dataframe(clusters, query_map):
"""
Creates a dataframe with cluster results including consolidation scores.
"""
rows = []
processed_queries = set()
# Process each cluster
for cluster_idx, cluster in enumerate(clusters):
cluster_name = get_shortest_query_in_cluster(cluster['queries'])
for query in cluster['queries']:
processed_queries.add(query)
# Calculate consolidation score
consolidation_score = calculate_consolidation_score(
cluster['cluster_metrics'],
cluster['cluster_size'],
len(cluster['overlapping_queries'])
)
rows.append({
'serp_cluster': cluster_name,
'cluster_type': cluster['cluster_type'],
'query': query,
'total_urls': len(query_map[query]),
'shared_url_count': cluster['shared_url_count'],
'shared_urls': ', '.join(cluster['shared_urls'][:5]),
'min_shared_urls': cluster['cluster_metrics']['min_shared_urls'],
'max_shared_urls': cluster['cluster_metrics']['max_shared_urls'],
'avg_shared_urls': round(cluster['cluster_metrics']['avg_shared_urls'], 2),
'connectivity_score': round(cluster['cluster_metrics']['connectivity_score'], 2),
'#_kws_in_cluster': cluster['cluster_size'],
'is_in_multiple_clusters': query in cluster['overlapping_queries'],
'consolidation_score': consolidation_score,
'consolidation_recommendation': get_consolidation_recommendation(consolidation_score)
})
# Add unclustered queries
for query in query_map.keys():
if query not in processed_queries:
rows.append({
'serp_cluster': 'NO_CLUSTER',
'cluster_type': 'none',
'query': query,
'total_urls': len(query_map[query]),
'shared_url_count': 0,
'shared_urls': '',
'min_shared_urls': 0,
'max_shared_urls': 0,
'avg_shared_urls': 0,
'connectivity_score': 0,
'#_kws_in_cluster': 1,
'is_in_multiple_clusters': False,
'consolidation_score': 0,
'consolidation_recommendation': 'Keep separate'
})
return pd.DataFrame(rows)
def export_results(df, file_path):
"""Exports the clustering results to a CSV file."""
# Sort by consolidation score (descending), then by cluster and query
df = df.sort_values(['consolidation_score', 'serp_cluster', 'query'],
ascending=[False, True, True])
df.to_csv(file_path, index=False, encoding='utf-8-sig')
logger.info(f"Results exported successfully to {file_path}")
# ----------------
# CLI Argument Parsing
# ----------------
def parse_args():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description="SERP Clustering at Scale - Cluster keywords by shared SERP URLs.",
epilog="CSV mode: reads SERP API exports from a folder. "
"Live mode: fetches SERPs from DataForSEO when --keywords-file is provided.",
)
# Mode selection (live mode triggered by --keywords-file)
parser.add_argument(
"--keywords-file",
type=str,
default=None,
help="Path to a text file with keywords (one per line). "
"When provided, fetches live SERPs from DataForSEO instead of reading CSVs.",
)
# DataForSEO credentials
parser.add_argument(
"--login",
type=str,
default=None,
help="DataForSEO login (or set DATAFORSEO_LOGIN env var).",
)
parser.add_argument(
"--password",
type=str,
default=None,
help="DataForSEO password (or set DATAFORSEO_PASSWORD env var).",
)
# DataForSEO options
parser.add_argument(
"--location",
type=str,
default="United Kingdom",
choices=list(LOCATION_CODES.keys()),
help="Location for SERP results (default: United Kingdom).",
)
parser.add_argument(
"--device",
type=str,
default="desktop",
choices=["desktop", "mobile"],
help="Device type for SERP results (default: desktop).",
)
# Clustering options
parser.add_argument(
"--common-urls",
type=int,
default=COMMON_URLS,
help=f"Minimum shared URLs to cluster keywords (default: {COMMON_URLS}).",
)
parser.add_argument(
"--strategy",
type=str,
default=CLUSTERING_STRATEGY,
choices=["connected", "cliques", "core", "all"],
help=f"Clustering strategy (default: {CLUSTERING_STRATEGY}).",
)
parser.add_argument(
"--core-threshold",
type=float,
default=CORE_THRESHOLD,
help=f"Core clustering connectivity threshold (default: {CORE_THRESHOLD}).",
)
# CSV mode options
parser.add_argument(
"--folder",
type=str,
default=FOLDER_LOCATION,
help=f"Folder containing SERP CSV exports (default: {FOLDER_LOCATION}).",
)
parser.add_argument(
"--file-prefix",
type=str,
default=FILE_PREFIX,
help=f"File prefix pattern for CSV files (default: {FILE_PREFIX}).",
)
# Output
parser.add_argument(
"--output",
type=str,
default=EXPORT_CSV_FILE_PATH,
help=f"Output CSV file path (default: {EXPORT_CSV_FILE_PATH}).",
)
return parser.parse_args()
# ----------------
# Main Processing Function
# ----------------
def process_serps(args=None):
"""Main function to process Search Engine Result Pages and identify consolidation opportunities."""
start_time = time.time()
try:
# Determine mode
if args and args.keywords_file:
# Live DataForSEO mode
logger.info("Mode: Live SERP fetching via DataForSEO")
# Resolve credentials
login = args.login or os.environ.get("DATAFORSEO_LOGIN")
password = args.password or os.environ.get("DATAFORSEO_PASSWORD")
if not login or not password:
logger.error(
"DataForSEO credentials required. Provide --login and --password "
"or set DATAFORSEO_LOGIN and DATAFORSEO_PASSWORD environment variables."
)
sys.exit(1)
# Read keywords
if not os.path.exists(args.keywords_file):
logger.error(f"Keywords file not found: {args.keywords_file}")
sys.exit(1)
with open(args.keywords_file, "r", encoding="utf-8") as f:
keywords = [line.strip() for line in f if line.strip()]
if not keywords:
logger.error("No keywords found in the file.")
sys.exit(1)
# Show estimated cost
location_code = LOCATION_CODES[args.location]
estimated_cost = len(keywords) * COST_PER_KEYWORD
logger.info(f"Keywords: {len(keywords)}")
logger.info(f"Location: {args.location} (code: {location_code})")
logger.info(f"Device: {args.device}")
logger.info(f"Estimated cost: ${estimated_cost:.2f}")
# Fetch SERPs
df = fetch_serps_dataforseo(
keywords=keywords,
login=login,
password=password,
location_code=location_code,
device=args.device,
)
else:
# CSV mode (original behaviour)
logger.info("Mode: CSV import")
folder = args.folder if args else FOLDER_LOCATION
prefix = args.file_prefix if args else FILE_PREFIX
logger.info(f"Looking for CSV files in: {folder}")
file_paths = validate_folder_and_files(folder, prefix)
df = read_csv_files(file_paths)
logger.info(f"Successfully read {len(df)} rows from CSV files")
# Prepare data
df = prepare_data(df)
logger.info(f"After preparation: {len(df)} unique query-URL pairs")
# Create query to URL mapping
query_map = create_query_map(df)
logger.info(f"Processing {len(query_map)} unique queries")
# Resolve clustering parameters
common_urls = args.common_urls if args else COMMON_URLS
strategy = args.strategy if args else CLUSTERING_STRATEGY
core_threshold = args.core_threshold if args else CORE_THRESHOLD
# Find consolidation clusters
clusters, similarity_matrix = find_consolidation_clusters(
query_map,
common_urls,
strategy=strategy,
core_threshold=core_threshold,
)
# Create results DataFrame
results_df = create_cluster_dataframe(clusters, query_map)
# Export results
output_path = args.output if args else EXPORT_CSV_FILE_PATH
export_results(results_df, output_path)
# Calculate statistics
total_queries = len(query_map)
clustered_queries = len(results_df[results_df['serp_cluster'] != 'NO_CLUSTER']['query'].unique())
clique_count = len([c for c in clusters if c['cluster_type'] == 'clique'])
component_count = len([c for c in clusters if c['cluster_type'] == 'connected_component'])
core_count = len([c for c in clusters if c['cluster_type'] == 'core_cluster'])
logger.info(f'Script completed in {time.time() - start_time:.2f} seconds')
logger.info(f'Found {len(clusters)} total clusters:')
logger.info(f' - {component_count} connected components')
logger.info(f' - {clique_count} cliques')
logger.info(f' - {core_count} core clusters')
logger.info(f'{clustered_queries} out of {total_queries} queries are in clusters')
logger.info(f'Results exported to: {output_path}')
# Show top consolidation opportunities
top_opportunities = results_df[results_df['consolidation_score'] >= 60].groupby('serp_cluster').first()
if not top_opportunities.empty:
logger.info(f"\nTop consolidation opportunities:")
for idx, row in top_opportunities.head(5).iterrows():
logger.info(f" - {idx}: Score {row['consolidation_score']} ({row['#_kws_in_cluster']} keywords)")
return results_df
except Exception as e:
logger.error(f"Error processing SERPs: {str(e)}")
raise
# ----------------
# Execute the Main Function
# ----------------
if __name__ == "__main__":
args = parse_args()
start = time.time()
process_serps(args)
print(f'The script took {time.time() - start:.2f} seconds!')