Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1848 from getnikola/wordpress-import-auth-downloads
Allowing HTTP authentication for downloading WordPress blog content
  • Loading branch information
felixfontein committed Jun 29, 2015
2 parents 3d3b030 + 1cdeb83 commit f330a1c
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 f330a1c

Please sign in to comment.