start working on chat capability

This commit is contained in:
Christophe Benz 2010-05-15 21:10:33 +02:00
commit 8344824970
4 changed files with 109 additions and 17 deletions

View file

@ -23,6 +23,7 @@ from __future__ import with_statement
from datetime import datetime
from weboob.backend import BaseBackend
from weboob.capabilities.chat import ICapChat
from weboob.capabilities.messages import ICapMessages, ICapMessagesReply, Message
from weboob.capabilities.dating import ICapDating
from weboob.tools.browser import BrowserUnavailable
@ -31,7 +32,11 @@ from .browser import AdopteUnMec
from .exceptions import AdopteCantPostMail
from .optim.profiles_walker import ProfilesWalker
class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating):
__all__ = ['AuMBackend']
class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapChat):
NAME = 'aum'
MAINTAINER = 'Romain Bignon'
EMAIL = 'romain@peerfuse.org'
@ -128,3 +133,9 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating):
if self._profiles_walker:
self._profiles_walker.stop()
self._profiles_walker = None
def iter_chat_contacts(self, online=True, offline=True):
return self.browser.iter_chat_contacts(online=online, offline=offline)
def send_chat_message(self, _id, message):
return self.browser.send_chat_message(_id, message)

View file

@ -1,25 +1,25 @@
# -*- coding: utf-8 -*-
"""
Copyright(C) 2008-2010 Romain Bignon
# Copyright(C) 2008-2010 Romain Bignon, Christophe Benz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import time
from logging import warning
import random
import simplejson
from weboob.tools.browser import BaseBrowser
from weboob.tools.parsers.html5libparser import Html5libParser
@ -36,6 +36,12 @@ from weboob.backends.aum.pages.login import LoginPage, RedirectPage, BanPage, Er
from weboob.backends.aum.pages.edit import EditPhotoPage, EditPhotoCbPage, EditAnnouncePage, EditDescriptionPage, EditSexPage, EditPersonalityPage
from weboob.backends.aum.pages.wait import WaitPage
from weboob.capabilities.chat import Contact
__all__ = ['AdopteUnMec']
class AdopteUnMec(BaseBrowser):
DOMAIN = 'www.adopteunmec.com'
PROTOCOL = 'http'
@ -262,3 +268,29 @@ class AdopteUnMec(BaseBrowser):
r = result.find('<td align="right" style="font-size:12px;font-weight:bold">en ligne</td>') >= 0
print 'isSlutOnline(%s) = %s' % (id, r)
return r
def iter_chat_contacts(self, online=True, offline=True):
json = simplejson.load(self.openurl('http://www.adopteunmec.com/1.1_cht_get.php?anticache=%f' % random.random()))
if json['error']:
raise ChatException(u'Error while getting chat infos. json:\n%s' % json)
for contact in json['contacts']:
if online and contact['cat'] == 1 or offline and contact['cat'] == 3:
if contact['cat'] == 1:
online = True
elif contact['cat'] == 3:
online = False
else:
raise ChatException(u'Unknown online status: contact=%s' % contact)
yield Contact(_id=contact['id'], pseudo=contact['pseudo'], online=online, avatar_url=contact['cover'], age=contact['birthday'])
def send_chat_message(self, _id, message):
url = 'http://www.adopteunmec.com/1.1_cht_send.php?anticache=%f' % random.random()
data = dict(id=_id, message=message)
headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Accept': 'text/plain',
'Referer': 'http://www.adopteunmec.com/chat.php',
'Origin': 'http://www.adopteunmec.com',
}
print url, data, headers
return self.post_request(url, data, headers)

View file

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010 Christophe Benz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from .cap import ICap
__all__ = ['ChatException', 'ICapChat']
class ChatException(Exception):
pass
class Contact(object):
def __init__(self, _id, pseudo, online, name=None, avatar_url=None, age=None):
self.id = _id
self.pseudo = pseudo
self.online = online
self.name = name
self.avatar_url = avatar_url
self.age = age
class ICapChat(ICap):
def iter_chat_contacts(self, online=True, offline=True):
raise NotImplementedError()
def send_chat_message(self, _id, message):
raise NotImplementedError()

View file

@ -362,3 +362,8 @@ class BaseBrowser(mechanize.Browser):
self[field] = value
except ClientForm.ControlNotFoundError:
return
def post_request(self, url, data, headers):
headers['User-Agent'] = self.USER_AGENT
req = urllib2.Request(url, urllib.urlencode(data), headers)
return urllib2.urlopen(req)