~khurshid-alam/gwibber/gwibber-hack

« back to all changes in this revision

Viewing changes to gwibber/microblog/plugins/qaiku/__init__.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
from gwibber.microblog import network, util
 
2
from gwibber.microblog.util import resources
 
3
from gettext import lgettext as _
 
4
 
 
5
PROTOCOL_INFO = {
 
6
  "name": "Qaiku",
 
7
  "version": "1.0",
 
8
  
 
9
  "config": [
 
10
    "private:password",
 
11
    "username",
 
12
    "color",
 
13
    "receive_enabled",
 
14
    "send_enabled",
 
15
  ],
 
16
 
 
17
  "authtype": "login",
 
18
  "color": "#729FCF",
 
19
 
 
20
  "features": [
 
21
    "send",
 
22
    "receive",
 
23
    "reply",
 
24
    "responses",
 
25
    "thread",
 
26
    "send_thread",
 
27
    "user_messages",
 
28
  ],
 
29
 
 
30
  "default_streams": [
 
31
    "receive",
 
32
    "responses",
 
33
  ],
 
34
}
 
35
 
 
36
URL_PREFIX = "http://www.qaiku.com"
 
37
 
 
38
class Client:
 
39
  def __init__(self, acct):
 
40
    self.account = acct
 
41
 
 
42
  def _common(self, data):
 
43
    m = {}; 
 
44
    m["mid"] = str(data["id"])
 
45
    m["service"] = "qaiku"
 
46
    m["account"] = self.account["id"]
 
47
    m["time"] = util.parsetime(data["created_at"])
 
48
    m["text"] = data["text"]
 
49
    m["to_me"] = ("@%s" % self.account["username"]) in data["text"]
 
50
 
 
51
    m["html"] = data["html"]
 
52
 
 
53
    # TODO: Change Qaiku's @-links to people to Gwibber-internal ones
 
54
    m["content"] = data["html"]
 
55
 
 
56
    if (data["external_url"]):
 
57
      # Qaiku posts can have external links in them, display that under the message
 
58
      m["content"] += "<p><a href=\"" + data["external_url"] + "\">" + data["external_url"] + "</a></p>"
 
59
 
 
60
    # TODO: Display picture Qaikus
 
61
 
 
62
    if "channel" in data and data["channel"]:
 
63
      # Put message's Qaiku channel as "source" so it will be displayed in the UI
 
64
      m["source"] = "<a href=\"http://www.qaiku.com/channels/show/" + data["channel"] + "/\">#" + data["channel"] + "</a>"
 
65
 
 
66
    if "in_reply_to_status_id" in data and data["in_reply_to_status_id"]:
 
67
      m["reply"] = {}
 
68
      m["reply"]["id"] = data["in_reply_to_status_id"]
 
69
      m["reply"]["nick"] = data["in_reply_to_screen_name"]
 
70
      m["reply"]["url"] = data["in_reply_to_status_url"]
 
71
 
 
72
    return m
 
73
 
 
74
  def _message(self, data):
 
75
    m = self._common(data)
 
76
    user = data["user"]
 
77
    img = user["profile_image_url"]
 
78
 
 
79
    m["sender"] = {}
 
80
    m["sender"]["name"] = user["name"]
 
81
    m["sender"]["nick"] = user["screen_name"]
 
82
    m["sender"]["id"] = user["id"]
 
83
    m["sender"]["location"] = user.get("location", "")
 
84
    m["sender"]["followers"] = user["followers_count"]
 
85
    m["sender"]["image"] = "/".join((URL_PREFIX, img)) if img[0] == "/" else img
 
86
    m["sender"]["url"] = user["url"]
 
87
    m["sender"]["is_me"] = m["sender"]["nick"] == self.account["username"]
 
88
    m["url"] = "/".join((m["sender"]["url"], "show", m["mid"]))
 
89
 
 
90
    return m
 
91
 
 
92
  def _get(self, path, parse="message", post=False, single=False, **args):
 
93
    url = "/".join((URL_PREFIX, "api", path))
 
94
    url += ("&" if "?" in url else "?") + "apikey=%s" % self.account["password"]
 
95
    data = network.Download(url, util.compact(args) or None, post).get_json()
 
96
 
 
97
    resources.dump(self.account["service"], self.account["id"], data)
 
98
 
 
99
    if single: return [getattr(self, "_%s" % parse)(data)]
 
100
    if parse: return [getattr(self, "_%s" % parse)(m) for m in data]
 
101
    else: return []
 
102
 
 
103
  def __call__(self, opname, **args):
 
104
    return getattr(self, opname)(**args)
 
105
 
 
106
  def receive(self):
 
107
    return self._get("statuses/friends_timeline.json")
 
108
 
 
109
  def user_messages(self, id=None):
 
110
    return self._get("statuses/user_timeline.json", screen_name=id)
 
111
 
 
112
  def responses(self):
 
113
    return self._get("statuses/mentions.json")
 
114
 
 
115
  def send(self, message):
 
116
    return self._get("statuses/update.json", post=True, single=True, status=message, source='gwibbernet')
 
117
 
 
118
  def send_thread(self, message, target):
 
119
    recipient = target.get("reply", {}).get("id", 0) or target.get("mid", 0)
 
120
    return self._get("statuses/update.json", post=True, single=True,
 
121
        status=message, in_reply_to_status_id=recipient, source='gwibbernet')