-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (119 loc) · 3.91 KB
/
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
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
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var bodyParser = require('body-parser')
var eventEmitter = require('events').EventEmitter
var app = express();
app.use(express.static(__dirname + '/static'))
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
var ee;
// loads index page
app.get('/', function(req, res) {
console.log("GET " + req.url)
res.sendFile(__dirname + '/index.html')
});
var results, urlsToVisit
var baseurl = "https://github.com/", repoTab = "?tab=repositories", folwTab = "?tab=following"
app.post('/', function(req, res) {
urlsToVisit = []
results = []
ee = new eventEmitter
var search = req.body.name
console.log("POST '/' username: " + search)
urlsToVisit.push(baseurl + search + repoTab)
getFollowers(baseurl + search + folwTab)
// executes when all followers are fetched
ee.on('followers fetched', function() {
getRepoInfo()
})
ee.on('repos fetched', function() {
console.log(" \x1b[32m%s\x1b[0m", "Crawling completed")
res.send(["success", results])
})
ee.on('error occured', function(error) {
res.send(["failed", error])
return
})
})
function getRepoInfo() {
var count = urlsToVisit.length
urlsToVisit.forEach(function(url) {
request(url, function(error, response, html) {
//fs.readFile(__dirname + '/ymotongpoo.html', function(error,html) {
var repos = [], fname, uname
if (error) {
console.log(" \x1b[31m%s\x1b[0m", "Error : " + error)
ee.emit('error occured', error)
} else {
var $ = cheerio.load(html)
fname = $('h1 > .vcard-fullname').text()
uname = $('h1 > .vcard-username').text()
console.log(" Fetching " + fname + "'s repositories...")
$('.js-repo-list').filter(function() {
$(this).children().each(function() {
var data = $(this).find('h3 > a')
var repo_name = $(data).text().replace(/\s+/g, ' ')
var repo_link = $(data).attr('href')
var description = $(this).find('.d-table-cell.col-6').children().last().text().replace(/\s+/g, ' ')
var language = $(this).find('.d-table-cell.col-2').children().last().text().replace(/\s+/g, ' ')
var stars = $(this).find('.d-table-cell.col-1 > a').text().replace(/\s+/g, ' ')
repos.push({
"name" : repo_name,
"link" : "https://github.com" + repo_link,
"description" : description,
"language" : language,
"stars" : stars
})
})
})
console.log(" \x1b[32m%s\x1b[0m", "Successfully fetched " + fname + "'s repositories!")
results.push(
{
"fullname" : fname,
"username" : uname,
"repos" : repos
}
);
if(!--count) {
ee.emit('repos fetched')
}
}
})
console.log("visiting \x1b[33m%s\x1b[0m", url)
})
}
function getFollowers(url) {
console.log("Requesting \x1b[33m%s\x1b[0m", url)
request(url, function(error, response, html) {
//fs.readFile(__dirname + '/following.html', function(error, html) {
if (error) {
console.log(" \x1b[31m%s\x1b[0m", "Error : " + error)
ee.emit('error occured', error)
} else {
var $ = cheerio.load(html), followings = []
$('.js-repo-filter').filter(function() {
$(this).children('.col-12').each(function() {
var names = $(this).find('.col-9 > a').children()
followings.push({
username: $(names).last().text().replace(/\s+/g, ' ')
//fullname: $(names).first().text().replace(/\s+/g, ' ')
})
})
})
console.log("Followers are :")
followings.forEach(function(follower) {
// var repos = getRepoInfo("https://github.com/" + follower.username + "?tab=repositories")
urlsToVisit.push(baseurl + follower.username + repoTab)
console.log("\x1b[2m >> %s\x1b[0m", follower.username)
})
ee.emit('followers fetched')
}
})
}
var server = app.listen(3000, function() {
console.log('Server running at localhost:%s', server.address().port);
});