Skip to content

Commit

Permalink
Fix getnikola/nikola-themes#49 -- https→http fallback
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jul 16, 2015
1 parent d9559f9 commit 63dcbc7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -33,6 +33,8 @@ Features
Bugfixes
--------

* Use ``http`` as fallback in plugin/theme installers if a SSL error occurs
(Issue getnikola/nikola-themes#49)
* Add missing ``xmlns:xhtml`` namespace to sitemaps (Issue #1890)
* Fixed superfluous rebuild problems with Python 3. Note that this will cause
rebuilds for most sites. (Issue #1887)
Expand Down
24 changes: 19 additions & 5 deletions nikola/plugins/command/install_theme.py
Expand Up @@ -27,7 +27,7 @@
from __future__ import print_function
import os
import io
import json
import time
import requests

import pygments
Expand Down Expand Up @@ -95,8 +95,13 @@ def _execute(self, options, args):
if name is None and not listing:
LOGGER.error("This command needs either a theme name or the -l option.")
return False
data = requests.get(url).text
data = json.loads(data)
try:
data = requests.get(url).json()
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('http', 'https', 1)
data = requests.get(url).json()
if listing:
print("Themes:")
print("-------")
Expand Down Expand Up @@ -124,9 +129,18 @@ def _execute(self, options, args):
def do_install(self, name, data):
if name in data:
utils.makedirs(self.output_dir)
LOGGER.info("Downloading '{0}'".format(data[name]))
url = data[name]
LOGGER.info("Downloading '{0}'".format(url))
try:
zip_data = requests.get(url).content
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('http', 'https', 1)
zip_data = requests.get(url).content

zip_file = io.BytesIO()
zip_file.write(requests.get(data[name]).content)
zip_file.write(zip_data)
LOGGER.info("Extracting '{0}' into themes/".format(name))
utils.extract_all(zip_file)
dest_path = os.path.join(self.output_dir, name)
Expand Down
22 changes: 19 additions & 3 deletions nikola/plugins/command/plugin.py
Expand Up @@ -30,6 +30,7 @@
import shutil
import subprocess
import sys
import time
import requests

import pygments
Expand Down Expand Up @@ -209,9 +210,18 @@ def do_install(self, url, name, show_install_notes=True):
data = self.get_json(url)
if name in data:
utils.makedirs(self.output_dir)
LOGGER.info('Downloading: ' + data[name])
url = data[name]
LOGGER.info("Downloading '{0}'".format(url))
try:
zip_data = requests.get(url).content
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('http', 'https', 1)
zip_data = requests.get(url).content

zip_file = io.BytesIO()
zip_file.write(requests.get(data[name]).content)
zip_file.write(zip_data)
LOGGER.info('Extracting: {0} into {1}/'.format(name, self.output_dir))
utils.extract_all(zip_file, self.output_dir)
dest_path = os.path.join(self.output_dir, name)
Expand Down Expand Up @@ -296,5 +306,11 @@ def do_uninstall(self, name):

def get_json(self, url):
if self.json is None:
self.json = requests.get(url).json()
try:
self.json = requests.get(url).json()
except requests.exceptions.SSLError:
LOGGER.warning("SSL error, using http instead of https (press ^C to abort)")
time.sleep(1)
url = url.replace('http', 'https', 1)
self.json = requests.get(url).json()
return self.json

0 comments on commit 63dcbc7

Please sign in to comment.