Is there a way to get all sites of type main_site
from the /site
method?
If not, what's the best way to get that list? Should I get all sites with a huge pagesize
and then filter it in my App?
Yes, the sites path does not filter results on site_type
. But the good news is, the path accepts a pagesize of 999
, so you can get all the results at once.
Then you must filter the results in your app. Here's what it looks like in jQuery:
$.ajax ( {
type: 'GET',
url: 'https://api.stackexchange.com/2.1/sites?pagesize=999&filter=!6ONhDO.aXP4LU&key=YOUR_APP_KEY',
dataType: 'JSON',
cache: true,
success: processSiteListJSON,
error: function (err) {
alert ("Oopsie! " + err.responseText);
}
} );
function processSiteListJSON (apiJson) {
var mainSites = $.map (apiJson.items, function (siteDat) {
if (siteDat.site_type == "main_site") {
return siteDat.api_site_parameter;
}
return null;
} );
console.log ("Main sites are: ", mainSites.sort () );
}
Note that meta.stackoverflow
is a special case and is considered a "main" site.