-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
533 lines (445 loc) · 21.3 KB
/
Copy pathprocessor.py
File metadata and controls
533 lines (445 loc) · 21.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
"""
Processor with Webshare Proxy + Undetected ChromeDriver
Bypasses Locoloader bot detection
"""
import re
import logging
import os
import time
import random
import glob
from typing import Optional, List
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import undetected_chromedriver as uc
logger = logging.getLogger(__name__)
class ProxyManager:
"""Webshare Proxy Manager"""
def __init__(self):
self.proxies = []
self.current_index = 0
self.failed_proxies = set()
self._load_proxies_from_file()
def _load_proxies_from_file(self):
"""Load proxies from Webshare_10_proxies.txt"""
try:
if os.path.exists('Webshare_10_proxies.txt'):
with open('Webshare_10_proxies.txt', 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
parts = line.split(':')
if len(parts) == 4:
ip, port, username, password = parts
self.proxies.append({
'ip': ip,
'port': port,
'username': username,
'password': password,
'full': f"{ip}:{port}"
})
elif len(parts) == 2:
self.proxies.append({
'ip': parts[0],
'port': parts[1],
'username': None,
'password': None,
'full': f"{parts[0]}:{parts[1]}"
})
if self.proxies:
logger.info(f"✅ Loaded {len(self.proxies)} proxies from Webshare_10_proxies.txt")
except Exception as e:
logger.warning(f"Failed to load proxies: {e}")
def get_next_proxy(self) -> Optional[dict]:
"""Get next working proxy"""
if not self.proxies:
logger.error("❌ No proxies available!")
return None
available = [p for p in self.proxies if p['full'] not in self.failed_proxies]
if not available:
logger.warning("⚠️ All proxies failed! Resetting...")
self.failed_proxies.clear()
available = self.proxies
if self.current_index >= len(available):
self.current_index = 0
proxy = available[self.current_index]
self.current_index = (self.current_index + 1) % len(available)
return proxy
def mark_failed(self, proxy: dict):
"""Mark proxy as failed"""
if proxy and proxy.get('full'):
self.failed_proxies.add(proxy['full'])
logger.warning(f"❌ Proxy marked failed: {proxy['ip']}:{proxy['port']}")
def get_proxy_count(self) -> int:
available = [p for p in self.proxies if p['full'] not in self.failed_proxies]
return len(available)
class LocoloaderProcessor:
"""Processor with Undetected ChromeDriver + Webshare Proxy"""
def __init__(self, extension_path: str = None):
self.extension_path = extension_path or self._find_extension()
self.driver = None
# Rate limit tracking
self.requests_made = 0
self.max_requests = 2
self.limit_hit = False
# Proxy manager
self.proxy_manager = ProxyManager()
self.current_proxy = None
# Proxy auth extension path
self.auth_ext_path = os.path.join(os.getcwd(), 'proxy_auth_extension')
if self.extension_path:
logger.info(f"📁 Locoloader Extension: {self.extension_path}")
proxy_count = self.proxy_manager.get_proxy_count()
logger.info(f"🌐 Available proxies: {proxy_count}")
def _find_extension(self) -> Optional[str]:
"""Find Locoloader extension"""
possible_paths = [
'./locoloader_extension',
'./extension',
'./locoloader_extension/chrome-extension-main',
'C:/Users/heats/OneDrive/Desktop/TEST/locoloader_extension/chrome-extension-main',
'C:/Users/heats/OneDrive/Desktop/TEST/locoloader_extension',
]
for path in possible_paths:
if os.path.exists(path) and os.path.isdir(path):
manifest = os.path.join(path, 'manifest.json')
if os.path.exists(manifest):
return path
found = glob.glob(os.path.join(path, "**", "manifest.json"), recursive=True)
if found:
return os.path.dirname(found[0])
return None
def _init_driver(self) -> bool:
"""Initialize Undetected ChromeDriver with proxy"""
try:
if not self.extension_path or not os.path.exists(self.extension_path):
logger.error("❌ Locoloader extension not found!")
return False
# Get proxy
self.current_proxy = self.proxy_manager.get_next_proxy()
if not self.current_proxy:
logger.error("❌ No proxy available!")
return False
ip = self.current_proxy.get('ip', '')
port = self.current_proxy.get('port', '')
username = self.current_proxy.get('username', '')
password = self.current_proxy.get('password', '')
# Create Chrome options
options = uc.ChromeOptions()
# Load Locoloader extension
options.add_argument(f'--load-extension={self.extension_path}')
# Load Proxy Auth extension if it exists
if os.path.exists(self.auth_ext_path):
options.add_argument(f'--load-extension={self.auth_ext_path}')
logger.info("🔐 Proxy Auth extension loaded")
# Proxy settings
proxy_string = f"{ip}:{port}"
options.add_argument(f'--proxy-server={proxy_string}')
logger.info(f"🌐 Using proxy: {proxy_string}")
if username and password:
logger.info(f"🔐 Proxy auth credentials will be auto-filled")
# Stealth settings to avoid detection
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-web-security')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1920,1080')
options.add_argument('--disable-features=IsolateOrigins,site-per-process')
# User agent
user_agents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
]
options.add_argument(f'--user-agent={random.choice(user_agents)}')
# Create undetected driver
logger.info("🔄 Initializing Undetected ChromeDriver...")
self.driver = uc.Chrome(
options=options,
version_main=147, # Match your Chrome version
use_subprocess=True,
)
# Set proxy auth credentials in the extension
if username and password:
try:
# Wait for driver to be ready
time.sleep(3)
# Send credentials to the auth extension
script = f'''
chrome.runtime.sendMessage({{
type: 'setProxyAuth',
username: '{username}',
password: '{password}'
}}, function(response) {{
console.log('✅ Proxy auth credentials set:', response);
}});
'''
self.driver.execute_script(script)
logger.info(f"✅ Proxy auth credentials auto-filled")
time.sleep(2)
except Exception as e:
logger.warning(f"Failed to set proxy auth: {e}")
logger.info("✅ Undetected browser initialized successfully")
return True
except Exception as e:
logger.error(f"Browser init failed: {e}")
if self.current_proxy:
self.proxy_manager.mark_failed(self.current_proxy)
return False
def _is_video_url(self, url: str) -> bool:
"""Check if URL is MP4 only (no M3U8)"""
if not url:
return False
url = url.strip()
if 'locoloader.com' in url or 'exceeded' in url:
return False
if '.m3u8' in url.lower():
return False
video_extensions = ('.mp4', '.webm', '.mkv', '.avi', '.mov')
if any(url.lower().endswith(ext) for ext in video_extensions):
return True
patterns = ['/video/', '/videos/', '/stream/', '/media/', '/cdn/', '/download/']
if any(pattern in url.lower() for pattern in patterns):
if '.m3u8' not in url.lower():
return True
return False
def _extract_from_play_button(self) -> Optional[str]:
"""Extract MP4 URL from Play button"""
try:
logger.info("🔍 Looking for Play button...")
play_buttons = self.driver.find_elements(By.CSS_SELECTOR, 'a.bt.pr, .bt.pr, [data-bt-type="bt-preview"]')
for btn in play_buttons:
try:
href = btn.get_attribute('href')
data_url = btn.get_attribute('data-url') or btn.get_attribute('data-link-url')
data_file = btn.get_attribute('data-file') or btn.get_attribute('data-file-url')
data_link = btn.get_attribute('data-link-1') or btn.get_attribute('data-link')
for url in [href, data_url, data_file, data_link]:
if url and url != 'exceeded' and self._is_video_url(url):
logger.info(f"✅ Found MP4 from Play button")
return url
# Click to open in new tab
logger.info("🔄 Clicking Play button...")
original_window = self.driver.current_window_handle
ActionChains(self.driver).key_down('ctrl').click(btn).key_up('ctrl').perform()
time.sleep(3)
for handle in self.driver.window_handles:
if handle != original_window:
self.driver.switch_to.window(handle)
break
current_url = self.driver.current_url
if self._is_video_url(current_url):
logger.info(f"✅ Found MP4 from new tab")
return current_url
from bs4 import BeautifulSoup
soup = BeautifulSoup(self.driver.page_source, 'html.parser')
for video in soup.find_all('video'):
src = video.get('src')
if src and self._is_video_url(src):
return src
for source in video.find_all('source'):
src = source.get('src')
if src and self._is_video_url(src):
return src
self.driver.close()
self.driver.switch_to.window(original_window)
except Exception as e:
continue
return None
except Exception as e:
logger.warning(f"Play button extraction failed: {e}")
return None
def _get_video_from_quality_options(self) -> Optional[str]:
"""Extract MP4 from quality options (720p/1080p)"""
try:
logger.info("🔍 Looking for quality options...")
quality_selectors = [
'a:has-text("720p")',
'a:has-text("1080p")',
'button:has-text("720p")',
'button:has-text("1080p")',
'.link-wrapper a',
]
for selector in quality_selectors:
try:
elements = self.driver.find_elements(By.CSS_SELECTOR, selector)
for el in elements:
href = el.get_attribute('href')
text = el.text
if ('720' in text or '1080' in text or 'HD' in text):
if href and self._is_video_url(href):
logger.info(f"✅ Found {text} MP4")
return href
data_url = el.get_attribute('data-url') or el.get_attribute('data-link-url')
if data_url and self._is_video_url(data_url):
logger.info(f"✅ Found {text} MP4")
return data_url
except:
continue
return None
except Exception as e:
logger.warning(f"Quality options extraction failed: {e}")
return None
def _extract_from_page_source(self, html: str) -> List[str]:
"""Extract MP4 URLs from page source"""
urls = []
try:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
for video in soup.find_all('video'):
src = video.get('src')
if src and self._is_video_url(src):
urls.append(src)
for source in video.find_all('source'):
src = source.get('src')
if src and self._is_video_url(src):
urls.append(src)
for source in soup.find_all('source'):
src = source.get('src')
if src and self._is_video_url(src):
urls.append(src)
for link in soup.find_all('a', href=True):
href = link.get('href')
if href and self._is_video_url(href):
urls.append(href)
for script in soup.find_all('script'):
if script.string:
content = script.string
patterns = [
r'https?://[^\s"\'<>]+\.(?:mp4|webm)',
r'video_url["\']\s*[:=]\s*["\']([^"\']+)',
r'src["\']\s*[:=]\s*["\']([^"\']+)',
r'url["\']\s*[:=]\s*["\']([^"\']+)',
r'file["\']\s*[:=]\s*["\']([^"\']+)',
]
for pattern in patterns:
matches = re.findall(pattern, content, re.IGNORECASE)
for match in matches:
if isinstance(match, tuple):
match = match[0]
if match and self._is_video_url(match):
urls.append(match)
except:
pass
return urls
def process_url(self, video_url: str) -> Optional[str]:
"""Process URL with Undetected ChromeDriver + Webshare proxy"""
logger.info(f"🎯 Processing: {video_url[:60]}...")
if self.limit_hit:
logger.info("🔄 Proxy hit limit! Getting new proxy...")
if self.current_proxy:
self.proxy_manager.mark_failed(self.current_proxy)
self.limit_hit = False
self.requests_made = 0
if self.driver:
try:
self.driver.quit()
except:
pass
self.driver = None
time.sleep(3)
max_attempts = 10
attempt = 0
while attempt < max_attempts:
try:
if not self.driver:
if not self._init_driver():
time.sleep(3)
continue
self.driver.get('https://www.locoloader.com/')
time.sleep(5)
# Check if page loaded
try:
self.driver.find_element(By.ID, 'inputUrl')
except:
logger.warning("Page didn't load properly, refreshing...")
self.driver.refresh()
time.sleep(5)
input_field = self.driver.find_element(By.ID, 'inputUrl')
input_field.clear()
for char in video_url:
input_field.send_keys(char)
time.sleep(0.01)
self.driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]').click()
time.sleep(10 + random.random() * 4)
page_source = self.driver.page_source
if 'exceeded' in page_source.lower() or 'limit' in page_source.lower():
logger.warning("⚠️ Proxy limit hit! Getting new proxy...")
self.limit_hit = True
if self.current_proxy:
self.proxy_manager.mark_failed(self.current_proxy)
if self.driver:
try:
self.driver.quit()
except:
pass
self.driver = None
attempt += 1
time.sleep(5)
continue
# Method 1: Play button
play_url = self._extract_from_play_button()
if play_url:
self.requests_made += 1
logger.info(f"✅ Successfully extracted MP4!")
return play_url
# Method 2: Quality options
quality_url = self._get_video_from_quality_options()
if quality_url:
self.requests_made += 1
logger.info(f"✅ Successfully extracted MP4!")
return quality_url
# Method 3: Page source
urls = self._extract_from_page_source(page_source)
mp4_urls = [u for u in urls if '.mp4' in u.lower()]
if mp4_urls:
for quality in ['1080', '720']:
for url in mp4_urls:
if quality in url.lower():
self.requests_made += 1
logger.info(f"✅ Successfully extracted MP4!")
return url
self.requests_made += 1
logger.info(f"✅ Successfully extracted MP4!")
return mp4_urls[0]
# Method 4: Download button
try:
download_btns = self.driver.find_elements(By.CSS_SELECTOR, 'a.bt.dl')
for btn in download_btns[:3]:
href = btn.get_attribute('href')
if href and href != 'exceeded' and self._is_video_url(href):
self.requests_made += 1
logger.info(f"✅ Successfully extracted MP4!")
return href
except:
pass
logger.warning(f"⚠️ No video found on attempt {attempt + 1}")
attempt += 1
if attempt < max_attempts:
time.sleep(3)
try:
self.driver.refresh()
time.sleep(3)
except:
pass
except Exception as e:
logger.error(f"❌ Error: {e}")
attempt += 1
if self.current_proxy:
self.proxy_manager.mark_failed(self.current_proxy)
if self.driver:
try:
self.driver.quit()
except:
pass
self.driver = None
time.sleep(5)
logger.error("❌ All attempts failed!")
return None
def close(self):
if self.driver:
try:
self.driver.quit()
except:
pass
self.driver = None