Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Created June 10, 2011 10:51
Show Gist options
  • Save jdlrobson/1018613 to your computer and use it in GitHub Desktop.
Save jdlrobson/1018613 to your computer and use it in GitHub Desktop.
This python script collects plugins specified by name in tiddlyspace spaces, minifies them and puts them to the DESTINATION space. A user and pass are provided for authorisation to save to DESTINATION
import httplib2, urllib, datetime, os
from datetime import datetime as d
import simplejson
USER = "<user>"
PASS = "<pass>"
DESTINATION = "package-i"
PLUGINS = [{ "title": "TiddlyPoller", "space": "poller" }, { "title": "StaticMapPlugin", "space": "staticmaps"}, { "title": "ExtraFiltersPlugin", "space": "filters" },
{ "title": "EditTemplateFieldsPlugin", "space": "first-class-fields" }, { "title": "MySearchPlugin", "space": "mysearch" }, { "title": "ReverseLookupFix", "space": "mysearch" },
{ "title": "TagCloudPlugin", "space": "tagcloud" }, { "title": "SerializerLinks", "space": "serializer-links" }, { "title": "AudioTiddlersPlugin", "space": "audio" },
{ "title": "LoadMissingTiddlersPlugin", "space": "lazy" }, { "title": "NiceTaggingPlugin", "space": "nicetagging" }]
h = httplib2.Http()
h.follow_redirects = False
#login
body = {'user': USER, 'password': PASS, 'tiddlyweb_redirect':'/'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = h.request("http://tiddlyspace.com/challenge/tiddlywebplugins.tiddlyspace.cookie_form", method='POST', headers=headers, body=urllib.urlencode(body))
print response.status
cookie = response['set-cookie']
siteinfo = "tags: package\n\nThis space is automatically generated on a daily basis. It packages together various plugins when they are updating in their original space. It also minifies their code to reduce bloat on includers. It contains the following useful plugins:\n"
changes = 0
for plugin in PLUGINS:
space = plugin["space"]
title = plugin["title"]
siteinfo += "* [[%s]]@%s\n"%(title, space)
print "requesting %s from %s"%(title, space)
resp, tiddler = h.request("http://tiddlyspace.com/bags/%s_public/tiddlers/%s.json"%(space, urllib.quote(title)), method="GET")
resp2, packagetiddler = h.request("http://tiddlyspace.com/bags/%s_public/tiddlers/%s.json"%(DESTINATION, urllib.quote(title)), method="GET")
tid = simplejson.loads(tiddler)
tid["fields"]["_twinrevision"] = tid["revision"] #keep track of the revision number we are copying across
if resp2.status == 404:
tid2 = { "title": title, "fields": {} }
else:
tid2 = simplejson.loads(packagetiddler)
originalRevision = str(tid["revision"])
try:
existingRevision = tid2["fields"]["_twinrevision"]
except KeyError:
existingRevision = "-1"
if originalRevision != existingRevision or resp2.status == 404: #then something has changed and the package version needs to be updated
try:
os.remove("tmptiddler.txt")
except OSError:
pass
x = open("tmptiddler.txt", "w")
x.writelines(tid["text"])
x.close()
x = open("tmptiddler.txt", "r")
os.system("java -jar yuicompressor-2.4.6.jar tmptiddler.txt --type js -o tmptiddler.txt")
print "#####"
x = open("tmptiddler.txt", "r")
tid["text"] = "".join(x.readlines())
tid["fields"]["_twinrevision"] = originalRevision
changes += 1
url = "http://%s.tiddlyspace.com/bags/%s_public/tiddlers/%s"%(DESTINATION, DESTINATION, urllib.quote(title))
print "updating %s"%url
resp, content = h.request(url,
"PUT", body=simplejson.dumps(tid),headers={'content-type':'application/json', 'Cookie': cookie})
print resp.status
else:
print "%s is already up to date"%title
if changes > 0:
print "put site info tiddler"
resp, content = h.request("http://%s.tiddlyspace.com/bags/%s_public/tiddlers/SiteInfo"%(DESTINATION, DESTINATION),
"PUT", body=siteinfo,headers={'content-type':'text/plain', 'Cookie': cookie})
print resp.status
print "fini"
else:
print "no need to update SiteInfo tiddler"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment