-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.py
More file actions
72 lines (56 loc) · 2.11 KB
/
Copy pathstorage.py
File metadata and controls
72 lines (56 loc) · 2.11 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
"""
YKS Sonuç Bildirim Botu — Depolama (Storage)
==============================================
Kullanıcı ID'lerini ve Telegram update ID'lerini dosyada saklar.
"""
import json
import logging
import config
logger = logging.getLogger(__name__)
def load_users() -> list[int]:
"""Kayıtlı kullanıcıların chat_id listesini döndürür."""
if not config.USERS_STORAGE_FILE.exists():
return []
try:
content = config.USERS_STORAGE_FILE.read_text(encoding="utf-8")
if not content.strip():
return []
users = json.loads(content)
return users if isinstance(users, list) else []
except (json.JSONDecodeError, OSError) as e:
logger.error("Kullanıcılar yüklenirken hata oluştu: %s", e)
return []
def save_users(users: list[int]) -> None:
"""Kullanıcıların chat_id listesini JSON olarak kaydeder."""
unique_users = sorted(set(users))
try:
config.USERS_STORAGE_FILE.write_text(
json.dumps(unique_users, indent=2), encoding="utf-8"
)
except OSError as e:
logger.error("Kullanıcılar kaydedilirken hata oluştu: %s", e)
def add_user(chat_id: int) -> bool:
"""Yeni bir kullanıcı ekler. Eklenirse True, zaten varsa False döner."""
users = load_users()
if chat_id not in users:
users.append(chat_id)
save_users(users)
return True
return False
def get_user_count() -> int:
"""Toplam kayıtlı kullanıcı sayısını döndürür."""
return len(load_users())
def load_last_update_id() -> int:
"""Son işlenen Telegram update_id değerini döndürür."""
if not config.LAST_UPDATE_ID_FILE.exists():
return 0
try:
return int(config.LAST_UPDATE_ID_FILE.read_text(encoding="utf-8").strip())
except (ValueError, OSError):
return 0
def save_last_update_id(update_id: int) -> None:
"""Son işlenen Telegram update_id değerini kaydeder."""
try:
config.LAST_UPDATE_ID_FILE.write_text(str(update_id), encoding="utf-8")
except OSError as e:
logger.error("update_id kaydedilirken hata oluştu: %s", e)