Skip to content

Instantly share code, notes, and snippets.

@Loddan
Created November 14, 2012 22:58
Show Gist options
  • Save Loddan/4075450 to your computer and use it in GitHub Desktop.
Save Loddan/4075450 to your computer and use it in GitHub Desktop.
Generates JSON ISO 639-1 Codes from Wikipedia
#!/usr/bin/env node
var jsdom = require('jsdom');
var wikipedia = 'http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes';
var jquery = 'http://code.jquery.com/jquery.js';
jsdom.env(wikipedia, [jquery], function (errors, window) {
var $ = window.$;
var tds = $('table[class="wikitable sortable"] > tr > td');
if (tds.size() % 10 != 0) {
throw new Error('Ehhh something went terribly wrong');
}
var languages = {};
for (var i = 0, s = tds.size(); i < s; i += 10) {
var family = $(tds[i+1]).text();
var name = $(tds[i+2]).text();
var nativeName = $(tds[i+3]).text();
var alpha1 = $(tds[i+4]).text();
var alpha2t = $(tds[i+5]).text();
var alpha2b = $(tds[i+6]).text();
var alpha3 = $(tds[i+7]).text().split(/\s+/)[0];;
var alpha6 = $(tds[i+8]).text();
languages[alpha1] = {
family : family,
name : name,
nativeName : nativeName,
};
}
console.log('/* ISO 639-1 Codes');
console.log(' * https://gist.github.com/4075450');
console.log(' *', wikipedia);
console.log(' */');
console.log(JSON.stringify(languages, null, 2));
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment