Files
insta/namecreate/username_utils.py
Beyhan Oğur 2be3a313ad first commit
2026-04-26 22:26:46 +03:00

61 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
def normalize_for_username(value):
"""Turkce karakterleri ASCII'ye cevirip username-safe hale getirir."""
tr_map = str.maketrans({
'c': 'c',
'C': 'c',
'g': 'g',
'G': 'g',
'i': 'i',
'I': 'i',
'o': 'o',
'O': 'o',
's': 's',
'S': 's',
'u': 'u',
'U': 'u',
'ç': 'c',
'Ç': 'c',
'ğ': 'g',
'Ğ': 'g',
'ı': 'i',
'İ': 'i',
'ö': 'o',
'Ö': 'o',
'ş': 's',
'Ş': 's',
'ü': 'u',
'Ü': 'u',
})
value = value.translate(tr_map).lower()
return re.sub(r'[^a-z0-9]+', '', value)
def build_unique_username(first_name, last_name, birth_date, used_usernames, _force_suffix=None):
"""
ad.soyadYY formatinda username uretir, cakisma olursa sonek ekler.
_force_suffix: verilirse bare base denenmez, bu sayidan itibaren baslar
(regeneration ping-pong onlemek icin kullanilir).
"""
first = normalize_for_username(first_name or '')
last = normalize_for_username(last_name or '')
yy = str(birth_date.year)[-2:]
base = f"{first}.{last}{yy}" if first and last else f"user{yy}"
if _force_suffix is not None:
# Yeniden uretim: bare base'i hic deneme, rastgele bir sonek ile basla
counter = _force_suffix
candidate = f"{base}{counter}"
else:
candidate = base
counter = 1
while candidate in used_usernames:
counter += 1
candidate = f"{base}{counter}"
used_usernames.add(candidate)
return candidate