Skip to content

Commit

Permalink
certifi missing on Windows fix
Browse files Browse the repository at this point in the history
  • Loading branch information
noam09 committed Sep 16, 2017
1 parent ce71dbb commit 01b09a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -46,7 +46,7 @@
__plugin_name__ = "Telegramer"
__author__ = "Noam"
__author_email__ = "noamgit@gmail.com"
__version__ = "1.1.4"
__version__ = "1.1.5"
__url__ = "https://github.com/noam09"
__license__ = "GPLv3"
__description__ = "Control Deluge using Telegram"
Expand Down
32 changes: 30 additions & 2 deletions telegramer/include/telegram/utils/request.py
Expand Up @@ -28,7 +28,10 @@
except ImportError:
import json

import certifi
try:
import certifi
except Exception as e:
log.error(str(e) + '\n' + traceback.format_exc())
try:
import telegram.vendor.ptb_urllib3.urllib3 as urllib3
import telegram.vendor.ptb_urllib3.urllib3.contrib.appengine as appengine
Expand Down Expand Up @@ -88,10 +91,35 @@ def __init__(self,

self._con_pool_size = con_pool_size


# This was performed on Windows only, but it shouldn't be a problem
# managing cacert.pem on Linux as well
#if os.name == 'nt':
try:
import urllib2
import tempfile
capath = os.path.join(tempfile.gettempdir(), 'tg-cacert.pem')
# Check if tg-cacert.pem exists and if it's older than 7 days
if not os.path.exists(capath) or (os.path.exists(capath) \
and (time.time() - os.path.getctime(capath)) // (24 * 3600) >= 7):
CACERT_URL = "https://curl.haxx.se/ca/cacert.pem"
request = urllib2.Request(CACERT_URL)
file_contents = urllib2.urlopen(request).read()
log.debug("## Telegramer downloaded "+os.path.realpath(capath))
cafile = open(os.path.realpath(capath), 'wb')
cafile.write(file_contents)
cafile.close()
except:
try:
capath = certifi.where()
except:
capath = os.path.join(tempfile.gettempdir(), 'tg-cacert.pem')


kwargs = dict(
maxsize=con_pool_size,
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
ca_certs=capath, #certifi.where(),
socket_options=sockopts,
timeout=urllib3.Timeout(
connect=self._connect_timeout, read=read_timeout, total=None))
Expand Down

0 comments on commit 01b09a8

Please sign in to comment.