view plugins/plugin_misc_cs.py @ 106:138d82f64b6f

plugin CS: friends parsing
author Goffi <goffi@goffi.org>
date Sat, 26 Jun 2010 15:33:16 +0800
parents 6be927a465ed
children 5ae370c71803
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
SAT plugin for managing xep-0045
Copyright (C) 2009, 2010  Jérôme Poisson (goffi@goffi.org)

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, either version 3 of the License, or
(at your option) any later version.

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, see <http://www.gnu.org/licenses/>.
"""

from logging import debug, info, warning, error
from twisted.words.xish import domish
from twisted.internet import protocol, defer, threads, reactor
from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.protocols.jabber import error as jab_error
from twisted.words.protocols.jabber.xmlstream import IQ
from twisted.web.client import getPage
import os.path
import pdb
import random

from zope.interface import implements

from wokkel import disco, iwokkel, data_form
from tools.xml_tools import XMLUI
import urllib

from BeautifulSoup import BeautifulSoup
import re


PLUGIN_INFO = {
"name": "CouchSurfing plugin",
"import_name": "CS",
"type": "Misc",
"protocols": [],
"dependencies": [],
"main": "CS_Plugin",
"handler": "no",
"description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""")
}

AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'

class CS_Plugin():

    params = """
    <params>
    <individual>
    <category name="CouchSurfing">
        <param name="Login" type="string" />
        <param name="Password" type="password" />
    </category>
    </individual>
    </params>
    """

    def __init__(self, host):
        info(_("Plugin CS initialization"))
        self.host = host
        #parameters
        host.memory.importParams(CS_Plugin.params)
        #menu
        host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
        self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while

    def erroCB(self, e, id):
        """Called when something is going wrong when contacting CS website"""
        message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")}
        self.host.bridge.actionResult("ERROR", id, message_data)

    def menuSelected(self, id, profile):
        """Called when the couchsurfing menu item is selected"""
        login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile)
        password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile)
        if not login or not password:
            message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")}
            self.host.bridge.actionResult("ERROR", id, message_data)
            return

        post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) 
        
        if not self.data.has_key(profile):
            self.data[profile] = {'cookies':{}}
        else:
            self.data[profile]['cookies'] = {}




   
        #tmp
        f = open('/home/goffi/tmp/CS_principale.html','r')
        html = f.read()
        self.__connectionCB(html, id, profile)

        """d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=self.data[profile]['cookies'])
        d.addCallback(self.__connectionCB, id, profile)
        d.addErrback(self.erroCB, id)"""


    #self.host.bridge.actionResult("SUPPRESS", id, {})


#pages parsing callbacks
    def savePage(self, name, html):
        f = open ('/home/goffi/tmp/CS_'+name+'.html','w')
        f.write(html)
        f.close()
        print "page [%s] sauvee" % name
        #pdb.set_trace()

    def __connectionCB(self, html, id, profile):
        print 'Response received'
        self.savePage('principale',html)
        soup = BeautifulSoup(html)
        self.data[profile]['user_nick'] = soup.find('a','item_link',href='/home.html').contents[0]
        self.data[profile]['user_name'] = soup.html.head.title.string.split(' - ')[1]
        #unread messages
        try:
            self.data[profile]['unread_messages'] = int(soup.find(lambda tag: tag.name=='div' and ('class','item_bubble') in tag.attrs and tag.find('a', href="/messages.html?message_status=inbox")).find(text=True))
        except:
            self.data[profile]['unread_messages'] = 0
        #unread couchrequest messages
        try:
            self.data[profile]['unread_CR_messages'] = int(soup.find(lambda tag: tag.name=='div' and ('class','item_bubble') in tag.attrs and tag.find('a', href="/couchmanager")).find(text=True))
        except:
            self.data[profile]['unread_CR_messages'] = 0

        #if we have already the list of friend, no need to make new requests
        if not self.data[profile].has_key('friends'):
            self.data[profile]['friends'] = {}
            """f = open('/home/goffi/tmp/CS_friends.html','r')
            html = f.read()
            self.__friendsPageCB(html, id, profile)"""
            d = getPage('http://www.couchsurfing.org/connections.html?type=myfriends&show=10000', agent=AGENT, cookies=self.data[profile]['cookies'])
            d.addCallback(self.__friendsPageCB, id=id, profile=profile)
            d.addErrback(self.erroCB, id)
        else:
            self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])})

    def __buildUI(self, data):
        """Build the XML UI of the plugin
        @param data: data store for the profile"""
        user_nick = data['user_nick']
        user_name = data['user_name']
        unread_mess = data['unread_messages']
        unread_CR_mess = data['unread_CR_messages']
        friends_list = data['friends'].keys()
        friends_list.sort()
        interface = XMLUI('window','tabs')
        interface.addCategory(_("Messages"), "vertical")
        interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''})
        interface.addList(friends_list, 'friends')
        interface.addCategory(_("Events"), "vertical")
        interface.addCategory(_("Couch search"), "vertical")
        return interface.toXml()

    def __meetingPageCB(self, html):
        """Called when the meeting page has been received"""

    def __friendsPageCB(self, html, id, profile):
        """Called when the friends list page has been received"""
        self.savePage('friends',html)
        soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"'))
        friends = self.data[profile]['friends']
        for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos
            _nobr = _tr.find('nobr')  #contain the friend name
            friend_name = unicode(_nobr.string)
            friend_link = u'http://www.couchsurfing.org'+_nobr.parent['href']
            regex_href = re.compile(r'/connections\.html\?id=([^&]+)')
            a_tag = _tr.find('a',href=regex_href)
            friend_id = regex_href.search(unicode(a_tag)).groups()[0]

            debug(_("CS friend found: %(friend_name)s (id: %(friend_id)s, link: %(friend_link)s)") % {'friend_name':friend_name, 'friend_id':friend_id, 'friend_link':friend_link})
            friends[friend_name] = {'link':friend_link,'id':friend_id} 
        a = soup.find('td','barmiddle next').a  #is there several pages ?
        if a:
            #yes, we parse the next page
            d = getPage('http://www.couchsurfing.org/'+str(a['href']), agent=AGENT, cookies=self.data[profile]['cookies'])
            d.addCallback(self.__friendsPageCB, id=id, profile=profile)
            d.addErrback(self.erroCB, id)
        else:
            #no, we show the result
            self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])})
            #and save the data
            self.host.memory.setPrivate('plugin_cs_data', self.data)