forked from imbaguanxin/GitHub-User-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_collector.Rmd
306 lines (282 loc) · 8.88 KB
/
Data_collector.Rmd
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
---
title: "GithubApi"
author: "Guan.Xin"
date: "2019/4/5"
output:
pdf_document: default
html_notebook: default
---
# GitHub Api and Data Collection
In this part, we made use of GitHub Api to fetch user and repositories information.
We view the network
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(jsonlite)
library(httpuv)
library(httr)
library(mongolite)
```
# Set Up github API
```{r}
oauth_endpoints("github")
projectApp <- oauth_app(appname = "GuanXin_Github_and_World",
key = "1151cd623b25dc65040b",
secret = "5343cd60555c10ccbeb6ace4adf8594b887f3024")
#projectApp <- oauth_app(appname = "JamesgeziqianGitOAuthApp",
# key = "885b524769dc6bef013f",
# secret = "ebac8d1117a038c5facb0a9f8d2be38833d37c60")
github_token <- oauth2.0_token(oauth_endpoints("github"), projectApp)
gtoken <- config(token = github_token)
# This function helps get something manually
github_get <- function(path){
url <- modify_url("https://api.github.com", path = path)
GET(url, gtoken)
}
# This function get a json file from given url
github_get_with_url<- function(path){
req <- GET(path, gtoken)
stop_for_status(req)
req <- req %>% content %>% jsonlite::toJSON()
return(req)
}
```
# connect to database
```{r}
# db for users
db.user <- mongo(collection = "users", db = "github", url = "mongodb://localhost:27123")
# db for repositories
db.repo <- mongo(collection = "repos", db = "github", url = "mongodb://localhost:27123")
# db for users we haven't fetch followers
db.userUnfetched <- mongo(collection = "unfetched", db = "github", url = "mongodb://localhost:27123")
# db for user finished with repo fetched
db.userWithRepo <- mongo(collection = "userWithRepo", db = "github", url = "mongodb://localhost:27123")
# db for user without repo fetched
db.userNoRepo <- mongo(collection = "userNoRepo", db = "github", url = "mongodb://localhost:27123")
```
# functions to process user information
```{r}
store_user_to_db <- function(dest_db, check_db, user_url){
# get user informationg from github
user_json <- github_get_with_url(user_url)
user_list <- jsonlite::fromJSON(user_json)
user_id <- user_list[["id"]]
user_name <- user_list[["name"]]
print(sprintf("user id: %s, user name: %s", user_id, user_name))
# find id in mongoDB
query <- sprintf('{"id" : %s}', user_id)
if(dest_db$find(query) %>% nrow() == 0 &&
check_db$find(query) %>% nrow() == 0){
dest_db$insert(user_json)
return(TRUE)
}
return(FALSE)
}
# store one's foller to a specific data base
get_follers_by_id <- function(user_id, fromDB = db.userUnfetched){
query <- sprintf('{"id" : %s}', user_id)
followers_url <- c()
# find given id in the database
df <- fromDB$find(query)
# if find given user
if (df %>% length > 0){
# get given id's follower url (one url)
followers_url <- df[["followers_url"]] %>% unlist
df <- github_get_with_url(followers_url[1]) %>% fromJSON()
# get given id's followers's url (many urls)
followers_url <- df[["url"]] %>% unlist
# if follers are more than 0, store all follers to userUnfetched
if (followers_url %>% length() > 0){
for(i in 1:length(followers_url)){
store_user_to_db(dest_db = db.userUnfetched,
check_db = db.user,
followers_url[i])
}
# perfectly fetched follers
return (0)
} else {
# if there are no follers, return 1
return (1)
}
}
# if not find given user, return -1
return (-1)
}
# store one's following to a specific data base
get_follering_by_id <- function(user_id, fromDB = db.userUnfetched){
query <- sprintf('{"id" : %s}', user_id)
foing_url <- c()
# find given id in the database
df <- fromDB$find(query)
# if find given user
if (df %>% length > 0){
# get given id's following url (one url)
foing_url <- df[["following_url"]] %>% unlist %>%
str_remove("(\\{\\/other_user\\})")
print(foing_url[1])
df <- github_get_with_url(foing_url[1]) %>% fromJSON()
# get given id's following's url (many urls)
foing_url <- df[["url"]] %>% unlist
# if follers are more than 0, store all follers to userUnfetched
if (foing_url %>% length() > 0){
for(i in 1:length(foing_url)){
store_user_to_db(dest_db = db.userUnfetched,
check_db = db.user,
foing_url[i])
}
# perfectly fetched follers
return (0)
} else {
# if there are no follers, return 1
return (1)
}
}
# if not find given user, return -1
return (-1)
}
# move a user, if no user in fromDB, return -1
# if there is duplicated user in toDB, renew it and return 1
# if there is no duplication in toDB, move and return 0
move_user <- function(user_id, fromDB = db.userUnfetched, toDB = db.user){
result <- 0
fetch_query <- sprintf('{"id" : %s}', user_id)
df <- fromDB$find(fetch_query)
if (df %>% length() == 0){
return(-1)
} else {
if (toDB$find(fetch_query) %>% length() > 0){
toDB$remove(fetch_query)
result <- 1
}
toDB$insert(df)
fromDB$remove(fetch_query)
return(result)
}
}
# copy a user, if no user in fromDB, return -1
# if there is duplicated user in toDB, renew it and return 1
# if there is no duplication in toDB, copy and return 0
copy_user <- function(user_id, fromDB, toDB){
result <- 0
fetch_query <- sprintf('{"id" : %s}', user_id)
df <- fromDB$find(fetch_query)
if (df %>% length() == 0){
return(-1)
} else {
if (toDB$find(fetch_query) %>% length() > 0){
toDB$remove(fetch_query)
result <- 1
}
toDB$insert(df)
print(result)
return(result)
}
}
# fetch users following and followers given a vector of user id.
fetch_users<- function(id_vec){
for(i in 1:length(id_vec)){
get_follering_by_id(id_vec[i])
get_follers_by_id(id_vec[i])
print("finish fetching one person")
copy_user(id_vec[i], fromDB = db.userUnfetched, toDB = db.userNoRepo)
print("finish copying to userNoRepo")
move_user(id_vec[i])
}
}
get_all_user_id <- function(db = db.userUnfetched){
df <- db$find('{}')
return(df[["id"]] %>% unlist)
}
```
# export mongoDB
```{r}
# export mongodb to a data frame with query
mongo_to_df_query <- function(database, query){
db_list <- database$find(query)
df <- db_list %>% as.tibble()
df <- apply(df, 2, as.character) %>% data.frame(stringsAsFactors = F)
for(name in names(df)){
df[, name] <- str_replace_all(df[,name], pattern = "list\\(\\)", replacement = "")
}
return(df)
}
# export mongodb as a data frame
mongo_to_df <- function(database){
return(mongo_to_df_query(database, '{}'))
}
# print nongodb to a csv file
mongo_to_csv <- function(database, file_name){
df <- mongo_to_df(database)
write.csv(df, file = file_name, row.names = F)
}
```
# set up
```{r}
store_user_to_db(user_url = "https://api.github.com/users/imbaguanxin", dest_db = db.userUnfetched, check_db = db.user)
follower <- get_follers_by_id(33470168, db.userUnfetched)
following <- get_follering_by_id(33470168)
# mongo_to_csv(db.userUnfetched, "user.csv")
```
# fetch
```{r}
id_vec <- get_all_user_id()
fetch_users(id_vec)
# mongo_to_csv(db.userUnfetched, "user.csv")
```
# move all userUnfetched to userNoRepo
```{r}
id_vec <- get_all_user_id(db = db.userUnfetched)
for(i in 1:length(id_vec)){
move_user(id_vec[i], fromDB = db.userUnfetched, toDB = db.userNoRepo)
}
```
# funtions to store repositories
```{r}
# copy user to user no repo
#id_vec <- get_all_user_id(db = db.user)
#for(i in 1:length(id_vec)) {
# copy_user(id_vec[i], db.user, db.userNoRepo)
#}
# fetch given user's repos
id_get_repo <- function(id_vec, toDB = db.repo, id_from_DB, id_to_DB){
for(i in 1:length(id_vec)){
user_id <- id_vec[i]
query <- sprintf('{"id" : %s}', user_id)
user_df <- id_from_DB$find(query)
if (user_df %>% length > 0){
repo_url <- user_df[["repos_url"]] %>% unlist
print(repo_url)
repos_json <- github_get_with_url(repo_url) %>% fromJSON
if(repos_json %>% length > 0) {
toDB$insert(repos_json)
}
}
move_user(id_vec[i], fromDB = id_from_DB, toDB = id_to_DB)
}
}
# fetch user's repo from userNoRepo data base
id_vec <- get_all_user_id(db = db.userNoRepo)
id_vec <- id_vec[-1]
id_get_repo(id_vec, toDB = db.repo, id_from_DB = db.userNoRepo, id_to_DB = db.userWithRepo)
```
# export to df and csv
```{r}
df <- mongo_to_df(db.user)
df <- df %>% rbind(mongo_to_df(db.userUnfetched))
df <- df[, -c(3:16)]
df$followers <- df$followers %>% as.numeric()
df$following <- df$following %>% as.numeric()
df <- df %>% arrange(desc(followers))
df <- df %>% arrange(desc(following))
ggplot(data = df) +
geom_point(mapping = aes(x = followers, y = following), position = "jitter")
```
# timer
repeat fetching user
```{r}
timer <- function(interval = 3601){
id_vec <- get_all_user_id()
fetch_users(id_vec)
later::later(timer, interval)
}
```