-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfetcher.js
More file actions
52 lines (37 loc) · 1.07 KB
/
fetcher.js
File metadata and controls
52 lines (37 loc) · 1.07 KB
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
const request = require('request')
const cheerio = require('cheerio')
const GITHUB_URL = 'https://github.com/search?q=stars%3a%3E1&s=stars&type=Repositories'
const DOM_SELECTOR_TOP_10_LANGS = '.filter-list .filter-item'
const DOM_SELECTOR_TOP_10_REPOS = '.repo-list .repo-list-item'
function fetcher(cb){
request(GITHUB_URL, function(err,resp,data){
if(err) return cb(err)
var d = _crawlDOMLangs(data)
cb && cb(null, d ) // yolo
})
}
// Crawl the DOM to extract the languages...
function _crawlDOMLangs(dom){
var $ = cheerio.load(dom)
, anchors = $(DOM_SELECTOR_TOP_10_LANGS)
, results = []
;
$(anchors).each(function(i, link){
var o = {
count: 0,
language: ''
}
var copy = _splitDOMCopy($(link).text())
o.count = copy[0]
o.language = copy[1]
results.push( o )
})
return results
}
// Format the copy from the DOM to it is JSON friendly.
function _splitDOMCopy(copy){
// embarrassing hack...fix this
var c = copy.replace(/\n/g, '').replace(/^\s+/g, "").replace(/\s+/g, ":")
return c.split(':')
}
exports = module.exports = fetcher;