Skip to main content
1 of 3
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60

Here is my take on kevin's sort using Soapi.JS

  • get a list of sites from stackauth /sites
  • get the community user from each site to get creation date
  • attach each meta to it's parent
  • sort by date
  • group by 'state' or type.
  • render grouped or as a single list.

Live Demo HERE


alt text http://img121.imageshack.us/img121/451/pantheon.png


/* 
This is the money shot
*/

// get a list of sites from stackauth
Soapi.RouteFactory("", apiKey).Sites()
.getResponse(function(sitesResponse) {

    // count down so we know when we are done
    var count = sitesResponse.items.length;

    // determine the increment of the progress bar for each operation
    progressStep = document.getElementById("progressOuter").clientWidth / count;


    function checkCount() {

        stepProgress();
        if (--count == 0) {
            // all sites reported in

            // perform meta assocations

            while (sitesResponse.items.length > 0) {

                // take the site of the bottom
                var item = sitesResponse.items.shift();

                // if it is not a meta site, add it to the site list
                // cannot depend on linked_meta right not
                if (item.api_endpoint.toLowerCase().indexOf(".meta.") == -1) {
                    sites.push(item);
                }
                else {
                    // check the site list to see if the parent has been seen yet
                    var parent = sites.tryGetParentByEndpoint(item.api_endpoint);
                    if (parent) {
                        // attach the meta to the parent
                        parent.linked_meta = item;
                    }
                    else {
                        // parent not seen yet, stick it back on top
                        sitesResponse.items.push(item);
                    };
                };
            };

            // do sorting and arrangement

            // now we have a list of sites similar to this
            // stackoverflow.com
            //    meta.stackoverflow.com
            // stackapps.com
            // gaming.stackexchange.com
            //    meta.gaming.stackexchange.com


            // lets sort by date
            sites = sites.sort(function(a, b) {
                return a.creation_date - b.creation_date;
            });

            // and segregate the sites by state as we 
            sites.normal = [];
            sites.open_beta = [];
            sites.closed_beta = [];
            sites.linked_meta = [];

            // empty the array
            var tmp = sites.splice(0, sites.length);

            // and push them in order of appearance to maintain date sort
            // into both the parent array and the group arrays
            while (tmp.length > 0) {
                var site = tmp.shift();
                sites.push(site);
                sites[site.state].push(site);
                if (site.linked_meta) {
                    sites.linked_meta.push(site.linked_meta);
                };
            };

            progressComplete();
            // return results
            callback(sites);
        };
    };


    // get community user from each site to get creation date

    for (var i = 0; i < sitesResponse.items.length; i++) {

        var site = sitesResponse.items[i];

        Soapi.RouteFactory(site.api_endpoint, apiKey)
        .UsersById(-1).getResponse(function(usersResponse, context) {

            // add the date to the site so we can sort
            context.creation_date = usersResponse.items[0].creation_date;
            checkCount();
        }, function(error, context) {

            // probably timed out. set now as creation date
            // attach the error
            context.creation_date = new Date();
            context.error = error.message;
            // still need to decrement count so we don't get stuck
            checkCount();
        }, 10000, site);
        // we want to give a reasonable timeout value because
        // we are doing a bunch of stuff here. it should not 
        // take this long, just being safe

    };
});
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60