Skip to content

Instantly share code, notes, and snippets.

@psychok7
Last active December 14, 2015 04:39
Show Gist options
  • Save psychok7/5029496 to your computer and use it in GitHub Desktop.
Save psychok7/5029496 to your computer and use it in GitHub Desktop.
Download Zipfile and extract
import urllib2 ,argparse, shutil, urlparse , os , zipfile, os.path
from zipfile import ZipFile as zip
parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()
url = args.url
def extractAll(zipName):
z = zip(zipName)
for f in z.namelist():
if f.endswith('/'):
os.makedirs(f)
else:
z.extract(f)
def download(url, 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)
with open(fileName, 'wb') as f:
shutil.copyfileobj(r,f)
finally:
r.close()
def getmodelandtexture():
if os.path.exists('./models'):
os.chdir("models")
for files in os.listdir("."):
if files.endswith("dae"):
print files
os.chdir("../")
if os.path.exists('./images'):
os.chdir("images")
for files in os.listdir("."):
if files.endswith("png") or files.endswith("jpg") or files.endswith("jpeg"):
print files
def main():
download(url,"temp_kmz")
os.mkdir('./temp')
os.chdir("temp")
extractAll("../temp_kmz")
#extracted_foldername = os.walk('.').next()[1][0]
getmodelandtexture()
#shutil.rmtree(extracted_foldername)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment