~khurshid-alam/gwibber/gwibber-hack

« back to all changes in this revision

Viewing changes to gwibber/microblog/jaiku.py

  • Committer: Khurshid Alam
  • Date: 2012-04-06 14:38:38 UTC
  • Revision ID: khurshid.alam@linuxmail.org-20120406143838-nz7hjg8vtzi2wl7i
initial revision

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