Import open_url from util.
authorCMDR furrycat <elite@furrycat.net>
Mon, 26 Sep 2016 08:52:53 +0000 (09:52 +0100)
committerCMDR furrycat <elite@furrycat.net>
Mon, 26 Sep 2016 08:52:53 +0000 (09:52 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 3f93971..e144406 100755 (executable)
--- 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> ')