Skip to content
Permalink
Browse files

Allowing HTTP authentication for downloading WordPress blog content.

Also providing error messages when not downloading instead of storing error message in destination file.
  • Loading branch information
felixfontein committed Jun 27, 2015
1 parent 3d3b030 commit 1cdeb837b9a356806a95e8de7f8b620e15d67e25
Showing with 20 additions and 1 deletion.
  1. +20 −1 nikola/plugins/command/import_wordpress.py
@@ -83,6 +83,13 @@ class CommandImportWordpress(Command, ImportMixin):
'type': bool,
'help': "Do not try to download files for the import",
},
{
'name': 'download_auth',
'long': 'download-auth',
'default': None,
'type': str,
'help': "Specify username and password for HTTP authentication (separated by ':')",
},
{
'name': 'separate_qtranslate_content',
'long': 'qtranslate',
@@ -131,6 +138,14 @@ def _execute(self, options={}, args=[]):
self.exclude_drafts = options.get('exclude_drafts', False)
self.no_downloads = options.get('no_downloads', False)

self.auth = None
if options.get('download_auth', None) is not None:
username_password = options.get('download_auth')
self.auth = tuple(username_password.split(':', 1))
if len(self.auth) < 2:
print("Please specify HTTP authentication credentials in the form username:password.")
return False

self.separate_qtranslate_content = options.get('separate_qtranslate_content')
self.translations_pattern = options.get('translations_pattern')

@@ -262,8 +277,12 @@ def download_url_content_to_file(self, url, dst_path):
return

try:
request = requests.get(url, auth=self.auth)
if request.status_code >= 400:
LOGGER.warn("Downloading {0} to {1} failed with HTTP status code {2}".format(url, dst_path, request.status_code))
return
with open(dst_path, 'wb+') as fd:
fd.write(requests.get(url).content)
fd.write(request.content)
except requests.exceptions.ConnectionError as err:
LOGGER.warn("Downloading {0} to {1} failed: {2}".format(url, dst_path, err))

0 comments on commit 1cdeb83

Please sign in to comment.
You can’t perform that action at this time.