-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
145 lines (107 loc) · 2.96 KB
/
utils.py
File metadata and controls
145 lines (107 loc) · 2.96 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# utils.py
"""
Shared utility functions for both Telegram bot and Desktop GUI.
Provides helper functions for date parsing, message formatting, and more.
"""
from datetime import datetime, timezone
def parse_date(date_str: str) -> datetime:
"""
Parse date string to datetime object (UTC).
Args:
date_str: Date in YYYY-MM-DD format
Returns:
datetime object with UTC timezone
"""
dt = datetime.strptime(date_str, "%Y-%m-%d")
return dt.replace(tzinfo=timezone.utc)
def format_date(dt: datetime) -> str:
"""
Format datetime to YYYY-MM-DD string.
Args:
dt: datetime object
Returns:
Formatted date string
"""
return dt.strftime("%Y-%m-%d")
def format_datetime(dt: datetime) -> str:
"""
Format datetime to full string with time.
Args:
dt: datetime object
Returns:
Formatted datetime string
"""
return dt.strftime("%Y-%m-%d %H:%M:%S")
def validate_date_format(date_str: str) -> bool:
"""
Check if string is valid YYYY-MM-DD format.
Args:
date_str: String to validate
Returns:
True if valid, False otherwise
"""
try:
datetime.strptime(date_str, "%Y-%m-%d")
return True
except ValueError:
return False
def generate_message_link(username: str, message_id: int) -> str:
"""
Generate a direct link to a Telegram message.
Args:
username: Chat username (without @)
message_id: Message ID
Returns:
Message link or empty string if no username
"""
if username:
return f"https://t.me/{username}/{message_id}"
return ""
def truncate_text(text: str, max_length: int = 100) -> str:
"""
Truncate text to specified length with ellipsis.
Args:
text: Text to truncate
max_length: Maximum length
Returns:
Truncated text
"""
if len(text) <= max_length:
return text
return text[: max_length - 3] + "..."
def is_bot_chat(entity) -> bool:
"""
Check if an entity is a bot.
Args:
entity: Telethon entity
Returns:
True if bot, False otherwise
"""
return getattr(entity, "bot", False)
def is_saved_messages(entity, me) -> bool:
"""
Check if entity is Saved Messages.
Args:
entity: Telethon entity
me: Current user entity
Returns:
True if Saved Messages, False otherwise
"""
return getattr(entity, "id", None) == getattr(me, "id", None)
def get_chat_title(dialog) -> str:
"""
Get display title for a chat/dialog.
Args:
dialog: Telethon dialog
Returns:
Chat title string
"""
entity = dialog.entity
if hasattr(entity, "title"):
return entity.title
if hasattr(entity, "first_name"):
name = entity.first_name or ""
if hasattr(entity, "last_name") and entity.last_name:
name += f" {entity.last_name}"
return name
return "Unknown Chat"