Created
November 20, 2014 01:43
-
-
Save goldeneggg/eaec87a457892ca80a79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description: | |
# Get github trending repositories | |
# | |
# Dependencies: | |
# "request": "^2.42.0" | |
# "cheerio": "^0.17.0" | |
# | |
# Configuration: | |
# | |
# Commands: | |
# hubot g tr [since ("m", "w", "d")] [lang] - Show trending repositories, since = "m": monthly, "w": weekly, "d": daily(default) | |
# | |
# Notes: | |
# | |
# Author: | |
# goldeneggg | |
request = require 'request' | |
module.exports = (robot) -> | |
trim = (str) -> | |
str.replace(/(^\s+)|(\s+$)/g, '').replace(/\r?\n/g, '') | |
robot.respond /g tr(?:\s+)?((m|w|d)\s+)?([^\s]+)?/i, (msg) -> | |
since = "daily" | |
if msg.match[2]? | |
switch msg.match[2] | |
when "m" | |
since = "monthly" | |
when "w" | |
since = "weekly" | |
l = if msg.match[3]? then "&l=#{msg.match[3]}" else "" | |
url = "https://github.com/trending?since=#{since}" + l | |
options = | |
url: url | |
method: "GET" | |
request options, (error, response, body) => | |
if error | |
console.log "A problem was caused: #{error}" | |
return | |
status = response.statusCode | |
if status == 200 | |
cheerio = require 'cheerio' | |
$ = cheerio.load body | |
m = "" | |
$('ol.repo-list > li').each (i, e) -> | |
$$ = cheerio.load e | |
repo = "https://github.com/" + $$('h3.repo-list-name > a').attr('href') | |
desc = trim $$('p.repo-list-description').text() | |
meta = trim $$('p.repo-list-meta').text() | |
star = trim meta.replace(/stars.+/g, '').split('•')[1] | |
repoInfo = "#{repo} - #{desc} (#{star} stars)" | |
m = m + "\n" + repoInfo | |
msg.send m | |
else | |
console.log "Status(#{status}) is error: #{body}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment