Create a gist now

Instantly share code, notes, and snippets.

@omz /New from Secret
Last active Jan 24, 2017

New from Gist
# This script downloads and opens a Gist from a URL in the clipboard.
# It's meant to be put in the editor's actions menu.
# It works with "raw" and "web" gist URLs, but not with gists that
# contain multiple files or non-Python files.
# If a file already exists, a dialog is shown that asks whether the
# new file should be renamed automatically.
import clipboard
import editor
import console
import re
import os
class InvalidGistURLError (Exception): pass
class MultipleFilesInGistError (Exception): pass
class NoFilesInGistError (Exception): pass
class GistDownloadError (Exception): pass
def download_gist(gist_url):
# Returns a 2-tuple of filename and content
# console.show_activity()
raw_match = re.match('http(s?)://', gist_url)
if raw_match:
import requests
from urlparse import urlparse
filename = os.path.split(urlparse(gist_url).path)[1]
r = requests.get(gist_url)
content = r.text
return filename, content
raise GistDownloadError()
gist_id_match = re.match('http(s?)://[0-9a-zA-Z]*)/([0-9a-f]*)', gist_url)
if gist_id_match:
import requests
gist_id =
json_url = '' + gist_id
import json
gist_json = requests.get(json_url).text
gist_info = json.loads(gist_json)
files = gist_info['files']
raise GistDownloadError()
py_files = []
for file_info in files.values():
lang = file_info.get('language', None)
if lang != 'Python':
if len(py_files) > 1:
raise MultipleFilesInGistError()
elif len(py_files) == 0:
raise NoFilesInGistError()
file_info = py_files[0]
filename = file_info['filename']
content = file_info['content']
return filename, content
raise InvalidGistURLError()
def main():
gist_url = clipboard.get()
filename, content = download_gist(gist_url)
if os.path.isfile(filename):
i = console.alert('File exists', 'A file with the name ' + filename +
' already exists in your library.',
'Auto Rename')
if i == 1:
editor.make_new_file(filename, content)
editor.make_new_file(filename, content)
except InvalidGistURLError:
console.alert('No Gist URL',
'The clipboard doesn\'t seem to contain a valid Gist URL.',
except MultipleFilesInGistError:
console.alert('Multiple Files', 'This Gist contains multiple ' +
'Python files, which isn\'t currently supported.')
except NoFilesInGistError:
console.alert('No Python Files', 'This Gist contains no Python files.')
except GistDownloadError:
console.alert('Error', 'The Gist could not be downloaded.')
if __name__ == '__main__':

If you change line 36 (the regex) then the ErrorMesage "The Gist could not be downloaded..." should be gone.

gist_id_match = re.match('http(s?)://[0-9a-zA-Z]*)/([0-9a-f]*)', gist_url)

You also need to change line 39 to get the correct gist id, due to the change of the regex

gist_id =

Hope this helps.

omz commented May 11, 2013

Thanks @salomonderossi, I've updated the script with the new regex.

cclauss commented Jan 20, 2014

My sense is that it is broken yet again. Line 45 'files' is not a valid key.


I needed to change row 41 to gist_id_match = re.match('http(s?)://[0-9a-zA-Z])/([0-9a-f])', gist_url)

and row 44 to be gist_id =


Why not support multiple files in gist? That might be very helpful when downloading a lib of my own, for instance

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