#!/usr/bin/env python3
"""Teknik SEO sağlık kontrolü (salt okuma): sitemap -> her URL için durum, yönlendirme,
title, description, canonical, noindex, h1, lang, og:image; iç linklerin durumu; çift title/desc.
Kullanım: python3 seo_saglik.py https://site.com cikti.json
Yerel build için: dist'i servis et ve yerel adresi ver (sitemap yerelde de olmalı)."""
import re, sys, json, urllib.request, urllib.parse, concurrent.futures as cf
from collections import Counter
SITE = sys.argv[1].rstrip('/')
UA = {'User-Agent': 'Mozilla/5.0 (SEO saglik kontrolu)'}
def get(u):
try:
r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=30)
return r.status, r.geturl(), r.read().decode('utf-8', 'ignore')
except urllib.error.HTTPError as e:
return e.code, u, ''
except Exception as e:
return 0, u, str(e)
_, _, sm = get(SITE + '/sitemap.xml')
urls = re.findall(r'(.*?)', sm)
def tag(h, pat):
m = re.search(pat, h, re.I | re.S); return m.group(1).strip() if m else ''
def check(u):
st, final, h = get(u)
return dict(url=u, status=st, final=final,
title=tag(h, r'
]', h, re.I)),
lang=tag(h, r']+lang=["\'](.*?)["\']'),
og=bool(re.search(r'property=["\']og:image', h, re.I)),
links=sorted(set(urllib.parse.urljoin(u, l).split('#')[0] for l in re.findall(r'href=["\'](/[^"\']*)["\']', h))))
with cf.ThreadPoolExecutor(8) as ex:
res = list(ex.map(check, urls))
alllinks = sorted({l for r in res for l in r['links'] if not re.search(r'\.(css|js|svg|png|jpg|ico|woff2?|xml|json|webmanifest)$', l)})
with cf.ThreadPoolExecutor(8) as ex:
lst = dict(zip(alllinks, ex.map(lambda l: get(l)[0], alllinks)))
bad_links = {l: s for l, s in lst.items() if s != 200}
tc = Counter(r['title'] for r in res); dc = Counter(r['desc'] for r in res if r['desc'])
out = dict(site=SITE, sitemap_url_sayisi=len(urls), sayfalar=[{k: v for k, v in r.items() if k != 'links'} for r in res],
kirik_ic_linkler=bad_links, cift_title=[t for t, n in tc.items() if n > 1], cift_desc=[d for d, n in dc.items() if n > 1],
ic_link_sayisi=len(alllinks))
json.dump(out, open(sys.argv[2], 'w'), ensure_ascii=False, indent=1)
print('URL', len(urls), '| iç link', len(alllinks), '| kırık', len(bad_links))
for r in res:
s = []
if r['status'] != 200: s.append(f"durum {r['status']}")
if r['final'].rstrip('/') != r['url'].rstrip('/'): s.append(f"yönlendirme -> {r['final']}")
if not r['title']: s.append('title yok')
elif len(r['title']) > 65: s.append(f"title uzun {len(r['title'])}")
if not r['desc']: s.append('description yok')
elif len(r['desc']) > 160: s.append(f"desc uzun {len(r['desc'])}")
if not r['canonical']: s.append('canonical yok')
elif r['canonical'].rstrip('/') != r['url'].rstrip('/'): s.append(f"canonical farklı {r['canonical']}")
if r['noindex']: s.append('NOINDEX')
if r['h1'] != 1: s.append(f"h1 sayısı {r['h1']}")
if not r['lang']: s.append('lang yok')
if not r['og']: s.append('og:image yok')
print(('OK ' if not s else '!! ') + r['url'].replace(SITE, '') + ('' if not s else ' ' + '; '.join(s)))
for l, s in bad_links.items(): print('KIRIK', s, l)
print('çift title', out['cift_title']); print('çift desc', out['cift_desc'])