diff --git a/CHANGES.txt b/CHANGES.txt index af0d40db38..fa57f6b4d7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -43,6 +43,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) diff --git a/nikola/plugins/command/install_theme.py b/nikola/plugins/command/install_theme.py index 4937509710..a243b2ceb9 100644 --- a/nikola/plugins/command/install_theme.py +++ b/nikola/plugins/command/install_theme.py @@ -27,7 +27,7 @@ from __future__ import print_function import os import io -import json +import time import requests import pygments @@ -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("-------") @@ -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) diff --git a/nikola/plugins/command/plugin.py b/nikola/plugins/command/plugin.py index 391a7c9953..ea8b073654 100644 --- a/nikola/plugins/command/plugin.py +++ b/nikola/plugins/command/plugin.py @@ -30,6 +30,7 @@ import shutil import subprocess import sys +import time import requests import pygments @@ -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) @@ -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