Skip to content

Instantly share code, notes, and snippets.

@suderman
Created August 24, 2016 21:42
Show Gist options
  • Save suderman/e7780994de889f6ef97ed01e79b39e70 to your computer and use it in GitHub Desktop.
Save suderman/e7780994de889f6ef97ed01e79b39e70 to your computer and use it in GitHub Desktop.
save_gist.py
#coding: utf-8
import requests
import appex,console,time,os
if appex.is_running_extension():
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)
content=f['content']
with open(os.path.join(destpath,filename),'w') as file:
file.write(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()
else:
console.hud_alert('Run this script from Share Sheet')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment