Skip to content

Instantly share code, notes, and snippets.

@spro
Created June 1, 2014 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spro/a1d26dcc093da18dc54e to your computer and use it in GitHub Desktop.
Save spro/a1d26dcc093da18dc54e to your computer and use it in GitHub Desktop.
fs = require 'fs'
util = require 'util'
exec = require('child_process').exec
jsdom = require 'jsdom'
jquery = require 'jquery'
_ = require 'underscore'
# Github APIs require a user agent to be set, so set one by default
request = require('request')
.defaults headers: 'user-agent': "spro's rainforest challenger"
# Helper for getting a random item from a list
randomChoice = (l) -> l[Math.floor(l.length * Math.random())]
# The Gists API unfortunately doesn't have a search endpoint, so let's approximate
# one by parsing their search page (looking only for python files for simplicity).
searchGistsUrl = (q) -> "https://gist.github.com/search?l=python&q=#{ q }"
searchGists = (q, cb) ->
request.get searchGistsUrl(q), (err, res, body) ->
console.log 'Parsing search results...'
jsdom.env body, (err, window) ->
$ = jquery window
gist_ids = $('.gist .byline a').map(-> $(@).attr('href'))
.get().filter((h) -> h.match /^\/\w+\/\w+/)
.map((h) -> h.split('/')[2])
cb null, gist_ids
# Get a Gist's content from the Gist API given its ID
getGistUrl = (id) -> "https://api.github.com/gists/#{ id }"
getGist = (id, cb) ->
request.get {url: getGistUrl(id), json: true}, (err, res, data) ->
cb null, randomChoice(_.values(data.files)).content
# Save and execute a python file (less than ideal but I couldn't figure
# out how to pipe directly into `exec`)
saveAndExecGist = (id, content, cb) ->
filename = "gist_#{ id }.py"
fs.writeFile filename, content, (err, wrote) ->
console.log "Wrote #{ filename }, running..."
exec "python2 #{ filename }", (err, stdout, stderr) ->
cb (err || stderr), stdout
# Go forth and run the pipeline
searchGists 'letsrevolutionizetesting.com', (err, gist_ids) ->
console.log "Got Gist IDs: #{ gist_ids }"
gist_id = randomChoice gist_ids
console.log "Reading Gist #{ gist_id }"
getGist gist_id, (err, gist_content) ->
console.log "Got Gist #{ gist_id }'s content: #{ gist_content.slice(0, 25) }..."
saveAndExecGist gist_id, gist_content, (err, stdout) ->
if err
console.log "Execution error:"
console.log err
else
console.log "Execution complete!"
console.log stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment