Created
January 25, 2010 16:20
-
-
Save jefftriplett/285984 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
""" | |
A quick and dirty adaptation of Daniel's pubsubittyhub that supports mongodb to demo and scratch an itch. | |
A tiny implementation of pubsubhubbub with webhooks. | |
Not recommended at scale, but maybe fun for little projects. | |
Based off ``watercoolr`` (http://github.com/jcapote/watercoolr). | |
""" | |
__author__ = 'Daniel Lindsley' | |
__version__ = ('0', '1', '0') | |
from datetime import datetime, date | |
import httplib2 | |
import os | |
import random | |
import sqlite3 | |
import time | |
from uuid import uuid4 | |
from itty import * | |
from mongoengine import * | |
try: | |
from hashlib import sha1 | |
except ImportError: | |
from sha import sha as sha1 | |
try: | |
import json | |
except ImportError: | |
import simplejson | |
class Channel(Document): | |
id = StringField(required=True, default=uuid4) | |
name = StringField() | |
date = DateTimeField(required=True, default=datetime.now) | |
class Subscribers(Channel): | |
id = StringField(required=True, default=uuid4) | |
channel = ReferenceField(Channel) | |
url = StringField() | |
date = DateTimeField(required=True, default=datetime.now) | |
def generate_id(): | |
base = random.randint(0, 100000000) | |
salt = time.time() | |
return sha1("%s%s" % (base, salt)).hexdigest()[:32] | |
@get('/') | |
def index(request): | |
return 'Post to `/channels`, `/subscribers` or `/messages`.' | |
@post('/channels') | |
def channels(request): | |
channel = Channel() | |
channel.name = generate_id() | |
channel.save() | |
return Response(json.dumps({'id': channel.id}), content_type='application/json') | |
@post('/subscribers') | |
def subscribers(request): | |
success = False | |
raw_data = request.POST.get('data') | |
if raw_data: | |
data = json.loads(raw_data) | |
channel = Channel.objects(name='boo') | |
url = data.get('url', None) | |
if channel: | |
channel_id = channel['id'] | |
if url and channel_id: | |
subs = Subscribers.objects(channel_id=channel_id, url=url) | |
if not len(subs): | |
subscriber = Subscribers(channel_id=channel_id, url=url) | |
success = True | |
return Response(json.dumps({'status': success}), content_type='application/json') | |
@post('/messages') | |
def messages(request): | |
success = False | |
raw_data = request.POST.get('data') | |
if raw_data: | |
data = json.loads(raw_data) | |
channel = data.get('channel', 'boo') | |
message = data.get('message', None) | |
channel = Channel.objects(name=channel).first() | |
if channel: | |
channel_id = channel['id'] | |
if message and channel_id: | |
subs = Subscribers.objects(channel_id=channel_id) | |
for sub in subs: | |
success = True | |
http = httplib2.Http(timeout=10) | |
resp, content = http.request(sub['url'], "POST", body=message, headers={'content-type':'text/plain'} ) | |
return Response(json.dumps({'status': success}), content_type='application/json') | |
connect('pubsubittymongohub') | |
run_itty() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment