@SigmaG/

GetListOfAllEmployeesThroughAPI

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var curr = 1, // specify start ID here, minimum 1
    MAX_IDS_AT_ONCE = 100,
    MAX_NUMBER_OF_QUERIES = 4000,
    MAX_ID = MAX_NUMBER_OF_QUERIES * MAX_IDS_AT_ONCE + curr,
    TIMER_NAME = "Checking employee list",
    ACCESS_TOKEN = "???",
    API_KEY = "???";

// beware: querying beyond the number of existing users will silently
// keep returning empty `.items` array without raising an array
// hence, set a sensible MAX_NUMBER_OF_QUERIES value to make sure the recursion ends
// check the new_users page (https://meta.stackexchange.com/users?tab=NewUsers&sort=creationdate) to get a sensible MAX_NUMBER_OF_QUERIES
// as of 2018 July, te latest new users was around ID number 95000, so a sensible MAX_NUMBER_OF_QUERIES = 4000 (=total / MAX_IDS_AT_ONCE)

function generateIDsString(id){
    var DELIMITER = ";", output = "", max = id + MAX_IDS_AT_ONCE;

    while(id < max){
        output += id + ";";
        id++;
    }

    return output.substring(0, output.length - 1);
}

function tryNextIDsSet(){
    var xhr = new XMLHttpRequest(),
        idString = generateIDsString(curr);
    
    xhr.open("GET", "https://api.stackexchange.com/2.2/users/" + idString + "?pagesize=" + MAX_IDS_AT_ONCE + "&site=meta&filter=!*MxJcsv91Tcz6yRH&access_token=" + ACCESS_TOKEN + "&key=" + API_KEY);

    xhr.send();

    xhr.onload = function(){
        var response = JSON.parse(this.response),
             items = response.items;
        if(response.error_message){
            console.log("Stopped at ", this);
            console.timeEnd(TIMER_NAME);
        } else {
            // some users might be deleted, hence don't loop till MAX_IDS_AT_ONCE
            for(var i = 0, len = items.length, item; i < len; i++){
                item = items[i];
                if(item.is_employee)
                    console.log(item.user_id);
            }

            if(curr <= MAX_ID){
                console.log("Currently at: " + curr);
                tryNextIDsSet();
            }
            else{
                console.log("Finished execution");
                console.timeEnd(TIMER_NAME);
            }
        }
    };

    curr += MAX_IDS_AT_ONCE;
}

// tryNextIDsSet();
Native Browser JavaScript