summaryrefslogtreecommitdiff
path: root/scripts/listExternalLinks.py
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-05-23 16:09:01 +0800
committerEltrac <hi@guhub.cn>2025-05-23 16:09:01 +0800
commit756af4514a46e3274879809a5470f809538e7530 (patch)
tree8314a7be5c57367bed28ef13b33339b64ce72b14 /scripts/listExternalLinks.py
parentaea9ddc38d12eab43c3a031e5cd1801e13d64bcd (diff)
feat: list external links ranking
also move python scripts to one folder Former-commit-id: 60d68be1b4ea81b235b73924d2a98be67d1ebfc9
Diffstat (limited to 'scripts/listExternalLinks.py')
-rw-r--r--scripts/listExternalLinks.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/scripts/listExternalLinks.py b/scripts/listExternalLinks.py
new file mode 100644
index 00000000..6b52ab76
--- /dev/null
+++ b/scripts/listExternalLinks.py
@@ -0,0 +1,53 @@
+import os
+from urllib.parse import urlparse
+from collections import Counter
+import json
+import re
+
+content_dir = "content"
+external_links = Counter()
+
+# 匹配 Markdown 中的 URL 链接 [text](url)
+markdown_link_re = re.compile(r'\[.*?\]\((http.*?)\)')
+
+def extract_front_matter_and_content(path):
+ with open(path, 'r', encoding='utf-8') as f:
+ lines = f.readlines()
+
+ if not lines or lines[0].strip() != '---':
+ return {}, ''.join(lines)
+
+ front_matter = {}
+ end_index = None
+ for i in range(1, len(lines)):
+ if lines[i].strip() == '---':
+ end_index = i
+ break
+ key_value = lines[i].split(':', 1)
+ if len(key_value) == 2:
+ key, value = key_value
+ front_matter[key.strip()] = value.strip()
+
+ content = ''.join(lines[end_index+1:]) if end_index else ''.join(lines)
+ return front_matter, content
+
+def extract_links_from_file(path):
+ _, content = extract_front_matter_and_content(path)
+
+ # 提取 Markdown 格式链接
+ for match in markdown_link_re.findall(content):
+ domain = urlparse(match).netloc
+ if domain:
+ external_links[domain] += 1
+
+for root, dirs, files in os.walk(content_dir):
+ for file in files:
+ if file.endswith(".md"):
+ extract_links_from_file(os.path.join(root, file))
+
+# 写入 JSON
+os.makedirs("data", exist_ok=True)
+with open("data/externallinks.json", "w", encoding="utf-8") as f:
+ json.dump(external_links.most_common(), f, indent=2, ensure_ascii=False)
+
+print("✅ External link data written to data/externallinks.json")