Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JoeGermuska/5a2ea14302a50848220e to your computer and use it in GitHub Desktop.
Save JoeGermuska/5a2ea14302a50848220e to your computer and use it in GitHub Desktop.
I [tweeted](https://twitter.com/JoeGermuska/status/503183667624423424)
> I needed to convert these 58 pseudo-JSON files to real JSON. How would you do it?
> https://github.com/NUKnightLab/TimelineJS/tree/master/source/js/Core/Language/locale … (Kinda proud of my solution TBH)
Here's what I did.
Noah Veltman had [a much leaner solution](https://gist.github.com/veltman/8822eb9a25488c228a52) that used NodeJS. I'm not very savvy with Node, although I started down a solution like Noah's. I got stuck because I didn't consider changing the source files.
from flask import Flask
from flask import abort, request, g
from flask import make_response, current_app, send_file, url_for
from flask import jsonify
import json
import codecs
app = Flask(__name__)
@app.route('/js/<filename>')
def serve_js(filename):
return send_file("/Users/germuska/src/TimelineJS/source/js/Core/Language/locale/" + filename,mimetype="text/javascript")
@app.route('/rewrite/<filename>',methods=['POST'])
def rewrite(filename):
ofn = "/Users/germuska/src/TimelineJS3/source/js/language/locale/%son" % filename
with codecs.open(ofn,"wb",encoding="utf-8") as f:
json.dump(request.json,f,indent=4,ensure_ascii=False)
return make_response("OK " + ofn)
@app.route('/')
def index():
return send_file("index.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5001, debug=True)
<!DOCTYPE html>
<html>
<head>
<title>Language rewriter</title>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
</head>
<body>
<script>
VMM = {}
function trace(msg) {
console.log(msg);
}
var files = ['af.js', 'ar.js', 'bg.js', 'ca.js', 'cz.js', 'da.js', 'de.js', 'el.js', 'en-24hr.js', 'en-week.js', 'en.js', 'eo.js', 'es.js', 'et.js', 'eu.js', 'fa.js', 'fi.js', 'fo.js', 'fr.js', 'gl.js', 'he.js', 'hr.js', 'hu.js', 'hy.js', 'id.js', 'is.js', 'it.js', 'iw.js', 'ja.js', 'ka.js', 'ko.js', 'lb.js', 'lt.js', 'lv.js', 'ms.js', 'ne.js', 'nl.js', 'no.js', 'pl.js', 'pt-br.js', 'pt.js', 'rm.js', 'ro.js', 'ru.js', 'si.js', 'sk.js', 'sl.js', 'sr-cy.js', 'sr.js', 'sv.js', 'ta.js', 'te.js', 'th.js', 'tl.js', 'tr.js', 'uk.js', 'zh-cn.js', 'zh-tw.js']
var i = 0;
function doit(fn) {
$(document.body).append("<p> Doing:" + fn + "</p>");
$.getScript("/js/"+fn).done(function(){
$(document.body).append("<p>posting</p>")
var done = $.ajax({
async: false,
url:"/rewrite/" + fn,
type:"POST",
data:JSON.stringify(VMM.Language),
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
console.log('success',arguments);
$(document.body).append("<p>posted</p>");
}
});
$(document.body).append(done.responseText);
i = i + 1;
if (i < files.length) {
doit(files[i]);
}
});
}
doit(files[0]);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment