Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
# When disabling ssl verify in requests (for proxies and firewalls),
# urllib3 prints an insecure warning on stdout. We disable that.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
prepared_requests = self._prepare_requests()
for idx, pr in enumerate(prepared_requests):
try:
with requests.Session() as s:
# Send request
r = s.send(request=pr,
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError as e: # pragma: no cover
# Request successful, bad response
log.debug(str(e))
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
log.debug(str(e))
raise gTTSError(tts=self)
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
# When disabling ssl verify in requests (for proxies and firewalls),
# urllib3 prints an insecure warning on stdout. We disable that.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
prepared_requests = self._prepare_requests()
for idx, pr in enumerate(prepared_requests):
try:
with requests.Session() as s:
# Send request
r = s.send(request=pr,
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError as e: # pragma: no cover
# Request successful, bad response
log.debug(str(e))
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
log.debug(str(e))
raise gTTSError(tts=self)
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError:
# Request successful, bad response
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
raise gTTSError(str(e))
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
fp.write(chunk)
log.debug("part-%i written to %s", idx, fp)
except (AttributeError, TypeError) as e:
raise TypeError(
"'fp' is not a file-like object or it does not take bytes: %s" %
str(e))
'tk': part_tk}
log.debug("payload-%i: %s", idx, payload)
try:
# Request
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=self.GOOGLE_TTS_HEADERS,
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError:
# Request successful, bad response
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
raise gTTSError(str(e))
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
fp.write(chunk)
log.debug("part-%i written to %s", idx, fp)
except (AttributeError, TypeError) as e:
log.debug(str(e), exc_info=True)
raise gTTSError(
"Connection error during token calculation: %s" %
str(e))
payload = {'ie': 'UTF-8',
'q': part,
'tl': self.lang,
'ttsspeed': self.speed,
'total': len(text_parts),
'idx': idx,
'client': 'tw-ob',
'textlen': _len(part),
'tk': part_tk}
log.debug("payload-%i: %s", idx, payload)
try:
# Request
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=self.GOOGLE_TTS_HEADERS,
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError:
'client': 'tw-ob',
'textlen': _len(part),
'tk': part_tk}
log.debug("payload-%i: %s", idx, payload)
try:
# Request
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=self.GOOGLE_TTS_HEADERS,
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError:
# Request successful, bad response
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
raise gTTSError(str(e))
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
fp.write(chunk)
r.raise_for_status()
except requests.exceptions.HTTPError as e: # pragma: no cover
# Request successful, bad response
log.debug(str(e))
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
log.debug(str(e))
raise gTTSError(tts=self)
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
fp.write(chunk)
log.debug("part-%i written to %s", idx, fp)
except (AttributeError, TypeError) as e:
raise TypeError(
"'fp' is not a file-like object or it does not take bytes: %s" %
str(e))
"""
# When disabling ssl verify in requests (for proxies and firewalls),
# urllib3 prints an insecure warning on stdout. We disable that.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
text_parts = self._tokenize(self.text)
log.debug("text_parts: %i", len(text_parts))
assert text_parts, 'No text to send to TTS API'
for idx, part in enumerate(text_parts):
try:
# Calculate token
part_tk = self.token.calculate_token(part)
except requests.exceptions.RequestException as e: # pragma: no cover
log.debug(str(e), exc_info=True)
raise gTTSError(
"Connection error during token calculation: %s" %
str(e))
payload = {'ie': 'UTF-8',
'q': part,
'tl': self.lang,
'ttsspeed': self.speed,
'total': len(text_parts),
'idx': idx,
'client': 'tw-ob',
'textlen': _len(part),
'tk': part_tk}
log.debug("payload-%i: %s", idx, payload)
# urllib3 prints an insecure warning on stdout. We disable that.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
prepared_requests = self._prepare_requests()
for idx, pr in enumerate(prepared_requests):
try:
with requests.Session() as s:
# Send request
r = s.send(request=pr,
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError as e: # pragma: no cover
# Request successful, bad response
log.debug(str(e))
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
log.debug(str(e))
raise gTTSError(tts=self)
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
fp.write(chunk)
# proxies=urllib.request.getproxies(),
proxies=proxies('tts_google'),
verify=False)
log.debug("headers-%i: %s", idx, r.request.headers)
log.debug("url-%i: %s", idx, r.request.url)
log.debug("status-%i: %s", idx, r.status_code)
r.raise_for_status()
except requests.exceptions.HTTPError as e: # pragma: no cover
# Request successful, bad response
log.debug(str(e))
raise gTTSError(tts=self, response=r)
except requests.exceptions.RequestException as e: # pragma: no cover
# Request failed
log.debug(str(e))
raise gTTSError(tts=self)
try:
# Write
# for chunk in r.iter_content(chunk_size=1024):
for chunk in r.iter_content(chunk_size=self._buff_size):
fp.write(chunk)
log.debug("part-%i written to %s", idx, fp)
except (AttributeError, TypeError) as e:
raise TypeError(
"'fp' is not a file-like object or it does not take bytes: %s" %
str(e))