Skip to content

Instantly share code, notes, and snippets.

@JKirchartz
Last active December 19, 2015 21:38
Show Gist options
  • Save JKirchartz/6021251 to your computer and use it in GitHub Desktop.
Save JKirchartz/6021251 to your computer and use it in GitHub Desktop.
Download JSFiddles, so you can keep 'em forever and ever and ever.

Backup JSFiddles

Back up your JSFiddle's in a snap

usage

$>wget https://gist.github.com/JKirchartz/6021251/raw/d12d85aa75d5decceed6e4ccbc1b9c4aa2b03ba1/GetFiddles.py
$>chmod +x GetFiddles.py
$>GetFiddles.py <username>

will download the script, then run it, which downloads up to 9000 fiddles from your account into a folder named "fiddles." and then generate a simple index page for it. It also nabs fiddle's normalize.css so most pages should work right, and look right.

Once the script has ran, you can serve it up, simply, with:

$>cd ./fiddles
$>python -m SimpleHTTPServer

then it will (probably) be hosted at http:/0.0.0.0:8000.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, os, httplib, codecs
import simplejson as json
def get(url,filename):
try:
conn = httplib.HTTPConnection('jsfiddle.net')
conn.request('GET',url)
f = open('fiddles/'+filename,'w+')
f.write(conn.getresponse().read())
f.close()
except Exception, e:
print e
def main(argv):
if len(sys.argv) == 1:
print "You forgot a username, try GetFiddles.py <username>"
exit()
else:
username = sys.argv[1];
url = "".join(["http://jsfiddle.net/api/user/",
username,
"/demo/list.json?limit=9000&sort=alphabetical"])
try:
conn = httplib.HTTPConnection('jsfiddle.net')
conn.request('GET',url)
result = conn.getresponse()
data = json.loads(str(result.read()))
except Exception, e:
print e
# make directories
if not os.path.exists('fiddles'):
os.makedirs('fiddles/css')
print "Backing up: "
index = "<!doctype><title>JSFiddles</title>"
index += "<h1>"+username+"'s JSFiddles</h1><ul>"
for datum in data:
filename = datum['url'][-6:-1] + '.htm'
title = datum['title']
index += "<li><a href="+filename+">"+title+"</a>"
print datum['url']
get(datum['url']+'show/light/', filename)
index += "</ul>"
get('http://jsfiddle.net/css/normalize.css', 'css/normalize.css')
f = open('fiddles/index.html','w+')
f.write( codecs.BOM_UTF8 )
f.write(index.encode( "utf-8" ))
f.close()
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment