forked from gayanvoice/most-active-github-users-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphQuery.js
133 lines (126 loc) · 4.87 KB
/
GraphQuery.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const axios = require('axios');
const util = require('./util');
var mongo = require('mongodb').MongoClient;
const assert = require('assert');
module.exports = class GraphQuery {
constructor(cities, key, records, url) {
process.stdout.write(cities[0].toLowerCase() + ' ');
this.jsonAr = [];
this.refresh = 0;
this.next = true;
this.cursor = null;
this.cities = cities;
this.country = cities[0].toLowerCase();
this.key = key;
this.url = url;
this.per = records[0];
this.num = records[1];
this.query = `
query {
search(type: USER, query:"%s sort:followers-desc", first:%s, after:%s) {
edges {
node {
__typename
... on User {
login,
avatarUrl(size: 72),
name,
company,
location,
organizations(first: 1) {
nodes {
login
}
}
followers {
totalCount
}
contributionsCollection {
contributionCalendar {
totalContributions
}
restrictedContributionsCount
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}`;
}
request() {
if (!(this.refresh < this.num)) {
this.next = false;
} else {
this.refresh = this.refresh + 1;
}
//console.log(this.re);
if (this.next) {
axios
.create({
baseURL: 'https://api.github.com/graphql',
headers: {
Authorization: `bearer ` + this.key + ``
},
})
.post('', {
query: util.parse(this.query, util.locations(this.cities), this.per, this.cursor)
})
.then(({data}) => {
//console.log(data);
var jsonStr = data.data.search.edges;
var jsonAr = this.jsonAr;
this.cursor = "\"" + data.data.search.pageInfo.endCursor + "\"";
this.next = data.data.search.pageInfo.hasNextPage;
process.stdout.write(this.refresh + ' ');
Object.keys(jsonStr).forEach(function (index, key) {
if (jsonStr[key].node.__typename === "User") {
// "__typename": "User",
//process.stdout.write(index + ' ');
//console.log(key, jsonStr[key].node.__typename, jsonStr[key].node.login, jsonStr[key].node.name, jsonStr[key].node.followers.totalCount);
var record = {
'id': key,
'avatar_url': jsonStr[key].node.avatarUrl,
'login': jsonStr[key].node.login,
'name': jsonStr[key].node.name,
'location': jsonStr[key].node.location,
'followers': jsonStr[key].node.followers.totalCount,
'public_contributions': jsonStr[key].node.contributionsCollection.contributionCalendar.totalContributions,
'private_contributions': jsonStr[key].node.contributionsCollection.restrictedContributionsCount
};
jsonAr.push(record);
} else {
console.log(jsonStr[key].node.__typename);
}
});
this.request();
})
.catch(function (e) {
console.log(util.getDateTime() + e);
});
} else {
var data = {
country: this.country,
dataset: this.jsonAr,
modified: util.getDateTime()
};
mongo.connect(this.url, {useUnifiedTopology: true}, function (err, client) {
assert.equal(null, err);
const collection = client.db("database").collection("countries");
collection.updateOne(
{_country: data.country},
{$set: data},
{upsert: true})
.then(function () {
// process result
client.close();
});
});
console.log(util.getDateTime() + " The file is saved!" + util.locations(this.cities));
}
util.check_file(this.path);
}
};