|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +BASE_DIR = r'c:\Users\PMLS\Desktop\Youtube Shorts\b2b_blog' |
| 5 | +POSTS_DIR = os.path.join(BASE_DIR, 'posts') |
| 6 | + |
| 7 | +def get_meta_content(content, property_name): |
| 8 | + match = re.search(f'<meta property="{property_name}" content="([^"]+)"', content) |
| 9 | + if not match: |
| 10 | + match = re.search(f'<meta name="{property_name}" content="([^"]+)"', content) |
| 11 | + return match.group(1) if match else None |
| 12 | + |
| 13 | +def optimize_file(fpath): |
| 14 | + with open(fpath, 'r', encoding='utf-8') as f: |
| 15 | + content = f.read() |
| 16 | + |
| 17 | + new_content = content |
| 18 | + rel_path = os.path.relpath(fpath, BASE_DIR).replace('\\', '/') |
| 19 | + canonical_url = f"https://techstackglobal.github.io/{rel_path}" |
| 20 | + |
| 21 | + # 1. Ensure Canonical Tag exists |
| 22 | + if '<link rel="canonical"' not in new_content and '<link href="https://techstackglobal.github.io' not in new_content: |
| 23 | + # Insert before </head> |
| 24 | + new_content = new_content.replace('</head>', f' <link rel="canonical" href="{canonical_url}" />\n</head>') |
| 25 | + print(f"Added Canonical: {rel_path}") |
| 26 | + |
| 27 | + # 2. Inject/Update Twitter Metadata based on OpenGraph |
| 28 | + og_title = get_meta_content(new_content, "og:title") |
| 29 | + og_desc = get_meta_content(new_content, "og:description") |
| 30 | + og_image = get_meta_content(new_content, "og:image") |
| 31 | + |
| 32 | + if og_title and 'name="twitter:title"' not in new_content: |
| 33 | + new_content = new_content.replace('<!-- Twitter -->', f'<!-- Twitter -->\n <meta name="twitter:title" content="{og_title}">') |
| 34 | + if og_desc and 'name="twitter:description"' not in new_content: |
| 35 | + new_content = new_content.replace('<!-- Twitter -->', f'<!-- Twitter -->\n <meta name="twitter:description" content="{og_desc}">') |
| 36 | + if og_image and 'name="twitter:image"' not in new_content: |
| 37 | + new_content = new_content.replace('<!-- Twitter -->', f'<!-- Twitter -->\n <meta name="twitter:image" content="{og_image}">') |
| 38 | + if 'name="twitter:card"' not in new_content: |
| 39 | + new_content = new_content.replace('<!-- Twitter -->', '<!-- Twitter -->\n <meta name="twitter:card" content="summary_large_image">') |
| 40 | + |
| 41 | + # 3. Synchronize Schema Logo |
| 42 | + # Old: /assets/icons/techstack-logo-192.png |
| 43 | + # New: /apple-touch-icon.png (high res TSG) |
| 44 | + old_logo_path = "assets/icons/techstack-logo-192.png" |
| 45 | + new_logo_url = "https://techstackglobal.github.io/apple-touch-icon.png" |
| 46 | + if old_logo_path in new_content: |
| 47 | + new_content = new_content.replace(old_logo_path, "apple-touch-icon.png") |
| 48 | + print(f"Updated Schema Logo: {rel_path}") |
| 49 | + |
| 50 | + if new_content != content: |
| 51 | + with open(fpath, 'w', encoding='utf-8') as f: |
| 52 | + f.write(new_content) |
| 53 | + return True |
| 54 | + return False |
| 55 | + |
| 56 | +# Execution logic |
| 57 | +files_to_check = [] |
| 58 | +for root, dirs, files in os.walk(BASE_DIR): |
| 59 | + if any(skip in root for skip in ['.git', '.agent', 'node_modules', '.venv']): continue |
| 60 | + for f in files: |
| 61 | + if f.endswith('.html'): |
| 62 | + files_to_check.append(os.path.join(root, f)) |
| 63 | + |
| 64 | +updated = 0 |
| 65 | +for fpath in files_to_check: |
| 66 | + if optimize_file(fpath): |
| 67 | + updated += 1 |
| 68 | + |
| 69 | +print(f"\nOptimization Sweep Complete. Files Prepared: {updated}") |
0 commit comments