Source

xbl_widget / xbl_widget.py

import gtk
import cairo
import hildondesktop
from BeautifulSoup import BeautifulSoup
from mechanize import Browser
import re
import simplejson as json

FRIEND_TABLE_CLASS = 'XbcProfileTable XbcFriendsListTable'
GAMER_TAG_CLASS = 'XbcGamerTag'
GAMER_PRESENCE_CLASS = 'XbcGamerPresence'
GAMER_STATUS_CLASS = 'XbcGamerDescription'

class HelloWorldDialog(gtk.Dialog):
    def __init__(self):
        gtk.Dialog.__init__(self, "Hello World", None,
                            gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR,
                            ("Close", gtk.RESPONSE_OK))
        self.vbox.add(gtk.Label("Hello World!"))
        self.show_all()

def hello_world_dialog_show(button):
    dialog = HelloWorldDialog()
    dialog.run()
    dialog.destroy()

def GetXboxLiveFriends( login, passwd):
      """Return a list of tuples (gamer_tag, gamer_presence)."""
      br = Browser()
      br.open('http://live.xbox.com/en-US/profile/Friends.aspx')
      br.select_form(name='f1')
      br['login'] = login
      br['passwd'] = passwd
      br.submit()  # Submit login form.
      br.select_form(name='fmHF')
      response = br.submit()  # Submit redirect form.
      friend_list = response.read()
      response.close()
        
      soup = BeautifulSoup(friend_list)
      friend_table = soup.find('table', {'class': FRIEND_TABLE_CLASS})
      if friend_table is None:
        return []

      friends = []
      for row in friend_table.contents[1:]:  # Skip header row.
        if(row == "\n"):
          continue
 
        gamer_tag = row.find('td', {'class': GAMER_TAG_CLASS})
        if(gamer_tag == None):
          continue
      
        gamer_tag = str(gamer_tag.find('a').contents[0])
        gamer_presence = row.find('td', {'class': GAMER_PRESENCE_CLASS})
        gamer_presence = str(gamer_presence.contents[0]).strip()
        gamer_status = row.find('td', {'class': GAMER_STATUS_CLASS})
        gamer_status = str(gamer_status.contents[0]).strip()
        friends.append((gamer_tag, gamer_presence, gamer_status))

      return friends


class HelloHomePlugin(hildondesktop.HomePluginItem):
 
    def __init__(self):
        hildondesktop.HomePluginItem.__init__(self)
                
        self.myListStore = gtk.ListStore(str, str, str)
        self.update_list()
        
        myTreeView = gtk.TreeView(self.myListStore)
        myTreeView.set_headers_visible(True)
        myTreeView.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_VERTICAL)
        
        tvcolumn1 = gtk.TreeViewColumn('Username')
        myTreeView.append_column(tvcolumn1)
        cell1 = gtk.CellRendererText()
        tvcolumn1.pack_start(cell1, True)
        tvcolumn1.add_attribute(cell1, 'text', 0)

        tvcolumn2 = gtk.TreeViewColumn('Status')
        myTreeView.append_column(tvcolumn2)
        cell2 = gtk.CellRendererText()
        tvcolumn2.pack_start(cell2, True)
        tvcolumn2.add_attribute(cell2, 'text', 1)

        tvcolumn3 = gtk.TreeViewColumn('Description')
        myTreeView.append_column(tvcolumn3)
        cell3 = gtk.CellRendererText()
        cell3.set_property('size-points', 10)
        tvcolumn3.pack_start(cell3, True)
        tvcolumn3.add_attribute(cell3, 'text', 2)
        
        myScrolledWin = gtk.ScrolledWindow()
        myScrolledWin.set_property( 'hscrollbar-policy', gtk.POLICY_AUTOMATIC)
        myScrolledWin.add(myTreeView)
        
        self.set_default_size(550,300)
        self.add(myScrolledWin)
        
        source_id = gobject.timeout_add(5*60*1000, self.update_list)

    def update_list(self):
        self.myListStore.clear()
        fp = open("xli.dat", "r")
        xli = json.load(fp)
        fp.close()
        friends = GetXboxLiveFriends(xli["uname"], xli["passwd"])
        for friend in friends:
            self.myListStore.append([friend[0], friend[1], friend[2]])
        return True
        
hd_plugin_type = HelloHomePlugin

# The code below is just for testing purposes.
# It allows to run the widget as a standalone process.
if __name__ == "__main__":
        import gobject
        gobject.type_register(hd_plugin_type)
        obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")
        obj.show_all()
        gtk.main()