-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathsubstack_scraper.py
More file actions
1367 lines (1161 loc) · 54.5 KB
/
substack_scraper.py
File metadata and controls
1367 lines (1161 loc) · 54.5 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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import hashlib
import json
import mimetypes
import os
import random
import re
import shutil
import subprocess
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from urllib.parse import unquote, urlparse
from typing import List, Optional, Tuple
from time import sleep
import html2text
import markdown
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from tqdm import tqdm
from xml.etree import ElementTree as ET
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.common.exceptions import SessionNotCreatedException, WebDriverException
from config import EMAIL, PASSWORD
USE_PREMIUM: bool = True
BASE_SUBSTACK_URL: str = "https://niallferguson.substack.com/"
BASE_MD_DIR: str = "substack_md_files"
BASE_HTML_DIR: str = "substack_html_pages"
BASE_IMAGE_DIR: str = "substack_images"
HTML_TEMPLATE: str = "author_template.html"
JSON_DATA_DIR: str = "data"
NUM_POSTS_TO_SCRAPE: int = 0
def resolve_image_url(url: str) -> str:
"""Get the original image URL from a Substack CDN URL."""
if url.startswith("https://substackcdn.com/image/fetch/"):
parts = url.split("/https%3A%2F%2F")
if len(parts) > 1:
url = "https://" + unquote(parts[1])
return url
def clean_linked_images(md_content: str) -> str:
"""Converts markdown linked images [](link) to ."""
pattern = r'\[!\[(.*?)\]\((.*?)\)\]\(.*?\)'
return re.sub(pattern, r'', md_content)
def count_images_in_markdown(md_content: str) -> int:
"""Count number of image references in markdown content."""
cleaned_content = clean_linked_images(md_content)
pattern = r'!\[.*?\]\((.*?)\)'
matches = re.findall(pattern, cleaned_content)
return len(matches)
def is_post_url(url: str) -> bool:
"""Check if URL points to a specific post (contains /p/)."""
return "/p/" in url
def get_publication_url(url: str) -> str:
"""Extract the base publication URL from a post URL."""
parsed = urlparse(url)
return f"{parsed.scheme}://{parsed.netloc}/"
def get_post_slug(url: str) -> str:
"""Extract the post slug from a Substack post URL."""
match = re.search(r'/p/([^/]+)', url)
return match.group(1) if match else 'unknown_post'
def sanitize_image_filename(url: str) -> str:
"""Create a safe filename from an image URL."""
url = resolve_image_url(url)
filename = url.split("/")[-1]
filename = filename.split("?")[0]
filename = re.sub(r'[<>:"/\\|?*]', '', filename)
if len(filename) > 100 or not filename:
hash_object = hashlib.md5(url.encode())
ext = mimetypes.guess_extension(
requests.head(url).headers.get('content-type', '')
) or '.jpg'
filename = f"{hash_object.hexdigest()}{ext}"
return filename
def download_image(url: str, save_path: Path, pbar=None) -> Optional[str]:
"""Download image from URL and save to path."""
try:
response = requests.get(url, stream=True)
if response.status_code == 200:
save_path.parent.mkdir(parents=True, exist_ok=True)
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
if pbar:
pbar.update(1)
return str(save_path)
except Exception as e:
msg = f"Error downloading image {url}: {str(e)}"
if pbar:
pbar.write(msg)
else:
print(msg)
return None
def process_markdown_images(md_content: str, author: str, post_slug: str, pbar=None) -> str:
"""Process markdown content to download images and update references."""
image_dir = Path(BASE_IMAGE_DIR) / author / post_slug
md_content = clean_linked_images(md_content)
def replace_image(match):
url = match.group(0).strip('()')
resolved_url = resolve_image_url(url)
filename = sanitize_image_filename(url)
save_path = image_dir / filename
if not save_path.exists():
download_image(resolved_url, save_path, pbar)
rel_path = os.path.relpath(save_path, Path(BASE_MD_DIR) / author)
return f"({rel_path})"
pattern = r'\(https://substackcdn\.com/image/fetch/[^\s\)]+\)'
return re.sub(pattern, replace_image, md_content)
def extract_main_part(url: str) -> str:
parts = urlparse(url).netloc.split('.')
return parts[1] if parts[0] == 'www' else parts[0]
def generate_html_file(author_name: str) -> None:
"""Generates a HTML file for the given author."""
if not os.path.exists(BASE_HTML_DIR):
os.makedirs(BASE_HTML_DIR)
json_path = os.path.join(JSON_DATA_DIR, f'{author_name}.json')
with open(json_path, 'r', encoding='utf-8') as file:
essays_data = json.load(file)
embedded_json_data = json.dumps(essays_data, ensure_ascii=False, indent=4)
with open(HTML_TEMPLATE, 'r', encoding='utf-8') as file:
html_template = file.read()
html_with_data = html_template.replace('<!-- AUTHOR_NAME -->', author_name).replace(
'<script type="application/json" id="essaysData"></script>',
f'<script type="application/json" id="essaysData">{embedded_json_data}</script>'
)
html_with_author = html_with_data.replace('author_name', author_name)
html_output_path = os.path.join(BASE_HTML_DIR, f'{author_name}.html')
with open(html_output_path, 'w', encoding='utf-8') as file:
file.write(html_with_author)
# =============================================================================
# BROWSER/DRIVER UTILITIES
# =============================================================================
class BrowserManager:
"""
Handles browser detection, driver management, and initialization.
Supports Chrome (preferred) and Edge with robust fallback logic.
Key insight: Instead of trying to move/delete system drivers (requires admin),
we download to a local cache and use explicit paths, bypassing PATH entirely.
"""
SUPPORTED_BROWSERS = ['chrome', 'edge']
CACHE_DIR = os.path.join(os.path.expanduser('~'), '.substack_scraper', 'drivers')
@classmethod
def get_cache_dir(cls) -> str:
"""Get or create the local driver cache directory."""
if not os.path.exists(cls.CACHE_DIR):
os.makedirs(cls.CACHE_DIR)
return cls.CACHE_DIR
@staticmethod
def get_browser_version(browser: str) -> Optional[str]:
"""
Attempts to detect the installed browser version.
Returns version string or None if not found.
"""
version = None
if browser == 'chrome':
if os.name == 'nt': # Windows
paths = [
r'C:\Program Files\Google\Chrome\Application\chrome.exe',
r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe',
os.path.expandvars(r'%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe'),
]
for path in paths:
if os.path.exists(path):
try:
result = subprocess.run(
['powershell', '-Command', f'(Get-Item "{path}").VersionInfo.FileVersion'],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
version = result.stdout.strip()
break
except Exception:
pass
else: # macOS/Linux
try:
result = subprocess.run(
['google-chrome', '--version'],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
match = re.search(r'(\d+\.\d+\.\d+\.\d+)', result.stdout)
if match:
version = match.group(1)
except Exception:
pass
elif browser == 'edge':
if os.name == 'nt': # Windows
paths = [
r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe',
r'C:\Program Files\Microsoft\Edge\Application\msedge.exe',
]
for path in paths:
if os.path.exists(path):
try:
result = subprocess.run(
['powershell', '-Command', f'(Get-Item "{path}").VersionInfo.FileVersion'],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
version = result.stdout.strip()
break
except Exception:
pass
else: # macOS/Linux
try:
result = subprocess.run(
['microsoft-edge', '--version'],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
match = re.search(r'(\d+\.\d+\.\d+\.\d+)', result.stdout)
if match:
version = match.group(1)
except Exception:
pass
return version
@staticmethod
def get_driver_version(driver_path: str) -> Optional[str]:
"""Get the version of a webdriver executable."""
if not os.path.exists(driver_path):
return None
try:
result = subprocess.run(
[driver_path, '--version'],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
match = re.search(r'(\d+\.\d+\.\d+\.\d+)', result.stdout)
if match:
return match.group(1)
except Exception:
pass
return None
@staticmethod
def versions_compatible(browser_version: str, driver_version: str) -> bool:
"""Check if browser and driver major versions match."""
if not browser_version or not driver_version:
return False
try:
browser_major = int(browser_version.split('.')[0])
driver_major = int(driver_version.split('.')[0])
return browser_major == driver_major
except (ValueError, IndexError):
return False
@staticmethod
def find_stale_drivers() -> List[str]:
"""Find potentially stale driver executables in common PATH locations."""
stale_paths = []
common_locations = [
r'C:\Windows\msedgedriver.exe',
r'C:\Windows\chromedriver.exe',
r'C:\Windows\System32\msedgedriver.exe',
r'C:\Windows\System32\chromedriver.exe',
]
for path in common_locations:
if os.path.exists(path):
stale_paths.append(path)
return stale_paths
@staticmethod
def get_user_data_dir(browser: str) -> str:
"""Returns a custom user data directory for browser session persistence."""
base_dir = os.path.join(os.path.expanduser('~'), '.substack_scraper')
if not os.path.exists(base_dir):
os.makedirs(base_dir)
return os.path.join(base_dir, f'{browser}_profile')
@classmethod
def download_driver_with_requests(cls, browser: str, browser_version: str) -> Optional[str]:
"""
Download the correct driver directly using requests.
This bypasses webdriver_manager issues and gives us full control.
Returns the path to the downloaded driver, or None if failed.
"""
import zipfile
import io
major_version = browser_version.split('.')[0]
cache_dir = cls.get_cache_dir()
if browser == 'chrome':
# Chrome for Testing JSON endpoint
driver_name = 'chromedriver.exe' if os.name == 'nt' else 'chromedriver'
driver_path = os.path.join(cache_dir, f'chromedriver-{major_version}', driver_name)
# Check if we already have a compatible driver cached
if os.path.exists(driver_path):
cached_version = cls.get_driver_version(driver_path)
if cached_version and cls.versions_compatible(browser_version, cached_version):
print(f"Using cached chromedriver {cached_version}")
return driver_path
try:
# Get the latest driver version for this Chrome version
print(f"Fetching Chrome driver info for version {major_version}...")
# Try the Chrome for Testing endpoints
endpoints = [
f"https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_{major_version}",
"https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json",
]
driver_version = None
download_url = None
# Try LATEST_RELEASE endpoint first
try:
resp = requests.get(endpoints[0], timeout=30)
if resp.ok:
driver_version = resp.text.strip()
# Construct download URL
platform = 'win64' if os.name == 'nt' else ('mac-x64' if sys.platform == 'darwin' else 'linux64')
download_url = f"https://storage.googleapis.com/chrome-for-testing-public/{driver_version}/{platform}/chromedriver-{platform}.zip"
except Exception:
pass
# Fallback to JSON endpoint
if not download_url:
resp = requests.get(endpoints[1], timeout=30)
if resp.ok:
data = resp.json()
channels = data.get('channels', {})
stable = channels.get('Stable', {})
driver_version = stable.get('version', '')
if driver_version.startswith(major_version):
downloads = stable.get('downloads', {}).get('chromedriver', [])
platform = 'win64' if os.name == 'nt' else ('mac-x64' if sys.platform == 'darwin' else 'linux64')
for d in downloads:
if d.get('platform') == platform:
download_url = d.get('url')
break
if not download_url:
print(f"Could not find chromedriver download URL for Chrome {major_version}")
return None
print(f"Downloading chromedriver {driver_version}...")
resp = requests.get(download_url, timeout=120)
if not resp.ok:
print(f"Download failed: HTTP {resp.status_code}")
return None
# Extract the driver
extract_dir = os.path.join(cache_dir, f'chromedriver-{major_version}')
if os.path.exists(extract_dir):
shutil.rmtree(extract_dir)
os.makedirs(extract_dir)
with zipfile.ZipFile(io.BytesIO(resp.content)) as zf:
# Find the chromedriver executable in the zip
for name in zf.namelist():
if name.endswith(driver_name):
# Extract to our directory
source = zf.open(name)
target_path = os.path.join(extract_dir, driver_name)
with open(target_path, 'wb') as target:
target.write(source.read())
# Make executable on Unix
if os.name != 'nt':
os.chmod(target_path, 0o755)
print(f"[OK] Chromedriver downloaded to: {target_path}")
return target_path
print("Could not find chromedriver in downloaded archive")
return None
except Exception as e:
print(f"Failed to download chromedriver: {e}")
return None
elif browser == 'edge':
driver_name = 'msedgedriver.exe' if os.name == 'nt' else 'msedgedriver'
driver_path = os.path.join(cache_dir, f'msedgedriver-{major_version}', driver_name)
# Check cache
if os.path.exists(driver_path):
cached_version = cls.get_driver_version(driver_path)
if cached_version and cls.versions_compatible(browser_version, cached_version):
print(f"Using cached msedgedriver {cached_version}")
return driver_path
try:
# Get latest Edge driver version
print(f"Fetching Edge driver info for version {major_version}...")
# Edge driver download URL pattern
platform = 'win64' if os.name == 'nt' else ('mac64' if sys.platform == 'darwin' else 'linux64')
# Try to get the exact version
version_url = f"https://msedgedriver.azureedge.net/LATEST_RELEASE_{major_version}"
try:
resp = requests.get(version_url, timeout=30)
if resp.ok:
driver_version = resp.text.strip()
else:
driver_version = browser_version # Fall back to browser version
except Exception:
driver_version = browser_version
download_url = f"https://msedgedriver.azureedge.net/{driver_version}/edgedriver_{platform}.zip"
print(f"Downloading msedgedriver {driver_version}...")
resp = requests.get(download_url, timeout=120)
if not resp.ok:
print(f"Download failed: HTTP {resp.status_code}")
return None
# Extract
extract_dir = os.path.join(cache_dir, f'msedgedriver-{major_version}')
if os.path.exists(extract_dir):
shutil.rmtree(extract_dir)
os.makedirs(extract_dir)
with zipfile.ZipFile(io.BytesIO(resp.content)) as zf:
for name in zf.namelist():
if name.endswith(driver_name):
source = zf.open(name)
target_path = os.path.join(extract_dir, driver_name)
with open(target_path, 'wb') as target:
target.write(source.read())
if os.name != 'nt':
os.chmod(target_path, 0o755)
print(f"[OK] msedgedriver downloaded to: {target_path}")
return target_path
print("Could not find msedgedriver in downloaded archive")
return None
except Exception as e:
print(f"Failed to download msedgedriver: {e}")
return None
return None
@classmethod
def create_driver(
cls,
browser: str = 'chrome',
headless: bool = False,
driver_path: Optional[str] = None,
browser_path: Optional[str] = None,
user_agent: Optional[str] = None,
use_persistent_profile: bool = False,
) -> webdriver.Remote:
"""
Creates a WebDriver instance with smart fallback logic.
Strategy:
1. Use explicit driver path if provided
2. Check our local cache for a compatible driver
3. Download driver directly to our cache (bypasses PATH issues)
4. Fall back to webdriver_manager
5. Fall back to Selenium Manager
"""
browser = browser.lower()
if browser not in cls.SUPPORTED_BROWSERS:
raise ValueError(f"Unsupported browser: {browser}. Use one of: {cls.SUPPORTED_BROWSERS}")
# Check for stale drivers (for warning purposes only)
stale_drivers = cls.find_stale_drivers()
if stale_drivers:
print(f"WARNING: Found old drivers in system PATH that may cause issues if other methods fail:")
for p in stale_drivers:
v = cls.get_driver_version(p) or "unknown"
print(f" - {p} (version: {v})")
print(" We'll try to bypass these by using our own driver cache.\n")
# Detect browser version
browser_version = cls.get_browser_version(browser)
print(f"Detected {browser.title()} version: {browser_version or 'unknown'}")
if not browser_version:
print(f"WARNING: Could not detect {browser.title()} version. Make sure it's installed.")
# Build options
if browser == 'chrome':
options = ChromeOptions()
else:
options = EdgeOptions()
if headless:
options.add_argument("--headless=new")
if browser_path:
options.binary_location = browser_path
if user_agent:
options.add_argument(f"user-agent={user_agent}")
if use_persistent_profile:
profile_dir = cls.get_user_data_dir(browser)
options.add_argument(f"user-data-dir={profile_dir}")
print(f"Using persistent profile at: {profile_dir}")
# Common options for stability
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
errors = []
# Strategy 1: Explicit driver path
if driver_path and os.path.exists(driver_path):
try:
print(f"Using explicit driver path: {driver_path}")
driver_version = cls.get_driver_version(driver_path)
if driver_version:
print(f"Driver version: {driver_version}")
if browser_version and not cls.versions_compatible(browser_version, driver_version):
print(f"WARNING: Driver version may not match browser version")
if browser == 'chrome':
service = ChromeService(executable_path=driver_path)
return webdriver.Chrome(service=service, options=options)
else:
service = EdgeService(executable_path=driver_path)
return webdriver.Edge(service=service, options=options)
except Exception as e:
errors.append(f"Explicit driver path failed: {e}")
print(f"[FAIL] Explicit driver path failed: {e}")
# Strategy 2: Download to our cache (primary method - bypasses PATH issues)
if browser_version:
print(f"\nDownloading driver to local cache (bypasses system PATH)...")
try:
downloaded_path = cls.download_driver_with_requests(browser, browser_version)
if downloaded_path and os.path.exists(downloaded_path):
print(f"Using downloaded driver: {downloaded_path}")
if browser == 'chrome':
service = ChromeService(executable_path=downloaded_path)
return webdriver.Chrome(service=service, options=options)
else:
service = EdgeService(executable_path=downloaded_path)
return webdriver.Edge(service=service, options=options)
except Exception as e:
errors.append(f"Direct download failed: {e}")
print(f"[FAIL] Direct download failed: {e}")
# Strategy 3: webdriver_manager with explicit path
print("\nTrying webdriver_manager...")
try:
if browser == 'chrome':
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
mgr = ChromeDriverManager()
driver_path_wdm = mgr.install()
print(f"webdriver_manager installed driver to: {driver_path_wdm}")
service = ChromeService(executable_path=driver_path_wdm)
return webdriver.Chrome(service=service, options=options)
else:
from webdriver_manager.microsoft import EdgeChromiumDriverManager
mgr = EdgeChromiumDriverManager()
driver_path_wdm = mgr.install()
print(f"webdriver_manager installed driver to: {driver_path_wdm}")
service = EdgeService(executable_path=driver_path_wdm)
return webdriver.Edge(service=service, options=options)
except Exception as e:
errors.append(f"webdriver_manager failed: {e}")
print(f"[FAIL] webdriver_manager failed: {e}")
# Strategy 4: Let Selenium Manager try (last resort)
print("\nTrying Selenium Manager (last resort)...")
try:
if browser == 'chrome':
return webdriver.Chrome(options=options)
else:
return webdriver.Edge(options=options)
except Exception as e:
errors.append(f"Selenium Manager failed: {e}")
print(f"[FAIL] Selenium Manager failed: {e}")
# All strategies failed
error_msg = cls._build_error_message(browser, browser_version, stale_drivers, errors)
raise RuntimeError(error_msg)
@classmethod
def _build_error_message(
cls,
browser: str,
browser_version: Optional[str],
stale_drivers: List[str],
errors: List[str]
) -> str:
"""Build a helpful error message when driver creation fails."""
lines = [
"",
"=" * 70,
"BROWSER DRIVER SETUP FAILED",
"=" * 70,
"",
f"Could not start {browser.title()} WebDriver.",
"",
]
if browser_version:
lines.append(f"Your {browser.title()} version: {browser_version}")
major_version = browser_version.split('.')[0]
else:
lines.append(f"Could not detect your {browser.title()} version.")
major_version = "XXX"
lines.append("")
if stale_drivers:
lines.extend([
"STALE DRIVERS IN SYSTEM PATH:",
"These old drivers may have interfered with automatic setup:",
])
for path in stale_drivers:
driver_ver = cls.get_driver_version(path) or "unknown version"
lines.append(f" - {path} (version: {driver_ver})")
lines.extend([
"",
"To fix: Open an Administrator command prompt and delete these files,",
"or rename them (e.g., chromedriver.exe.bak)",
"",
])
lines.extend([
"HOW TO FIX:",
"",
"Option 1: Download the correct driver manually",
])
if browser == 'chrome':
lines.extend([
f" 1. Go to: https://googlechromelabs.github.io/chrome-for-testing/",
f" 2. Download chromedriver for version {major_version} (win64)",
f" 3. Extract chromedriver.exe somewhere (e.g., C:\\tools\\chromedriver.exe)",
f" 4. Run with: --chrome-driver-path C:\\tools\\chromedriver.exe",
])
else:
lines.extend([
f" 1. Go to: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/",
f" 2. Download msedgedriver for version {major_version}",
f" 3. Extract msedgedriver.exe somewhere (e.g., C:\\tools\\msedgedriver.exe)",
f" 4. Run with: --edge-driver-path C:\\tools\\msedgedriver.exe",
])
lines.extend([
"",
"Option 2: Try a different browser",
f" python substack_scraper.py --premium --browser {'edge' if browser == 'chrome' else 'chrome'}",
"",
"Option 3: Delete stale drivers (requires Administrator)",
" Open cmd as Administrator and run:",
])
for path in stale_drivers:
lines.append(f" del \"{path}\"")
lines.extend([
"",
"-" * 70,
"Debug info (errors encountered):",
])
for i, error in enumerate(errors, 1):
error_short = str(error)[:300] + "..." if len(str(error)) > 300 else str(error)
lines.append(f" {i}. {error_short}")
lines.extend(["", "=" * 70])
return "\n".join(lines)
# =============================================================================
# BASE SCRAPER CLASS
# =============================================================================
class BaseSubstackScraper(ABC):
def __init__(
self,
base_substack_url: str,
md_save_dir: str,
html_save_dir: str,
download_images: bool = False,
):
self.is_single_post: bool = is_post_url(base_substack_url)
self.post_slug: Optional[str] = get_post_slug(base_substack_url) if self.is_single_post else None
original_url = base_substack_url
if self.is_single_post:
base_substack_url = get_publication_url(base_substack_url)
if not base_substack_url.endswith("/"):
base_substack_url += "/"
self.base_substack_url: str = base_substack_url
self.writer_name: str = extract_main_part(base_substack_url)
md_save_dir: str = f"{md_save_dir}/{self.writer_name}"
self.md_save_dir: str = md_save_dir
self.html_save_dir: str = f"{html_save_dir}/{self.writer_name}"
if not os.path.exists(md_save_dir):
os.makedirs(md_save_dir)
print(f"Created md directory {md_save_dir}")
if not os.path.exists(self.html_save_dir):
os.makedirs(self.html_save_dir)
print(f"Created html directory {self.html_save_dir}")
self.download_images: bool = download_images
self.image_dir = Path(BASE_IMAGE_DIR) / self.writer_name
if self.is_single_post:
self.post_urls: List[str] = [original_url]
else:
self.keywords: List[str] = ["about", "archive", "podcast"]
self.post_urls: List[str] = self.get_all_post_urls()
def get_all_post_urls(self) -> List[str]:
"""Attempts to fetch URLs from sitemap.xml, falling back to feed.xml if necessary."""
urls = self.fetch_urls_from_sitemap()
if not urls:
urls = self.fetch_urls_from_feed()
return self.filter_urls(urls, self.keywords)
def fetch_urls_from_sitemap(self) -> List[str]:
"""Fetches URLs from sitemap.xml."""
sitemap_url = f"{self.base_substack_url}sitemap.xml"
response = requests.get(sitemap_url)
if not response.ok:
print(f'Error fetching sitemap at {sitemap_url}: {response.status_code}')
return []
root = ET.fromstring(response.content)
urls = [element.text for element in root.iter('{http://www.sitemaps.org/schemas/sitemap/0.9}loc')]
return urls
def fetch_urls_from_feed(self) -> List[str]:
"""Fetches URLs from feed.xml."""
print('Falling back to feed.xml. This will only contain up to the 22 most recent posts.')
feed_url = f"{self.base_substack_url}feed.xml"
response = requests.get(feed_url)
if not response.ok:
print(f'Error fetching feed at {feed_url}: {response.status_code}')
return []
root = ET.fromstring(response.content)
urls = []
for item in root.findall('.//item'):
link = item.find('link')
if link is not None and link.text:
urls.append(link.text)
return urls
@staticmethod
def filter_urls(urls: List[str], keywords: List[str]) -> List[str]:
"""Filters out URLs that contain certain keywords."""
return [url for url in urls if all(keyword not in url for keyword in keywords)]
@staticmethod
def html_to_md(html_content: str) -> str:
"""Converts HTML to Markdown."""
if not isinstance(html_content, str):
raise ValueError("html_content must be a string")
h = html2text.HTML2Text()
h.ignore_links = False
h.body_width = 0
return h.handle(html_content)
@staticmethod
def save_to_file(filepath: str, content: str) -> None:
"""Saves content to a file."""
if not isinstance(filepath, str):
raise ValueError("filepath must be a string")
if not isinstance(content, str):
raise ValueError("content must be a string")
if os.path.exists(filepath):
print(f"File already exists: {filepath}")
return
with open(filepath, 'w', encoding='utf-8') as file:
file.write(content)
@staticmethod
def md_to_html(md_content: str) -> str:
"""Converts Markdown to HTML."""
return markdown.markdown(md_content, extensions=['extra'])
def save_to_html_file(self, filepath: str, content: str) -> None:
"""Saves HTML content to a file with a link to an external CSS file."""
if not isinstance(filepath, str):
raise ValueError("filepath must be a string")
if not isinstance(content, str):
raise ValueError("content must be a string")
html_dir = os.path.dirname(filepath)
css_path = os.path.relpath("./assets/css/essay-styles.css", html_dir)
css_path = css_path.replace("\\", "/")
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Markdown Content</title>
<link rel="stylesheet" href="{css_path}">
</head>
<body>
<main class="markdown-content">
{content}
</main>
</body>
</html>
"""
with open(filepath, 'w', encoding='utf-8') as file:
file.write(html_content)
@staticmethod
def get_filename_from_url(url: str, filetype: str = ".md") -> str:
"""Gets the filename from the URL."""
if not isinstance(url, str):
raise ValueError("url must be a string")
if not isinstance(filetype, str):
raise ValueError("filetype must be a string")
if not filetype.startswith("."):
filetype = f".{filetype}"
return url.split("/")[-1] + filetype
@staticmethod
def combine_metadata_and_content(title: str, subtitle: str, date: str, like_count: str, content) -> str:
"""Combines the title, subtitle, and content into a single string with Markdown format."""
if not isinstance(title, str):
raise ValueError("title must be a string")
if not isinstance(content, str):
raise ValueError("content must be a string")
metadata = f"# {title}\n\n"
if subtitle:
metadata += f"## {subtitle}\n\n"
metadata += f"**{date}**\n\n"
metadata += f"**Likes:** {like_count}\n\n"
return metadata + content
def extract_post_data(self, soup: BeautifulSoup) -> Tuple[str, str, str, str, str]:
"""Converts a Substack post soup to markdown, returning metadata and content."""
# Title
title_element = soup.select_one("h1.post-title, h2")
title = title_element.text.strip() if title_element else "Untitled"
# Subtitle
subtitle_element = soup.select_one("h3.subtitle, div.subtitle-HEEcLo")
subtitle = subtitle_element.text.strip() if subtitle_element else ""
# Date
date = ""
date_element = soup.select_one("div.meta-EgzBVA")
if date_element and date_element.text.strip():
date = date_element.text.strip()
if not date:
script_tag = soup.find("script", {"type": "application/ld+json"})
if script_tag and script_tag.string:
try:
metadata = json.loads(script_tag.string)
if "datePublished" in metadata:
date_str = metadata["datePublished"]
date_obj = datetime.fromisoformat(date_str.replace("Z", "+00:00"))
date = date_obj.strftime("%b %d, %Y")
except (json.JSONDecodeError, ValueError, KeyError):
pass
if not date:
date = "Date not found"
# Like count
like_count_element = soup.select_one('div.like-button-container button div.label')
like_count = (
like_count_element.text.strip()
if like_count_element and like_count_element.text.strip().isdigit()
else "0"
)
# Content
content_element = soup.select_one("div.available-content")
content_html = str(content_element) if content_element else ""
md = self.html_to_md(content_html)
md_content = self.combine_metadata_and_content(title, subtitle, date, like_count, md)
return title, subtitle, like_count, date, md_content
@abstractmethod
def get_url_soup(self, url: str) -> str:
raise NotImplementedError
def save_essays_data_to_json(self, essays_data: list) -> None:
"""Saves essays data to a JSON file for a specific author."""
data_dir = os.path.join(JSON_DATA_DIR)
if not os.path.exists(data_dir):
os.makedirs(data_dir)
json_path = os.path.join(data_dir, f'{self.writer_name}.json')
if os.path.exists(json_path):
with open(json_path, 'r', encoding='utf-8') as file:
existing_data = json.load(file)
essays_data = existing_data + [data for data in essays_data if data not in existing_data]
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(essays_data, f, ensure_ascii=False, indent=4)
def scrape_posts(self, num_posts_to_scrape: int = 0) -> None:
"""Iterates over all posts and saves them as markdown and html files."""
essays_data = []
count = 0
total = num_posts_to_scrape if num_posts_to_scrape != 0 else len(self.post_urls)
with tqdm(total=total, desc="Scraping posts") as pbar:
for url in self.post_urls:
try:
md_filename = self.get_filename_from_url(url, filetype=".md")
html_filename = self.get_filename_from_url(url, filetype=".html")
md_filepath = os.path.join(self.md_save_dir, md_filename)
html_filepath = os.path.join(self.html_save_dir, html_filename)
if not os.path.exists(md_filepath):
soup = self.get_url_soup(url)
if soup is None:
total += 1
pbar.total = total
pbar.refresh()
continue
title, subtitle, like_count, date, md = self.extract_post_data(soup)
if self.download_images:
total_images = count_images_in_markdown(md)
slug = get_post_slug(url) if is_post_url(url) else url.rstrip('/').split('/')[-1]
with tqdm(
total=total_images,
desc=f"Downloading images for {slug}",
leave=False,
) as img_pbar:
md = process_markdown_images(md, self.writer_name, slug, img_pbar)
self.save_to_file(md_filepath, md)
html_content = self.md_to_html(md)
self.save_to_html_file(html_filepath, html_content)