-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstagram_fixed.py
More file actions
287 lines (232 loc) · 11.4 KB
/
instagram_fixed.py
File metadata and controls
287 lines (232 loc) · 11.4 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
286
287
#!/usr/bin/env python3
"""
Script con inicialización robusta de Instaloader
Soluciona el error 'NoneType' object has no attribute 'wait_before_query'
"""
import instaloader
import time
import random
import sys
import os
from datetime import datetime
class RobustInstagramDownloader:
def __init__(self):
self.loader = None
self.max_init_attempts = 5
def create_robust_loader(self, attempt=1):
"""Crea un loader con múltiples intentos de inicialización"""
print(f"🔧 Inicializando Instaloader (intento {attempt}/{self.max_init_attempts})...")
try:
# Configuración básica pero robusta
loader = instaloader.Instaloader(
dirname_pattern="{target}",
filename_pattern="{date_utc}_{shortcode}",
download_video_thumbnails=False,
download_geotags=False,
download_comments=False,
save_metadata=False,
compress_json=False,
download_pictures=True,
download_videos=True,
post_metadata_txt_pattern=""
)
# Verificar que el context se inicializó correctamente
if loader.context is None:
raise Exception("Context es None - reiniciando...")
# Verificar métodos críticos
if not hasattr(loader.context, 'get_json') or not hasattr(loader.context, 'username'):
raise Exception("Context no tiene métodos necesarios")
print(f"✅ Instaloader inicializado correctamente")
return loader
except Exception as e:
print(f"❌ Error inicializando (intento {attempt}): {e}")
if attempt < self.max_init_attempts:
wait_time = attempt * 2
print(f"⏳ Esperando {wait_time} segundos antes del siguiente intento...")
time.sleep(wait_time)
return self.create_robust_loader(attempt + 1)
else:
print(f"💔 No se pudo inicializar después de {self.max_init_attempts} intentos")
return None
def safe_profile_lookup(self, loader, profile_name, max_attempts=3):
"""Búsqueda de perfil con reintentos"""
for attempt in range(max_attempts):
try:
print(f"🔍 Buscando perfil @{profile_name} (intento {attempt + 1}/{max_attempts})...")
# Verificar que el loader y context están OK
if loader is None or loader.context is None:
raise Exception("Loader o context son None")
profile = instaloader.Profile.from_username(loader.context, profile_name)
if profile is None:
raise Exception("Profile es None")
print(f"✅ Perfil encontrado: {profile.full_name}")
return profile
except instaloader.exceptions.ProfileNotExistsException:
print(f"❌ El perfil @{profile_name} no existe")
return None
except Exception as e:
print(f"❌ Error buscando perfil (intento {attempt + 1}): {e}")
if attempt < max_attempts - 1:
# Reinicializar loader si es necesario
if "NoneType" in str(e) or loader is None or loader.context is None:
print("🔄 Reinicializando loader...")
loader = self.create_robust_loader()
if loader is None:
print("💔 No se puede reinicializar loader")
return None
wait_time = (attempt + 1) * 5
print(f"⏳ Esperando {wait_time} segundos...")
time.sleep(wait_time)
else:
print("💔 Agotados todos los intentos de búsqueda")
return None
return None
def download_profile_robust(self, profile_name):
"""Descarga robusta de perfil"""
print("🚀 DESCARGADOR ROBUSTO DE INSTAGRAM")
print("=" * 50)
print(f"🎯 Perfil objetivo: @{profile_name}")
print(f"⏰ Inicio: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 50)
# Inicializar loader
loader = self.create_robust_loader()
if loader is None:
print("💔 FALLO CRÍTICO: No se puede inicializar Instaloader")
print("\n🛠️ POSIBLES SOLUCIONES:")
print(" 1. pip install --upgrade instaloader")
print(" 2. pip uninstall instaloader && pip install instaloader")
print(" 3. Usar Python diferente (3.8, 3.9, 3.10)")
return False
# Buscar perfil
profile = self.safe_profile_lookup(loader, profile_name)
if profile is None:
return False
# Mostrar información del perfil
try:
print(f"📊 Información del perfil:")
print(f" Nombre completo: {profile.full_name}")
print(f" Nombre de usuario: {profile.username}")
print(f" Posts: {profile.mediacount}")
print(f" Seguidores: {profile.followers}")
print(f" Privado: {'Sí' if profile.is_private else 'No'}")
if profile.is_private:
print("🔒 PERFIL PRIVADO - Se requiere autenticación")
return False
except Exception as e:
print(f"⚠️ Error obteniendo info del perfil: {e}")
print("🔄 Continuando con la descarga...")
# Crear carpeta de descarga
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
download_folder = f"{profile_name}_{timestamp}"
try:
if not os.path.exists(download_folder):
os.makedirs(download_folder)
os.chdir(download_folder)
print(f"📁 Carpeta de descarga: {download_folder}")
except Exception as e:
print(f"❌ Error creando carpeta: {e}")
return False
# Descargar posts
return self.download_posts_safe(loader, profile)
def download_posts_safe(self, loader, profile):
"""Descarga segura de posts"""
print("\n📥 INICIANDO DESCARGA DE POSTS")
print("🐌 Modo ultra-lento activado")
print("-" * 30)
posts_downloaded = 0
posts_failed = 0
try:
# Obtener lista de posts
print("📋 Obteniendo lista de posts...")
posts = []
try:
for post in profile.get_posts():
posts.append(post)
if len(posts) >= 50: # Limitar a 50 posts por ahora
break
except Exception as e:
print(f"⚠️ Error obteniendo posts: {e}")
if len(posts) == 0:
return False
total_posts = len(posts)
print(f"📊 Posts a descargar: {total_posts}")
# Descargar cada post
for i, post in enumerate(posts):
try:
print(f"\n📥 Post {i+1}/{total_posts}")
print(f" Código: {post.shortcode}")
print(f" Fecha: {post.date}")
print(f" Tipo: {'Video' if post.is_video else 'Imagen'}")
# Intentar descarga con reintentos
success = self.download_single_post(loader, post, max_retries=2)
if success:
posts_downloaded += 1
print(f" ✅ Descargado exitosamente")
else:
posts_failed += 1
print(f" ❌ Falló la descarga")
# Estadísticas actuales
print(f"📊 Progreso: {posts_downloaded} exitosos, {posts_failed} fallidos")
# Pausa entre posts
if i < total_posts - 1:
wait_time = random.randint(15, 30)
print(f"⏳ Pausa: {wait_time}s")
time.sleep(wait_time)
except Exception as e:
posts_failed += 1
print(f"❌ Error en post {i+1}: {e}")
time.sleep(10)
continue
# Resultados finales
print(f"\n🎉 DESCARGA COMPLETADA")
print(f"📊 Estadísticas finales:")
print(f" ✅ Posts descargados: {posts_downloaded}")
print(f" ❌ Posts fallidos: {posts_failed}")
print(f" 📁 Ubicación: {os.getcwd()}")
return posts_downloaded > 0
except Exception as e:
print(f"💔 Error crítico en descarga: {e}")
return False
def download_single_post(self, loader, post, max_retries=2):
"""Descarga un solo post con reintentos"""
for attempt in range(max_retries):
try:
loader.download_post(post, target="")
return True
except Exception as e:
print(f" ⚠️ Intento {attempt + 1} falló: {e}")
if attempt < max_retries - 1:
time.sleep(5)
# Si es error de context, reinicializar
if "NoneType" in str(e) or "context" in str(e).lower():
print(" 🔄 Reinicializando loader...")
new_loader = self.create_robust_loader()
if new_loader:
loader = new_loader
return False
def main():
if len(sys.argv) != 2:
print("🛠️ INSTAGRAM DOWNLOADER - VERSIÓN ROBUSTA")
print("=" * 50)
print("✅ Soluciona errores de inicialización")
print("✅ Manejo robusto de errores")
print("✅ Reintentos automáticos")
print()
print("📋 Uso: python instagram_fixed.py <nombre_usuario>")
print("📋 Ejemplo: python instagram_fixed.py escolaia_2n")
print()
sys.exit(1)
profile_name = sys.argv[1]
downloader = RobustInstagramDownloader()
success = downloader.download_profile_robust(profile_name)
if success:
print("\n🎉 ¡DESCARGA EXITOSA!")
else:
print("\n💔 La descarga no se pudo completar")
print("\n🛠️ SI EL PROBLEMA PERSISTE:")
print(" 1. pip install --upgrade instaloader")
print(" 2. Reinicia tu computadora")
print(" 3. Usa una VPN")
print(" 4. Intenta con otro perfil público")
if __name__ == "__main__":
main()