-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy_http_scraper.py
More file actions
110 lines (97 loc) · 3.72 KB
/
proxy_http_scraper.py
File metadata and controls
110 lines (97 loc) · 3.72 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
import requests
import random
import os
import sys
import json
import time
import logging
try:
from kodi_six import xbmc, xbmcplugin, xbmcgui, xbmcaddon, xbmcvfs
except ImportError:
import xbmc
import xbmcplugin
import xbmcgui
import xbmcaddon
import xbmcvfs
from dns import customdns
PY2 = sys.version_info[0] == 2
ADDON_ = xbmcaddon.Addon()
TRANSLATE_ = xbmc.translatePath if PY2 else xbmcvfs.translatePath
profile = TRANSLATE_(ADDON_.getAddonInfo('profile'))
if not os.path.exists(profile):
os.makedirs(profile)
CACHE_FILE = os.path.join(profile, 'proxy_cache.json')
logging.basicConfig(level=logging.DEBUG)
customdns(cache_ttl=14400) # Ativa DNS customizado com cache de 4 horas
# URL para pegar a lista de proxies
BASE_PROXIES_URL = 'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt'
class ProxyScraper:
def __init__(self, cache_file=CACHE_FILE, cache_ttl=14400):
self.cache_file = cache_file
self.cache_ttl = cache_ttl
self.cache = self._load_cache()
def _load_cache(self):
"""Carrega o cache do arquivo JSON, removendo entradas expiradas."""
try:
if os.path.exists(self.cache_file):
with open(self.cache_file, 'r') as f:
cache = json.load(f)
current_time = time.time()
# Mantém apenas entradas não expiradas
valid_cache = {
key: data for key, data in cache.items()
if data['expires'] > current_time
}
return valid_cache
return {}
except Exception as e:
logging.error(f"Erro ao carregar cache: {e}")
return {}
def _save_cache(self):
"""Salva o cache no arquivo JSON."""
try:
with open(self.cache_file, 'w') as f:
json.dump(self.cache, f, indent=2)
except Exception as e:
logging.error(f"Erro ao salvar cache: {e}")
def _fetch_new_proxy(self):
"""Busca um proxy novo da lista online."""
try:
response = requests.get(BASE_PROXIES_URL, headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
}, timeout=10)
if response.status_code == 200:
proxies = response.text.splitlines()
if proxies:
selected = random.choice(proxies)
proxy_auth = f"http://{selected}"
return proxy_auth
logging.warning("Não foi possível obter proxies da lista online.")
except Exception as e:
logging.error(f"Erro ao buscar proxies: {e}")
return None
def get_proxy(self, key="default"):
"""Retorna um proxy do cache ou pega um novo se expirado."""
current_time = time.time()
# Verifica cache
if key in self.cache:
cached = self.cache[key]
if cached['expires'] > current_time:
logging.info(f"Usando proxy do cache: {cached['proxy']}")
return cached['proxy']
else:
logging.info(f"Proxy expirado, removendo do cache: {cached['proxy']}")
del self.cache[key]
self._save_cache()
# Busca novo proxy
new_proxy = self._fetch_new_proxy()
if new_proxy:
self.cache[key] = {
'proxy': new_proxy,
'expires': current_time + self.cache_ttl
}
self._save_cache()
logging.info(f"Novo proxy salvo no cache: {new_proxy}")
return new_proxy
logging.warning("Nenhum proxy disponível.")
return None