<commit_before>"""
Campfire API implementation in Python

The API is described at http://developer.37signals.com/campfire/index
"""

__author__ = "<NAME> <<EMAIL>>"
__data__ = [ 'Campfire', 'CampfireRoom' ]

import urllib2

class Campfire(object):
    def __init__(self, url, token):
        self.url = url
        self.token = token
        passwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passwd_manager.add_password(None, uri=self.url, user=self.token, passwd='X')
        auth_handler = urllib2.HTTPBasicAuthHandler(passwd_manager)
        self.url_opener = urllib2.build_opener(auth_handler)

    def get(self, path):
        return self.url_opener.open(self.url + path).read()

    def put(self, path, data=''):
        request = urllib2.Request(self.url + path, data)
        request.add_header('Content-Type', 'application/xml')
        return self.url_opener.open(request).read()

    def rooms(self):
        return self.get('/rooms.xml')

    def room(self, room_id):
        return CampfireRoom(self, room_id)

    def user(self, user_id):
        return self.get('/users/%s.xml' % user_id)

    def me(self):
        return self.get('/users/me.xml')

    def presence(self):
        return self.get('/presence.xml')

    def search(self, term):
        return self.get('/search/%s.xml' % term)


class CampfireRoom(object):
    def __init__(self, campfire, room_id):
        self.campfire = campfire
        self.room_id = room_id

    def get(self, path):
        return self.campfire.get('/room/%s%s' % (self.room_id, path))

    def put(self, path, data=''):
        return self.campfire.put('/room/%s%s' % (self.room_id, path), data)

    def show(self):
        return self.get('.xml')

    # FIXME
    def update(self, name='', topic=''):
        return self.put('.xml', '<room><name>%s</name><topic>%s</topic></room>' % (name, topic))

    def recent(self):
        return self.get('/recent.xml')

    def transcript(self):
        return self.get('/transcript.xml')

    def uploads(self):
        return self.get('/uploads.xml')

    def join(self):
        return self.put('/join.xml')

    def leave(self):
        return self.put('/leave.xml')

    def lock(self):
        return self.put('/lock.xml')

    def unlock(self):
        return self.put('/unlock.xml')

    def speak(self, message, type='TextMessage'):
        return self.put('/speak.xml', '<message><body>%s</body><type>%s</type></message>' % (message, type))

    def paste(self, message):
        return self.speak(message, 'PasteMessage')<commit_msg>Decode and encode messages using simplejson<commit_after>"""
Campfire API implementation in Python

The API is described at http://developer.37signals.com/campfire/index
"""

__author__ = "<NAME> <<EMAIL>>"
__data__ = [ 'Campfire', 'CampfireRoom' ]

import urllib2
import simplejson as json

class Campfire(object):
    def __init__(self, url, token):
        self.url = url
        self.token = token
        passwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passwd_manager.add_password(None, uri=self.url, user=self.token, passwd='X')
        auth_handler = urllib2.HTTPBasicAuthHandler(passwd_manager)
        self.url_opener = urllib2.build_opener(auth_handler)

    def get(self, path):
        response = self.url_opener.open(self.url + path).read()
        return json.loads(response)

    def put(self, path, data=''):
        request = urllib2.Request(self.url + path, data)
        request.add_header('Content-Type', 'application/json')
        response = self.url_opener.open(request).read().strip()
        return json.loads(response) if len(response) else None

    def rooms(self):
        return self.get('/rooms.json')['rooms']

    def room(self, room_id):
        return CampfireRoom(self, room_id)

    def user(self, user_id):
        return self.get('/users/%s.json' % user_id)['user']

    def me(self):
        return self.get('/users/me.json')['user']

    def presence(self):
        return self.get('/presence.json')['rooms']

    def search(self, term):
        return self.get('/search/%s.json' % term)['messages']


class CampfireRoom(object):
    def __init__(self, campfire, room_id):
        self.campfire = campfire
        self.room_id = room_id

    def get(self, path):
        return self.campfire.get('/room/%s%s' % (self.room_id, path))

    def put(self, path, data=''):
        return self.campfire.put('/room/%s%s' % (self.room_id, path), data)

    def show(self):
        return self.get('.json')['room']

    # FIXME
    def update(self, name='', topic=''):
        data = { 'room': { 'name': name, 'topic': topic } }
        return self.put('/speak.json', json.dumps(data))

    def recent(self):
        return self.get('/recent.json')['messages']

    def transcript(self):
        return self.get('/transcript.json')['messages']

    def uploads(self):
        return self.get('/uploads.json')['uploads']

    def join(self):
        return self.put('/join.json')

    def leave(self):
        return self.put('/leave.json')

    def lock(self):
        return self.put('/lock.json')

    def unlock(self):
        return self.put('/unlock.json')

    def speak(self, message, type='TextMessage'):
        data = { 'message': { 'body': message, 'type': type } }
        return self.put('/speak.json', json.dumps(data))['message']

    def paste(self, message):
        return self.speak(message, 'PasteMessage')
{'fraction_non_alphanumeric': 0.15780761449328673, 'fraction_numerical': 0.0025749494206363803, 'mean_word_length': 2.9462989840348333, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '284887', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1730, 'n_words': 366}