From b365d8cb9e768878223673c25632a86dfa6edca7 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Mon, 26 Sep 2016 09:52:53 +0100 Subject: [PATCH] Import open_url from util. --- bot.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 3f93971..e144406 100755 --- a/bot.py +++ b/bot.py @@ -9,7 +9,12 @@ import logging import pexpect import random import re -import util +import sys + +if sys.version_info >= (3, 0): + import urllib.request +else: + import urllib2 admins = [] admin_role = 'Cat' @@ -31,6 +36,28 @@ feeds = [ { 'channel': 227710451226509312, 'url': 'https://miggy.org/games/elite-dangerous/devtracker/ed-dev-posts.rss', 'summary': True } ] +def open_url(url): + if sys.version_info >= (3, 0): + # Specify our own user agent as Cloudflare doesn't seem to like the urllib one + request = urllib.request.Request(url, headers={'User-Agent': USER_AGENT}) + try: + return urllib.request.urlopen(request) + except urllib.error.HTTPError as err: + log.error("Error {0} opening {1}: {2}".format(err.code, url, err.reason)) + return None + else: + sslctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) + # If we're on OSX with OpenSSL 0.9.x, manually specify preferred ciphers so CloudFlare can negotiate successfully + if platform.system() == 'Darwin' and ssl.OPENSSL_VERSION_INFO[0] < 1: + sslctx.set_ciphers("ECCdraft:HIGH:!aNULL") + # Specify our own user agent as Cloudflare doesn't seem to like the urllib one + request = urllib2.Request(url, headers={'User-Agent': USER_AGENT}) + try: + return urllib2.urlopen(request, context=sslctx) + except urllib2.HTTPError as err: + log.error("Error {0} opening {1}: {2}".format(err.code, url, err.reason)) + return None + def wait_for_prompt(p): p.expect('EDI> ') -- 2.7.4