4
Jaiku interface for Gwibber
5
SegPhault (Ryan Paul) - 01/05/2008
10
import urllib2, urllib, re, simplejson, urlparse
34
NONCE_PARSE = re.compile('.*_nonce" value="([^"]+)".*', re.M | re.S)
35
LINK_MARKUP_PARSE = re.compile("\[([^\]]+)\]\(([^)]+)\)")
38
def __init__(self, client, data):
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"]
48
# Jaiku's timestamp format is lousy
49
d, t = data["created_at"].split("T")
50
self.time = support.parse_time(d + t.replace("-", ":"))
54
self.text = data["title"]
55
#self.html_string = LINK_MARKUP_PARSE.sub('<a href="\\2">\\1</a>', self.text.replace(
56
# "&", "&").replace("<", "<").replace(">", ">"))
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)
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
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
# "&", "&").replace("<", "<").replace(">", ">")))
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))
84
if "comment_id" in data:
85
self.id = data["comment_id"]
86
else: self.id = data["id"]
89
def __init__(self, acct):
92
def send_enabled(self):
93
return self.account["send_enabled"] and \
94
self.account["username"] != None and \
95
self.account["private:password"] != None
97
def receive_enabled(self):
98
return self.account["receive_enabled"] and \
99
self.account["username"] != None and \
100
self.account["private:password"] != None
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())
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())
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)
121
for data in self.get_messages()["stream"]:
122
if "id" in data: yield Message(self, data)
123
else: yield Comment(self, data)
125
def send_thread(self, message, target):
126
print urllib2.urlopen(urllib2.Request("http://api.jaiku.com/json",
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"],
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()