Created
October 7, 2012 17:48
-
-
Save squiddy/3849057 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
import functools | |
import httplib | |
import json | |
import threading | |
import time | |
import urllib | |
from wsgiref.simple_server import make_server | |
from irc.client import SimpleIRCClient | |
def shorten_urls(urls): | |
conn = httplib.HTTPConnection("git.io") | |
headers = {"Content-type": "application/x-www-form-urlencoded", | |
"Accept": "text/plain"} | |
shortened = [] | |
for url in urls: | |
params = urllib.urlencode({'url': url}) | |
conn = httplib.HTTPConnection("git.io") | |
conn.request("POST", "/create", params, headers) | |
response = conn.getresponse() | |
shortened.append(response.read()) | |
return shortened | |
def send_update(payload): | |
send = functools.partial(bot.connection.privmsg, '#squiddies') | |
branch = payload['ref'].split('/')[-1] | |
short_url = shorten_urls(c['url'] for c in payload['commits']) | |
for c, url in zip(payload['commits'], short_url): | |
message = c['message'].splitlines()[0] | |
send("uh: %s %s * r%s / : %s - http://git.io/%s" % (c['author']['name'], branch, c['id'][:6], message, url)) | |
time.sleep(0.3) | |
def app(environ, start_response): | |
body_size = int(environ['CONTENT_LENGTH']) | |
body = environ['wsgi.input'].read(body_size) | |
send_update(json.loads(urllib.unquote(body[8:]))) | |
status = '200 OK' | |
headers = [('Content-type', 'text/plain')] | |
start_response(status, headers) | |
return [] | |
class Bot(SimpleIRCClient): | |
def on_welcome(self, connection, event): | |
connection.join('#squiddies') | |
httpd = make_server('', 5000, app) | |
http_thread = threading.Thread(target=httpd.serve_forever) | |
bot = Bot() | |
bot.connect('irc.freenode.net', 6667, 'uh-github') | |
bot_thread = threading.Thread(target=bot.start) | |
http_thread.start() | |
bot_thread.start() | |
bot_thread.join() | |
http_thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
irc bot that notifies channel when changes are pushed to github. quickly hacked together replacement for the cia.vc services. public domain