-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_client.py
More file actions
285 lines (244 loc) · 13.1 KB
/
Copy pathtelegram_client.py
File metadata and controls
285 lines (244 loc) · 13.1 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Módulo de conexión a Telegram usando Telethon
Maneja la conexión, autenticación y operaciones básicas
"""
import asyncio
from typing import List, Dict, Any, Optional
from telethon import TelegramClient
from telethon.errors import FloodWaitError, RPCError
from telethon.tl.types import InputPeerChannel, InputPeerChat
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
console = Console()
class TelegramManager:
"""Clase para manejar la conexión y operaciones con Telegram"""
def __init__(self, api_id: int, api_hash: str, phone: str, session_name: str = "telegram_session"):
self.api_id = api_id
self.api_hash = api_hash
self.phone = phone
self.session_name = session_name
self.client: Optional[TelegramClient] = None
self.is_connected = False
async def connect(self) -> bool:
"""Establece conexión con Telegram"""
try:
console.print("[bold blue]🔐 Conectando a Telegram...[/bold blue]")
# Crear cliente con sesión persistente
self.client = TelegramClient(self.session_name, self.api_id, self.api_hash)
# Iniciar conexión
await self.client.start(phone=self.phone)
self.is_connected = True
# Verificar si estamos autenticados
if await self.client.is_user_authorized():
me = await self.client.get_me()
console.print(Panel(
f"[green]✅ Conexión exitosa[/green]\n"
f"Usuario: {me.first_name} {me.last_name or ''}\n"
f"ID: {me.id}",
title="Telegram Conectado",
border_style="green"
))
return True
else:
console.print("[red]❌ Error de autenticación[/red]")
return False
except FloodWaitError as e:
console.print(f"[red]⏰ Límite de Telegram: espera {e.seconds} segundos[/red]")
return False
except RPCError as e:
console.print(f"[red]❌ Error de RPC: {e}[/red]")
return False
except Exception as e:
console.print(f"[red]❌ Error de conexión: {e}[/red]")
return False
async def get_recent_dialogs(self, limit: int = 50) -> List[Dict[str, Any]]:
"""Obtiene los diálogos recientes (canales, grupos y bots)"""
if not self.is_connected or not self.client:
raise RuntimeError("No hay conexión activa con Telegram")
try:
dialogs = []
async for dialog in self.client.iter_dialogs(limit=limit):
entity = dialog.entity
# Filtrar canales, grupos y chats con bots
is_bot_chat = False
if dialog.is_user and hasattr(entity, 'bot') and entity.bot:
is_bot_chat = True
if dialog.is_channel or dialog.is_group or is_bot_chat:
# Obtener información adicional
participants_count = getattr(entity, 'participants_count', 'N/A')
# Determinar el tipo
if is_bot_chat:
dialog_type = 'Bot'
elif dialog.is_channel:
dialog_type = 'Canal'
else:
dialog_type = 'Grupo'
dialogs.append({
'id': dialog.id,
'title': dialog.title or getattr(entity, 'first_name', 'Sin nombre'),
'type': dialog_type,
'participants': participants_count,
'entity': entity
})
return dialogs
except FloodWaitError as e:
console.print(f"[red]⏰ Límite de Telegram: espera {e.seconds} segundos[/red]")
return []
except Exception as e:
console.print(f"[red]❌ Error obteniendo diálogos: {e}[/red]")
return []
async def search_media(self, entities: List[Any], keyword: str, limit: int = 20, offset: int = 0) -> Dict[str, Any]:
"""Busca videos y audios en las entidades especificadas con paginación"""
if not self.is_connected or not self.client:
raise RuntimeError("No hay conexión activa con Telegram")
media_files = []
total_found = 0
skipped_count = 0
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console
) as progress:
task = progress.add_task(f"🔍 Buscando media con '{keyword}'...", total=len(entities))
for entity in entities:
try:
# Buscar mensajes en la entidad sin límite para buscar todo el historial
entity_media = []
async for message in self.client.iter_messages(
entity=entity,
search=keyword,
limit=None, # Sin límite para buscar todo el historial
filter=None
):
media_info = None
media_type = None
# Verificar si el mensaje contiene video
if message.video and message.media:
media_info = message.video
media_type = 'video'
# Verificar si el mensaje contiene audio
elif message.audio and message.media:
media_info = message.audio
media_type = 'audio'
# Verificar si es mensaje de voz
elif message.voice and message.media:
media_info = message.voice
media_type = 'voice'
if media_info and media_type:
# Extraer atributos del media con más métodos
duration = getattr(media_info, 'duration', 0)
file_size = getattr(media_info, 'file_size', 0)
width = getattr(media_info, 'width', 0)
height = getattr(media_info, 'height', 0)
mime_type = getattr(media_info, 'mime_type', '')
# Intentar obtener tamaño de otras formas
if file_size == 0 and hasattr(media_info, 'size'):
file_size = getattr(media_info, 'size', 0)
# Criterios para considerar un archivo válido
is_valid_media = False
if media_type == 'video':
is_valid_media = (
(duration > 0) or
(file_size > 0) or
(width > 0 and height > 0) or
(mime_type and 'video' in mime_type.lower())
)
elif media_type in ('audio', 'voice'):
is_valid_media = (
(duration > 0) or
(file_size > 0) or
(mime_type and ('audio' in mime_type.lower() or 'ogg' in mime_type.lower()))
)
if is_valid_media:
# Extraer título para archivos de audio
title = None
performer = None
if media_type == 'audio' and hasattr(media_info, 'attributes'):
for attr in media_info.attributes:
if hasattr(attr, 'title') and attr.title:
title = attr.title
if hasattr(attr, 'performer') and attr.performer:
performer = attr.performer
# Construir título completo para audio (artista - título)
if media_type == 'audio' and (title or performer):
if performer and title:
audio_title = f"{performer} - {title}"
elif title:
audio_title = title
else:
audio_title = performer
else:
audio_title = None
media_data = {
'id': message.id,
'date': message.date.strftime('%Y-%m-%d %H:%M'),
'channel_title': getattr(entity, 'title', getattr(entity, 'first_name', 'Desconocido')),
'message': message.message or 'Sin descripción',
'title': audio_title, # Título del archivo (para audio)
'duration': duration,
'file_size': file_size,
'width': width,
'height': height,
'mime_type': mime_type,
'media_type': media_type,
'message_obj': message,
'entity': entity
}
entity_media.append(media_data)
else:
skipped_count += 1
total_found += len(entity_media)
media_files.extend(entity_media)
# Mostrar información de depuración
if skipped_count > 0:
console.print(f"[dim]ℹ️ {getattr(entity, 'title', getattr(entity, 'first_name', 'entidad'))}: {len(entity_media)} archivos válidos, {skipped_count} omitidos[/dim]")
except FloodWaitError as e:
console.print(f"[yellow]⏰ Esperando {e.seconds} segundos por límite de API...[/yellow]")
await asyncio.sleep(e.seconds)
except Exception as e:
console.print(f"[red]❌ Error buscando en {getattr(entity, 'title', 'entidad')}: {e}[/red]")
progress.advance(task)
# Ordenar resultados por fecha (más recientes primero)
media_files.sort(key=lambda x: x['date'], reverse=True)
# Aplicar paginación
page_size = 50
start_idx = offset * page_size
end_idx = start_idx + page_size
paginated_media = media_files[start_idx:end_idx]
has_more = end_idx < len(media_files)
# Mostrar resumen de búsqueda
if skipped_count > 0:
console.print(f"[yellow]ℹ️ Se omitieron {skipped_count} mensajes sin media válido[/yellow]")
return {
'media': paginated_media,
'total_found': len(media_files),
'current_page': offset + 1,
'total_pages': (len(media_files) + page_size - 1) // page_size,
'has_more': has_more,
'page_size': page_size
}
async def download_media(self, message: Any, entity: Any, file_path: str, progress_callback=None) -> bool:
"""Descarga un archivo de media específico (video, audio, etc.)"""
if not self.is_connected or not self.client:
raise RuntimeError("No hay conexión activa con Telegram")
try:
# Descargar el archivo con progreso
await self.client.download_media(
message=message,
file=file_path,
progress_callback=progress_callback
)
return True
except FloodWaitError as e:
console.print(f"[red]⏰ Límite de descarga: espera {e.seconds} segundos[/red]")
return False
except Exception as e:
console.print(f"[red]❌ Error descargando archivo: {e}[/red]")
return False
async def disconnect(self):
"""Cierra la conexión con Telegram"""
if self.client and self.is_connected:
await self.client.disconnect()
self.is_connected = False
console.print("[yellow]🔌 Conexión cerrada[/yellow]")