Skip to content

Commit

Permalink
Adds incomplete User and Room, and a failing test setting some goals.
Browse files Browse the repository at this point in the history
        me = client.get_me()
        sandbox = client.get_room(11540)

        with sandbox.messages() as messages:
            sandbox.send_message("hello worl")

            for message in messages:
                if message.owner is me:
                    assert my_message.content == "hello worl"

ref #65, ref #52, ref #43
  • Loading branch information
jeremyBanks committed May 14, 2014
1 parent 41cc50d commit a572d36
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 16 deletions.
4 changes: 4 additions & 0 deletions chatexchange/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from . import browser

from . import users
from . import messages
from . import rooms
from . import events
from . import client


Browser = browser.Browser

Client = client.Client
13 changes: 10 additions & 3 deletions chatexchange/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ class Client(object):
max_recent_events = 1000
max_recently_accessed_messages = 1000

def __init__(self, host='stackexchange.com'):
def __init__(self, host='stackexchange.com', email=None, password=None):
self.logger = logger.getChild('SEChatWraper')

if email or password:
assert email and password, (
"must specify both email and password or neither")

# any known Message instances
self._messages = weakref.WeakValueDictionary()

Expand All @@ -49,6 +53,9 @@ def __init__(self, host='stackexchange.com'):
self.thread = threading.Thread(target=self._worker, name="message_sender")
self.thread.setDaemon(True)

if email:
self.login(email, password)

def get_message(self, message_id):
message = self._messages.setdefault(
message_id, messages.Message(message_id, self))
Expand All @@ -74,11 +81,11 @@ def get_message(self, message_id):
'SO': 'stackexchange.com'
}

def login(self, username, password):
def login(self, email, password):
assert not self.logged_in
self.logger.info("Logging in.")

self.br.loginSEOpenID(username, password)
self.br.loginSEOpenID(email, password)

self.br.loginSite(self.host)

Expand Down
4 changes: 2 additions & 2 deletions chatexchange/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def scrape_history(self):

if not has_editor_name:
has_editor_name = True
user_soup = item.select('.username a')[0]
user_soup = item.select('.email a')[0]
self.editor_user_id = self._user_id_from_user_link(user_soup)
self.editor_user_name = user_soup.text

Expand Down Expand Up @@ -114,7 +114,7 @@ def scrape_transcript(self):
monologues_soups = transcript_soup.select(
'#transcript .monologue')
for monologue_soup in monologues_soups:
user_link, = monologue_soup.select('.signature .username a')
user_link, = monologue_soup.select('.signature .email a')
user_id = self._user_id_from_user_link(user_link)
user_name = user_link.text

Expand Down
22 changes: 22 additions & 0 deletions chatexchange/rooms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging

from . import _utils


logger = logging.getLogger(__name__)


class Room(object):
def __init__(self, id, client):
self.logger = logger.getChild('Room')
self.id = id
self.client = client

name = _utils.LazyFrom('scrape_info')
description = _utils.LazyFrom('scrape_info')
message_count = _utils.LazyFrom('scrape_info')
user_count = _utils.LazyFrom('scrape_info')
parent_site_name = _utils.LazyFrom('scrape_info')

def scrape_info(self):
raise NotImplementedError()
21 changes: 21 additions & 0 deletions chatexchange/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging

from . import _utils


logger = logging.getLogger(__name__)


class User(object):
def __init__(self, id, client):
self.logger = logger.getChild('User')
self.id = id
self.client = client

name = _utils.LazyFrom('scrape_profile')
about = _utils.LazyFrom('scrape_profile')
is_moderator = _utils.LazyFrom('scrape_profile')
parent_user_url = _utils.LazyFrom('scrape_profile')

def scrape_profile(self):
raise NotImplementedError()
6 changes: 3 additions & 3 deletions examples/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ def main():
room_id = '14219' # Charcoal Chatbot Sandbox

if 'ChatExchangeU' in os.environ:
username = os.environ['ChatExchangeU']
email = os.environ['ChatExchangeU']
else:
sys.stderr.write("Username: ")
sys.stderr.flush()
username = raw_input()
email = raw_input()
if 'ChatExchangeP' in os.environ:
password = os.environ['ChatExchangeP']
else:
password = getpass.getpass("Password: ")

client = chatexchange.client.Client(host_id)
client.login(username, password)
client.login(email, password)

client.joinRoom(room_id)
client.watchRoom(room_id, on_message, 1)
Expand Down
36 changes: 36 additions & 0 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
from getpass import getpass
import logging

import chatexchange
from chatexchange.events import MessageEdited


logging.basicConfig(level=logging.DEBUG)

email = raw_input('Email: ')
password = getpass()
client = chatexchange.Client('stackexchange.com', email, password)

me = client.get_me()
sandbox = client.get_room(11540)
my_message = None

with sandbox.messages() as messages:
sandbox.send_message("hello worl")

for message in messages:
if message.owner is me:
my_message = message
assert my_message.content == "hello worl"
print "message sent successfully"
break

with sandbox.events(MessageEdited) as edits:
my_message.edit("hello world")

for edit in edits:
if edit.message is my_message:
assert my_message.content == "hello world"
print "message edited successfully"
break
6 changes: 3 additions & 3 deletions examples/web_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ def main(port='8462'):
room_id = 14219 # Charcoal Chatbot Sandbox

if 'ChatExchangeU' in os.environ:
username = os.environ['ChatExchangeU']
email = os.environ['ChatExchangeU']
else:
sys.stderr.write("Username: ")
sys.stderr.flush()
username = raw_input()
email = raw_input()
if 'ChatExchangeP' in os.environ:
password = os.environ['ChatExchangeP']
else:
password = getpass.getpass("Password: ")

chat = client.Client('stackexchange.com')
chat.login(username, password)
chat.login(email, password)

httpd = Server(
('127.0.0.1', 8462), Handler, chat=chat, room_id=room_id)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name='ChatExchange',
version='0.0.1a0dev1',
version='0.0.1a1dev1',
url='https://github.com/Manishearth/ChatExchange',
packages=[
'chatexchange'
Expand Down
2 changes: 1 addition & 1 deletion test/live_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
if (os.environ.get('ChatExchangeU') and
os.environ.get('ChatExchangeP')):
enabled = True
username = os.environ['ChatExchangeU']
email = os.environ['ChatExchangeU']
password = os.environ['ChatExchangeP']
2 changes: 1 addition & 1 deletion test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_se_message_echo(host_id, room_id):

client = Client(host_id)
client.login(
live_testing.username,
live_testing.email,
live_testing.password)

timeout_duration = 60
Expand Down
1 change: 0 additions & 1 deletion test/test_messages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

from chatexchange import Client
import chatexchange

import live_testing

Expand Down
2 changes: 1 addition & 1 deletion test/test_openid_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ def test_openid_login_recognizes_failure():
invalid_password = 'no' + 't' * len(live_testing.password)

browser.loginSEOpenID(
live_testing.username,
live_testing.email,
invalid_password)
33 changes: 33 additions & 0 deletions test/test_rooms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import chatexchange
from chatexchange.events import MessageEdited

import live_testing


if live_testing:
def test_room_iterators():
client = chatexchange.Client(
'stackexchange.com', live_testing.email, live_testing.password)

me = client.get_me()
sandbox = client.get_room(11540)
my_message = None

with sandbox.messages() as messages:
sandbox.send_message("hello worl")

for message in messages:
if message.owner is me:
my_message = message
assert my_message.content == "hello worl"
break

with sandbox.events(MessageEdited) as edits:
my_message.edit("hello world")

for edit in edits:
assert isinstance(edit, MessageEdited)

if edit.message is my_message:
assert my_message.content == "hello world"
break

0 comments on commit a572d36

Please sign in to comment.