-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
164 lines (148 loc) · 4.36 KB
/
handler.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
require("dotenv").config();
const axios = require("axios");
const co = require("co");
const AWS = require("aws-sdk");
const { check_if_table_exists } = require("./create_table");
const table_name = "github-stats-information";
AWS.config.update({
region: process.env.REGION
});
axios.defaults.headers.common["Authorization"] = `token ${
process.env.GITHUB_APIKEY
}`;
// axios.defaults.baseURL = "https://api.github.com";
async function main(event, context, callback) {
await co(function* t() {
// const { data } = yield axios.get("https://api.github.com/repos/jackdh/RasaTalk/traffic/views");
const dynamodb = new AWS.DynamoDB();
yield check_if_table_exists(dynamodb, table_name);
const { data } = yield axios.get("https://api.github.com/user/repos");
for (let i = 0; i < data.length; i++) {
const repo = data[i];
const repo_info = yield axios.get(repo["url"]);
const repo_views = yield axios.get(`${repo["url"]}/traffic/views`);
const repo_clones = yield axios.get(`${repo["url"]}/traffic/clones`);
for (let j = 0; j < repo_views.data.views.length; j++) {
const view = repo_views.data.views[j];
const today =
new Date(view.timestamp).getDate() === new Date().getDate();
if (today) {
const params = {
Item: {
repository: {
S: repo_info.data.name.toString()
},
date: {
S: view.timestamp.toString()
},
forks: {
S: repo_info.data.forks.toString()
},
open_issues: {
S: repo_info.data.open_issues.toString()
},
stargazers_count: {
S: repo_info.data.stargazers_count.toString()
},
subscribers_count: {
S: repo_info.data.subscribers_count.toString()
},
watchers: {
S: repo_info.data.watchers.toString()
},
views: {
S: view.count.toString()
},
uniques: {
S: view.uniques.toString()
}
},
TableName: table_name
};
yield new Promise((res, rej) =>
dynamodb.putItem(params, err => (err ? rej(err) : res()))
);
} else {
const params = {
TableName: table_name,
ExpressionAttributeNames: {
"#V": "views",
"#U": "uniques"
},
ExpressionAttributeValues: {
":v": {
N: view.count.toString()
},
":u": {
N: view.uniques.toString()
}
},
Key: {
repository: {
S: repo_info.data.name
},
date: {
S: view.timestamp
}
},
UpdateExpression: "SET #V = :v, #U = :u"
};
yield new Promise((res, rej) =>
dynamodb.updateItem(params, err => (err ? rej(err) : res()))
);
}
}
for (let j = 0; j < repo_clones.data.clones.length; j += 1) {
const clone = repo_clones.data.clones[j];
const params = {
TableName: table_name,
ExpressionAttributeNames: {
"#C": "clones",
"#UC": "unqiue_clones"
},
ExpressionAttributeValues: {
":c": {
N: clone.count.toString()
},
":uc": {
N: clone.uniques.toString()
}
},
Key: {
repository: {
S: repo_info.data.name
},
date: {
S: clone.timestamp
}
},
UpdateExpression: "SET #C = :c, #UC = :uc"
};
yield new Promise((res, rej) =>
dynamodb.updateItem(params, err => (err ? rej(err) : res()))
);
}
}
console.log("Done");
return true;
})
.then(() => {
console.log("Success");
callback(null, {
statusCode: 200,
body: JSON.stringify({
message: "Scraped!"
})
});
})
.catch(err => {
console.log("Failed: ");
console.log(err);
callback(err);
});
}
// main(null);
module.exports = {
main
};