~ubuntu-branches/ubuntu/precise/gwibber/precise-proposed

« back to all changes in this revision

Viewing changes to gwibber/microblog/jaiku.py

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2009-02-20 14:45:57 UTC
  • Revision ID: james.westby@ubuntu.com-20090220144557-xmhe71jdn1mf7yo9
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""
 
3
 
 
4
Jaiku interface for Gwibber
 
5
SegPhault (Ryan Paul) - 01/05/2008
 
6
 
 
7
"""
 
8
 
 
9
import urllib2, urllib, support, re, can, simplejson, urlparse
 
10
 
 
11
PROTOCOL_INFO = {
 
12
  "name": "Jaiku",
 
13
  "version": 0.1,
 
14
  
 
15
  "config": [
 
16
    "private:password",
 
17
    "username",
 
18
    "message_color",
 
19
    "comment_color",
 
20
    "receive_enabled",
 
21
    "send_enabled"
 
22
  ],
 
23
 
 
24
  "features": [
 
25
    can.SEND,
 
26
    can.RECEIVE,
 
27
    can.REPLY,
 
28
    can.THREAD,
 
29
    can.THREAD_REPLY,
 
30
  ],
 
31
}
 
32
 
 
33
NONCE_PARSE = re.compile('.*_nonce" value="([^"]+)".*', re.M | re.S)
 
34
LINK_MARKUP_PARSE = re.compile("\[([^\]]+)\]\(([^)]+)\)")
 
35
 
 
36
class Message:
 
37
  def __init__(self, client, data):
 
38
    self.client = client
 
39
    self.account = client.account
 
40
    self.protocol = client.account["protocol"]
 
41
    self.username = client.account["username"]
 
42
    if data.has_key("id"): self.id = data["id"]
 
43
    self.sender = "%s %s" % (data["user"]["first_name"], data["user"]["last_name"])
 
44
    self.sender_nick = data["user"]["nick"]
 
45
    self.sender_id = data["user"]["nick"]
 
46
    self.time = support.parse_time(data["created_at"])
 
47
    self.text = ""
 
48
    if data.has_key("title"):
 
49
      self.text = data["title"]
 
50
      #self.html_string = LINK_MARKUP_PARSE.sub('<a href="\\2">\\1</a>', self.text.replace(
 
51
      #  "&", "&amp;").replace("<", "&lt;").replace(">", "&gt;"))
 
52
    self.image = data["user"]["avatar"]
 
53
    self.bgcolor = "message_color"
 
54
    self.url = data["url"]
 
55
    self.profile_url = "http://%s.jaiku.com" % data["user"]["nick"]
 
56
    if data.has_key("icon") and data["icon"] != "": self.icon = data["icon"]
 
57
    self.can_thread = True
 
58
    self.is_reply = re.compile("@%s[\W]+|@%s$" % (self.username, self.username)).search(self.text) or \
 
59
      (urlparse.urlparse(self.url)[1].split(".")[0].strip() == self.username and \
 
60
        self.sender_nick != self.username)
 
61
 
 
62
class Comment(Message):
 
63
  def __init__(self, client, data):
 
64
    Message.__init__(self, client, data)
 
65
    self.text = data["content"]
 
66
    self.bgcolor = "comment_color"
 
67
    self.is_comment = True
 
68
 
 
69
    self.text = data["content"]
 
70
    #self.html_string = support.LINK_PARSE.sub('<a href="\\1">\\1</a>', LINK_MARKUP_PARSE.sub('<a href="\\2">\\1</a>', self.text.replace(
 
71
    #  "&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")))
 
72
 
 
73
    if data.has_key("entry_title"):
 
74
      self.original_title = data["entry_title"]
 
75
      self.title = "<small>Comment by</small> %s <small>on %s</small>" % (
 
76
        self.sender, support.truncate(data["entry_title"],
 
77
          client.account["comment_title_length"] or 20))
 
78
 
 
79
    if data.has_key("comment_id"):
 
80
      self.id = data["comment_id"]
 
81
    else: self.id = data["id"]
 
82
 
 
83
class Client:
 
84
  def __init__(self, acct):
 
85
    self.account = acct
 
86
 
 
87
  def send_enabled(self):
 
88
    return self.account["send_enabled"] and \
 
89
      self.account["username"] != None and \
 
90
      self.account["private:password"] != None
 
91
 
 
92
  def receive_enabled(self):
 
93
    return self.account["receive_enabled"] and \
 
94
      self.account["username"] != None and \
 
95
      self.account["private:password"] != None
 
96
 
 
97
  def get_messages(self):
 
98
    return simplejson.loads(urllib2.urlopen(urllib2.Request(
 
99
      "http://%s.jaiku.com/contacts/feed/json" % self.account["username"],
 
100
        urllib.urlencode({"user": self.account["username"],
 
101
          "personal_key":self.account["private:password"]}))).read())
 
102
 
 
103
  def get_thread_data(self, msg):
 
104
    return simplejson.loads(urllib2.urlopen(urllib2.Request(
 
105
      "%s/json" % ("#" in msg.url and msg.url.split("#")[0] or msg.url),
 
106
        urllib.urlencode({"user": self.account["username"],
 
107
          "personal_key":self.account["private:password"]}))).read())
 
108
 
 
109
  def get_thread(self, msg):
 
110
    thread_content = self.get_thread_data(msg)
 
111
    yield Message(self, thread_content)
 
112
    for data in thread_content["comments"]:
 
113
      yield Comment(self, data)
 
114
 
 
115
  def receive(self):
 
116
    for data in self.get_messages()["stream"]:
 
117
      if data.has_key("id"): yield Message(self, data)
 
118
      else: yield Comment(self, data)
 
119
 
 
120
  def get_nonce(self, msg):
 
121
    try:
 
122
      page = urllib2.urlopen(urllib2.Request(
 
123
        ("#" in msg.url and msg.url.split("#")[0] or msg.url),
 
124
          urllib.urlencode({"user": self.account["username"], 
 
125
            "personal_key":self.account["private:password"]}))).read()
 
126
      
 
127
      return NONCE_PARSE.match(page, 1).group(1)
 
128
    except: return None
 
129
 
 
130
  def send_thread(self, msg, message):
 
131
    nonce = self.get_nonce(msg)
 
132
    if nonce:
 
133
      return urllib2.urlopen(urllib2.Request(
 
134
        ("#" in msg.url and msg.url.split("#")[0] or msg.url),
 
135
          urllib.urlencode({"user": self.account["username"], "_nonce": nonce, 
 
136
            "personal_key":self.account["private:password"], "comment": message}))).read()
 
137
 
 
138
  def send(self, message):
 
139
    return urllib2.urlopen(urllib2.Request(
 
140
      "http://api.jaiku.com/json", urllib.urlencode({"user": self.account["username"],
 
141
      "personal_key":self.account["private:password"],
 
142
      "message": message, "method": "presence.send"}))).read()
 
143