Skip to content

Commit

Permalink
Allowing HTTP authentication for downloading WordPress blog content.
Browse files Browse the repository at this point in the history
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 1cdeb83
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion nikola/plugins/command/import_wordpress.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 1cdeb83

Please sign in to comment.