Skip to content

Commit

Permalink
Save file name from response header
Browse files Browse the repository at this point in the history
fourkitchens#12 get integrated
  • Loading branch information
cdgte committed Jan 26, 2018
2 parents 71ebf40 + b50bea9 commit d41622d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions zonedata-download/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from urlparse import urlparse
import os
import cgi
import logging

logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -61,13 +62,16 @@
stream=True)
logging.info("Fetching data from %s" % url)
if r.status_code == 200:
parsed_url = urlparse(r.url)
filename = os.path.basename(parsed_url.path)
_, c = cgi.parse_header(r.headers['content-disposition'])
filename = c['filename']
if filename == "":
parsed_url = urlparse(r.url)
filename = os.path.basename(parsed_url.path) + '.txt.gz'
directory = './zonefiles'

if not os.path.exists(directory):
os.makedirs(directory)
path = directory + '/' + filename + '.txt.gz'
path = directory + '/' + filename
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
Expand Down

0 comments on commit d41622d

Please sign in to comment.