Source

tuiter / replies.py

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

import oauth2 as oauth
import time
import urllib
import pynotify
import indicate
import os
import gtk
import gobject

from xml.etree import ElementTree

ROOT = os.path.abspath(os.curdir)

try:
    from settings import *
except:
    exit(1)

pynotify.init('Replies')

class Api(object):
    def __init__(self):
        # TODO: Init for public and authenticated requests
        self.consumer_key = CONSUMER_KEY
        self.consumer_secret = CONSUMER_SECRET
        self.token_key = OAUTH_TOKEN_KEY
        self.token_secret = OAUTH_TOKEN_SECRET
        self.consumer = oauth.Consumer(self.consumer_key, self.consumer_secret)
        self.token = oauth.Token(self.token_key, self.token_secret)
        self.client = oauth.Client(self.consumer, self.token)

    def get(self, url):
        resp, content = self.client.request(url, "GET")
        doc = ElementTree.fromstring(content)
        return (resp, content, doc)

    def post(self, url, data=None):
        if type(data) == dict:
            data = urllib.urlencode(data)
        resp, content = self.client.request(url, "POST", data)
        return (resp, content)

class Replies(Api):
    def __init__(self):
        super(Replies, self).__init__()

        # Inicia el servidor de mensajes
        desktop_file = os.path.join(ROOT, "replies_indicator.desktop")

        server = indicate.indicate_server_ref_default()
        server.set_type("message.im")
        server.set_desktop_file(desktop_file)

        self.indicator = indicate.Indicator()
        self.indicator.connect("user-display", self.callback_show_status)
        self.indicator.set_property('draw-attention', 'true');
        self.indicator.set_property("subtype", "im")

        # Obtener el último reply
        resp, content, doc = self.get("http://api.twitter.com/1/statuses/mentions.xml")
        text = doc.find('status/text').text
        status_id = doc.find('status/id').text
        screen_name = doc.find('status/user/screen_name').text
        profile_image_url = doc.find('status/user/profile_image_url').text

        self.notify(text, screen_name, profile_image_url, status_id)

        # Mueve el cursor de paginación
        # Busca los ultimos replies mayores a since_id
        self.since_id = status_id

        # Ejecuta fetch_replies() cada 80 segundos
        gobject.timeout_add_seconds(REFRESH_TIME, self.fetch_replies, self.indicator)

    def callback_show_status(self, indicator, timestamp):
        self.indicator.hide()
        screen_name = self.indicator.get_property("sender")
        status_id = self.indicator.get_property("status_id")

        if DEBUG:
            print "xdg-open http://twitter.com/%s/status/%s" % (screen_name, status_id)

        os.system("xdg-open http://twitter.com/%s/status/%s" % (screen_name, status_id))

    def icon_from_url(self, url):
        image, data =urllib.urlretrieve(url)
        return gtk.gdk.pixbuf_new_from_file(image)

    def notify(self, text, screen_name, profile_image_url, status_id):
        notification = pynotify.Notification("@" + screen_name, text)
        notification.set_icon_from_pixbuf(self.icon_from_url(profile_image_url))
        self.indicator.set_property("name", "Replies")
        self.indicator.set_property("sender", screen_name)
        self.indicator.set_property("body", text)
        self.indicator.set_property("status_id", status_id)
        self.indicator.set_property_time("time", time.time())
        self.indicator.show()
        notification.show()

    def fetch_replies(self, indicator):
        self.indicator.set_property_time("time", time.time())
        if DEBUG:
            print "GET http://api.twitter.com/1/statuses/mentions.xml?since_id=%s" % self.since_id
        resp, content, doc = self.get("http://api.twitter.com/1/statuses/mentions.xml?since_id=%s" % self.since_id)

        if doc.find('status/text') != None:
            text = doc.find('status/text').text
            status_id = doc.find('status/id').text
            screen_name = doc.find('status/user/screen_name').text
            profile_image_url = doc.find('status/user/profile_image_url').text

            if DEBUG:
                print "  NEW (%s) %s: %s" % (status_id, screen_name, text)

            self.since_id = status_id
            self.notify(text, screen_name, profile_image_url, status_id)



        return True

if __name__ == "__main__":
    replies = Replies()
    gtk.main()