forked from theJenix/mailplan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
29 lines (26 loc) · 1.17 KB
/
Copy pathutil.py
File metadata and controls
29 lines (26 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import urllib2
import shutil
import urlparse
import os
def download(url, targetPath='.', fileName=None):
def getFileName(url,openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
openUrl.info()['Content-Disposition'].split(';')))
if 'filename' in cd:
filename = cd['filename'].strip("\"'")
if filename: return filename
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlparse.urlsplit(openUrl.url)[2])
r = urllib2.urlopen(urllib2.Request(url))
try:
fileName = fileName or getFileName(url,r)
expandedPath = os.path.expanduser(targetPath)
print "Downloading file", fileName
with open(expandedPath + '/' + fileName, 'wb') as f:
shutil.copyfileobj(r,f)
finally:
r.close()
#download("http://delivery.qmags.com/d/DefaultV1.aspx?cid=2278792&cid=2278792&sessionID=62F241AABEF354FBE0F124CF3&editionID=18887&platform=A&")