Skip to content

Commit

Permalink
Move from sitematrix.json to wikis.json
Browse files Browse the repository at this point in the history
Also, settings language selector now displays localName as well
  • Loading branch information
yuvipanda authored and jdlrobson committed May 1, 2012
1 parent 3d77912 commit 9042823
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 37 deletions.
2 changes: 1 addition & 1 deletion assets/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<label for="contentLanguageSelector"><msg key="settings-language-label"></msg></label>
<select id='contentLanguageSelector'>
{{#languages}}
<option value="{{code}}">{{name}}</option>
<option value="{{code}}">{{name}} {{#localName}}({{localName}}){{/localName}}</option>
{{/languages}}
</select>
</div>
Expand Down
18 changes: 17 additions & 1 deletion assets/www/js/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
window.app = function() {

var wikis = [];

function getWikiMetadata() {
var d = $.Deferred();
if(wikis.length === 0) {
$.get(ROOT_URL + 'wikis.json').done(function(data) {
wikis = JSON.parse(data);
d.resolve(wikis);
});
} else {
d.resolve(wikis);
}
return d;
}

function loadCachedPage (url, title, lang) {
chrome.showSpinner();
var d = $.Deferred();
Expand Down Expand Up @@ -223,7 +238,8 @@ window.app = function() {
setCurrentPage: setCurrentPage,
track: track,
curPage: null,
navigateTo: navigateTo
navigateTo: navigateTo,
getWikiMetadata: getWikiMetadata
};

return exports;
Expand Down
48 changes: 14 additions & 34 deletions assets/www/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,25 @@ window.appSettings = function() {
];
}

if(locales.length == 0) {
var dataType = 'json';
if ($('html').hasClass('winphone')) {
// Not sure why, but on Windows Phone if we ask for text we get JSON.
// MYSTERIOUS
dataType = 'text';
}
$.ajax({
type:'GET',
url:requestUrl,
dataType: dataType,
success:function(results) {
console.log('sitematrix got: ' + results);
var allLocales;
if (results) {
allLocales = results.sitematrix;
} else {
allLocales = []; // hack
if(locales.length === 0) {
app.getWikiMetadata().done(function(wikis) {
$.each(wikis, function(lang, wikiData) {
var locale = {
code: lang,
name: wikiData.name
};
if(wikiData.name !== wikiData.localName) {
locale.localName = wikiData.localName;
}
$.each(allLocales, function(key, value) {
// Because the JSON result from sitematrix is messed up
if(!isNaN(key)) {
if(value.site.some(function(site) { return site.code == "wiki"; })) {
locales.push({
code: value.code,
name: value.name
});
}
}
});
chrome.hideSpinner();
renderSettings();
}
locales.push(locale);
});
renderSettings();
chrome.hideSpinner();
});
} else {
chrome.hideSpinner();
renderSettings();
chrome.hideSpinner();
}

}

function renderSettings() {
Expand Down
1 change: 0 additions & 1 deletion assets/www/sitematrix.json

This file was deleted.

1 change: 1 addition & 0 deletions assets/www/wikis.json

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions scripts/generate_wikimeta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from urllib2 import urlopen, HTTPError
try:
import json
except:
import simplejson as json

SITEMATRIX_URL = "http://en.wikipedia.org/w/api.php?action=sitematrix&format=json"

sitematrix = json.loads(urlopen(SITEMATRIX_URL).read())

wikis = {}

for k in sitematrix['sitematrix']:
# exclude the 'count' key
if k.isdigit():
lang = sitematrix['sitematrix'][k]
for site in lang['site']:
if site['code'] == 'wiki' and 'closed' not in site:
curWiki = {
'name': lang['name'],
'localName': lang['localname'],
}
wikis[lang['code']] = curWiki

print "Obtained wiki list"

mainPageCounter = 0
for lang in wikis:
wiki = wikis[lang]
url = 'http://' + lang + '.wikipedia.org/wiki/MediaWiki:Mainpage?action=raw'
# 404 returned if MediaWiki:Mainpage is not overriden
# At which point we ask specifically for the message
try:
wiki['mainPage'] = urlopen(url).read()
except HTTPError, err:
if err.code == 404:
url = url + "&usemsgcache=true"
wiki['mainPage'] = urlopen(url).read()
else:
raise
mainPageCounter += 1
print "Obtained Main Page for " + lang + " (" + str(mainPageCounter) + "/" + str(len(wikis)) + ")"

open("wikis.json", "w").write(json.dumps(wikis))

0 comments on commit 9042823

Please sign in to comment.