Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created January 28, 2013 14:07
Show Gist options
  • Save simon-engledew/4655764 to your computer and use it in GitHub Desktop.
Save simon-engledew/4655764 to your computer and use it in GitHub Desktop.
cow-say for git post-receive and campfire
#!/usr/bin/env python
import json
import requests
import json
import commands
import pipes
import sys
import glob
import os
import random
singleton = lambda fn: fn()
@singleton
def config():
with open(os.path.expanduser('~/.campfire'), 'r') as handle:
return json.load(handle)
cow_formats = [os.path.splitext(os.path.basename(path))[0] for path in glob.glob('/usr/share/cowsay/cows/*.cow')]
cow_format = lambda: random.choice(cow_formats)
oldrev, newrev, refname = sys.stdin.read().split()
for message in (message.strip() for message in commands.getoutput("git log --format='[%an] %s' {0}..{1}".format(oldrev, newrev)).split('\n')):
message = json.dumps({
'message': {
'body': commands.getoutput('/usr/games/cowsay -f {0} {1}'.format(pipes.quote(cow_format()), pipes.quote(message))),
'type': 'PasteMessage'
}
})
requests.post(
'https://{user}.campfirenow.com/room/{room}/speak.json'.format(**config),
data=message,
headers={'Content-Type': 'application/json'},
auth=(config['token'], 'X'),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment