概要
Awesome-XXから、githubのリンクをたどって、スター数を取る方法を書いておきます。
Tips
Nodeのライブラリに、cheerioというライブラリがあります。
XMLやHTMLのパースに、大変便利です。
そのライブラリの友達に、cheerio-httpcliというものがあって、URLからhtmlを取ってこれるので、この2つを使ってごにょごにょすれば、30分もあればスクリプトが出来上がりです。
手順
- たとえば、https://github.com/avelino/awesome-go にアクセスし、ページのソースを出して保存します。
- github.comで始まるaタグを取得して、そのURLをcheerio-httpcliでパースします。
- githubのスターは、「social-count js-social-count」というCSSクラス名で書かれているため、それを取得したら終わりです。
ソース
github-star-gather.js
const fs = require('fs')
const che = require('cheerio')
const client = require('cheerio-httpcli')
try {
if( process.argv.length !== 3 ) {
console.log('github-star-gather.js 使用方法\n')
console.log(' node github-star-gather.js <awesome-html-path>\n')
process.exit()
}
const filePath = process.argv[2]
const body = fs.readFileSync(filePath, 'utf-8')
dollar = che.load(body);
dollar("li").each(function(i0, e0) {
const arefs = dollar(this).children("a")
arefs.each(function(i1,e1){
const href = dollar(this).attr("href")
if( href.startsWith("https://github.com") === true ) {
const name = dollar(this).text()
const result = client.fetchSync(href)
const $ = result.$
$(".social-count.js-social-count").each(function(i2,e2) {
const stars = $(this).text().replace(/¥n/g, "").replace(/¥r/g,"").trim()
console.log(name + " " + stars + " " + href)
})
return
}
})
})
} catch(err) {
console.log(err.message)
}
process.exit()
さいごに
くぅー疲れました。
Markdownの表にして、エクセルに★を出す式を書くところがめんどうでした。