Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jsbain
Last active March 1, 2019 07:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsbain/fcb3f42932dde9b0ff6c122893d1b230 to your computer and use it in GitHub Desktop.
Save jsbain/fcb3f42932dde9b0ff6c122893d1b230 to your computer and use it in GitHub Desktop.
savefile.py
#coding: utf-8
import requests
import appex,console,time,os
unquote=requests.utils.unquote
urlparse=requests.utils.urlparse
url=appex.get_url()
p=urlparse(url)
urlfilename=unquote(unquote(urlparse(appex.get_url()).path.split('/')[-1]))
console.hud_alert('downloading '+urlfilename)
console.show_activity()
if p.netloc.startswith('gist.github.com'):
gist_id=urlfilename
data=requests.get('https://api.github.com/gists/{}'.format(gist_id)).json()
if data:
gistpath=os.path.expanduser('~/Documents/gists/')
destpath=os.path.join(gistpath,gist_id)
for pth in [gistpath, destpath]:
if not os.path.exists(pth):
os.mkdir(pth)
for f in data['files'].values():
filename=f['filename']
console.hud_alert('writing '+filename)
url=f['raw_url']
#rather than deal with possibly truncated files, or unicode, just download raw url as binary
with open(os.path.join(destpath,filename),'wb') as file:
file.write(requests.get(url).content)
else:
console.hud_alert('could not download')
else:
destpath=urlfilename
with open(destpath,'wb') as file:
file.write(requests.get(url).content)
console.hud_alert(urlfilename +' complete')
console.hide_activity()
appex.finish()
@jsbain
Copy link
Author

jsbain commented Sep 11, 2016

Updated to handle unicode/truncated gists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment