Fix proxytranslate
This commit is contained in:
@@ -112,8 +112,8 @@ class CipherSuiteAdapter(host_header_ssl.HostHeaderSSLAdapter):
|
|||||||
request.url = urlparse.urlunparse(parse)
|
request.url = urlparse.urlunparse(parse)
|
||||||
try:
|
try:
|
||||||
ret = super(CipherSuiteAdapter, self).send(request, **kwargs)
|
ret = super(CipherSuiteAdapter, self).send(request, **kwargs)
|
||||||
if 400 <= ret.status_code < 500:
|
# if 400 <= ret.status_code < 500:
|
||||||
raise Exception
|
# raise Exception
|
||||||
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError, requests.exceptions.SSLError) as e:
|
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError, requests.exceptions.SSLError) as e:
|
||||||
logger.info('Request for ' + domain + ' with ip ' + ip + ' failed')
|
logger.info('Request for ' + domain + ' with ip ' + ip + ' failed')
|
||||||
logger.info(e)
|
logger.info(e)
|
||||||
|
|||||||
@@ -11,28 +11,26 @@ else:
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
try:
|
try:
|
||||||
from platformcode import logger
|
from platformcode import logger
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logger = None
|
logger = None
|
||||||
|
|
||||||
HEADERS = {
|
HEADERS = {'User-Agent': 'android',
|
||||||
'Host': 'translate.google.com',
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
|
||||||
'User-Agent': 'android'
|
"Accept-Language": "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Charset": "UTF-8",
|
||||||
}
|
"Accept-Encoding": "gzip"}
|
||||||
|
|
||||||
MAX_CONECTION_THREAD = 10
|
|
||||||
SL = 'en'
|
SL = 'en'
|
||||||
TL = 'it'
|
TL = 'it'
|
||||||
|
|
||||||
BASE_URL_PROXY = 'https://translate.googleusercontent.com'
|
BASE_URL_PROXY = 'https://translate.googleusercontent.com'
|
||||||
BASE_URL_TRANSLATE = 'https://translate.google.com/translate?hl=it&sl=' + SL + '&tl=' + TL + '&u=[TARGET_URL]&sandbox=0' # noqa: E501
|
BASE_URL_TRANSLATE = 'https://translate.google.com/translate?hl=it&sl=' + SL + '&tl=' + TL + '&u=[TARGET_URL]' # noqa: E501
|
||||||
|
|
||||||
|
|
||||||
def checker_url(html, url):
|
def checker_url(html, url):
|
||||||
grep_regex = re.findall(r'(?:href="|src="|value=")([^"]+)', html) # noqa: E501
|
grep_regex = re.findall(r'(?:href="|src="|value=")(https?://translate[^"]+)', html) # noqa: E501
|
||||||
for url_result_regex in grep_regex:
|
for url_result_regex in grep_regex:
|
||||||
if url in url_result_regex:
|
if url in url_result_regex:
|
||||||
return url_result_regex.replace('&', '&')
|
return url_result_regex.replace('&', '&')
|
||||||
@@ -44,6 +42,8 @@ def process_request_proxy(url):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
domain = urlparse.urlparse(url).netloc
|
domain = urlparse.urlparse(url).netloc
|
||||||
|
session = requests.Session()
|
||||||
|
session.headers.update(HEADERS)
|
||||||
|
|
||||||
target_url = \
|
target_url = \
|
||||||
BASE_URL_TRANSLATE.replace('[TARGET_URL]', request.quote(url))
|
BASE_URL_TRANSLATE.replace('[TARGET_URL]', request.quote(url))
|
||||||
@@ -53,7 +53,7 @@ def process_request_proxy(url):
|
|||||||
else:
|
else:
|
||||||
print(target_url)
|
print(target_url)
|
||||||
|
|
||||||
return_html = requests.get(target_url, timeout=20, headers=HEADERS)
|
return_html = session.get(target_url, timeout=20)
|
||||||
|
|
||||||
if not return_html:
|
if not return_html:
|
||||||
return
|
return
|
||||||
@@ -68,10 +68,9 @@ def process_request_proxy(url):
|
|||||||
else:
|
else:
|
||||||
print(url_request)
|
print(url_request)
|
||||||
|
|
||||||
request_final = requests.get(
|
request_final = session.get(
|
||||||
url_request,
|
url_request,
|
||||||
timeout=20,
|
timeout=20
|
||||||
headers={'User-Agent': 'android'}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
url_request_proxy = checker_url(
|
url_request_proxy = checker_url(
|
||||||
@@ -86,10 +85,9 @@ def process_request_proxy(url):
|
|||||||
result = None
|
result = None
|
||||||
while not data or 'Sto traducendo' in data:
|
while not data or 'Sto traducendo' in data:
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
result = requests.get(
|
result = session.get(
|
||||||
url_request_proxy,
|
url_request_proxy,
|
||||||
timeout=20
|
timeout=20
|
||||||
# headers={'User-Agent': 'android'}
|
|
||||||
)
|
)
|
||||||
data = result.content.decode('utf-8', 'ignore')
|
data = result.content.decode('utf-8', 'ignore')
|
||||||
if not PY3:
|
if not PY3:
|
||||||
|
|||||||
Reference in New Issue
Block a user