~khurshid-alam/gwibber/gwibber-hack

« back to all changes in this revision

Viewing changes to gwibber/microblog/brightkite.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
BrightKite interface for Gwibber
 
4
SegPhault (Ryan Paul) - 10/19/2008
 
5
 
 
6
"""
 
7
 
 
8
from . import support
 
9
import urllib2, urllib, base64, re, simplejson
 
10
from xml.dom import minidom
 
11
 
 
12
PROTOCOL_INFO = {
 
13
  "name": "BrightKite",
 
14
  "version": 0.2,
 
15
  
 
16
  "config": [
 
17
    "password",
 
18
    "username",
 
19
    "message_color",
 
20
    "receive_enabled",
 
21
    "send_enabled"
 
22
  ],
 
23
 
 
24
  "features": [
 
25
    "receive",
 
26
    "responses",
 
27
    "thread",
 
28
  ],
 
29
}
 
30
 
 
31
NICK_PARSE = re.compile("@([A-Za-z0-9]+)")
 
32
 
 
33
class Message:
 
34
  def __init__(self, client, data):
 
35
    self.client = client
 
36
    self.account = client.account
 
37
    self.service = client.account["service"]
 
38
    self.username = client.account["username"]
 
39
 
 
40
    self.sender = data["creator"]["fullname"]
 
41
    self.sender_nick = data["creator"]["login"]
 
42
    self.sender_id = data["creator"]["login"]
 
43
    self.image = data["creator"]["small_avatar_url"]
 
44
    
 
45
    self.time = support.parse_time(data["created_at"])
 
46
    self.text = data["body"] or ""
 
47
    self.bgcolor = "message_color"
 
48
    self.id = data["id"]
 
49
    
 
50
    self.url = "http://brightkite.com/objects/%s" % data["id"]
 
51
    self.profile_url = "http://brightkite.com/people/%s" % self.sender_nick
 
52
    
 
53
    self.html_string = '<span class="text">%s</span>' % \
 
54
      NICK_PARSE.sub('@<a class="inlinenick" href="http://brightkite.com/people/\\1">\\1</a>',
 
55
        support.linkify(self.text))
 
56
    
 
57
    self.is_reply = ("@%s" % self.username) in self.text
 
58
    self.can_thread = data["comments_count"] > 0
 
59
 
 
60
    # Geolocation
 
61
    self.location_lon = data["place"]["longitude"]
 
62
    self.location_lat = data["place"]["latitude"]
 
63
    self.location_id = data["place"]["id"]
 
64
    self.location_name = data["place"]["name"]
 
65
    self.location_fullname = data["place"]["display_location"]
 
66
    self.geo_position = (self.location_lat, self.location_lon)
 
67
 
 
68
    if "photo" in data:
 
69
      self.thumbnails = [{"src": data["photo"], "href": data["photo"]}]
 
70
 
 
71
class Comment:
 
72
  def __init__(self, client, data):
 
73
    self.client = client
 
74
    self.account = client.account
 
75
    self.service = client.account["service"]
 
76
    self.username = client.account["username"]
 
77
 
 
78
    self.sender = data["user"]["fullname"]
 
79
    self.sender_nick = data["user"]["login"]
 
80
    self.sender_id = data["user"]["login"]
 
81
    self.image = data["user"]["small_avatar_url"]
 
82
    
 
83
    self.time = support.parse_time(data["created_at"])
 
84
    self.text = data["comment"]
 
85
    self.bgcolor = "message_color"
 
86
    
 
87
    self.url = "http://brightkite.com/objects/%s" % data["place_object"]["id"]
 
88
    self.profile_url = "http://brightkite.com/people/%s" % self.sender_nick
 
89
    
 
90
    self.html_string = '<span class="text">%s</span>' % \
 
91
      NICK_PARSE.sub('@<a class="inlinenick" href="http://brightkite.com/people/\\1">\\1</a>',
 
92
        support.linkify(self.text))
 
93
    
 
94
    self.is_reply = ("@%s" % self.username) in self.text
 
95
 
 
96
class FriendPosition:
 
97
  def __init__(self, client, data):
 
98
    self.client = client
 
99
    self.account = client.account
 
100
    self.service = client.account["service"]
 
101
    self.username = client.account["username"]
 
102
    self.sender = data["fullname"]
 
103
    self.sender_nick = data["login"]
 
104
    self.sender_id = self.sender_nick
 
105
    self.time = support.parse_time(data["last_checked_in"])
 
106
    self.text = data["place"]["display_location"]
 
107
    self.image = data["small_avatar_url"]
 
108
    self.image_small = data["smaller_avatar_url"]
 
109
    self.bgcolor = "message_color"
 
110
    self.url = "http://brightkite.com" # TODO
 
111
    self.profile_url = "http://brightkite.com" # TODO
 
112
    self.is_reply = False
 
113
 
 
114
    # Geolocation
 
115
    self.location_lon = data["place"]["longitude"]
 
116
    self.location_lat = data["place"]["latitude"]
 
117
    self.location_id = data["place"]["id"]
 
118
    self.location_name = data["place"]["name"]
 
119
    self.location_fullname = data["place"]["display_location"]
 
120
 
 
121
class Client:
 
122
  def __init__(self, acct):
 
123
    self.account = acct
 
124
 
 
125
  def get_auth(self):
 
126
    return "Basic %s" % base64.encodestring(
 
127
      ("%s:%s" % (self.account["username"], self.account["password"]))).strip()
 
128
 
 
129
  def connect(self, url, data = None):
 
130
    return urllib2.urlopen(urllib2.Request(
 
131
      url, data, {"Authorization": self.get_auth()}))
 
132
 
 
133
  def get_friend_positions(self):
 
134
    return simplejson.load(self.connect(
 
135
      "http://brightkite.com/me/friends.json"))
 
136
 
 
137
  def get_messages(self):
 
138
    return simplejson.load(self.connect(
 
139
      "http://brightkite.com/me/friendstream.json"))
 
140
 
 
141
  def get_responses(self):
 
142
    return simplejson.load(self.connect(
 
143
      "http://brightkite.com/me/mentionsstream.json"))
 
144
 
 
145
  def get_thread_data(self, msg):
 
146
    return simplejson.load(self.connect(
 
147
      "http://brightkite.com/objects/%s/comments.json" % msg.id))
 
148
 
 
149
  def get_search(self, query):
 
150
    return minidom.parseString(urllib2.urlopen(
 
151
      urllib2.Request("http://identi.ca/search/notice/rss",
 
152
        urllib.urlencode({"q": query}))).read()).getElementsByTagName("item")
 
153
 
 
154
  def thread(self, msg):
 
155
    yield msg
 
156
    for data in self.get_thread_data(msg):
 
157
      yield Comment(self, data)
 
158
 
 
159
  def friend_positions(self):
 
160
    for data in self.get_friend_positions():
 
161
      yield FriendPosition(self, data)
 
162
 
 
163
  def search(self, query):
 
164
    for data in self.get_search(query):
 
165
      yield SearchResult(self, data, query)
 
166
 
 
167
  def responses(self):
 
168
    for data in self.get_responses():
 
169
      yield Message(self, data)
 
170
 
 
171
  def receive(self):
 
172
    for data in self.get_messages():
 
173
      yield Message(self, data)
 
174
 
 
175
  def send(self, message):
 
176
    return self.connect("http://identi.ca/api/statuses/update.json",
 
177
        urllib.urlencode({"status":message}))