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

« back to all changes in this revision

Viewing changes to gwibber/microblog/brightkite.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
BrightKite interface for Gwibber
 
4
SegPhault (Ryan Paul) - 10/19/2008
 
5
 
 
6
"""
 
7
 
 
8
import urllib2, urllib, base64, re, support, can, simplejson
 
9
from xml.dom import minidom
 
10
 
 
11
PROTOCOL_INFO = {
 
12
  "name": "BrightKite",
 
13
  "version": 0.1,
 
14
  
 
15
  "config": [
 
16
    "password",
 
17
    "username",
 
18
    "message_color",
 
19
    "receive_enabled",
 
20
    "send_enabled"
 
21
  ],
 
22
 
 
23
  "features": [
 
24
    #can.SEND,
 
25
    #can.RECEIVE,
 
26
    #can.SEARCH,
 
27
    #can.REPLY,
 
28
    #can.RESPONSES,
 
29
    #can.DELETE,
 
30
    #can.THREAD,
 
31
    can.GEO_FRIEND_POSITIONS
 
32
  ],
 
33
}
 
34
 
 
35
NICK_PARSE = re.compile("@([A-Za-z0-9]+)")
 
36
HASH_PARSE = re.compile("#([A-Za-z0-9_\-.]+)")
 
37
 
 
38
class Message:
 
39
  def __init__(self, client, data):
 
40
    self.client = client
 
41
    self.account = client.account
 
42
    self.protocol = client.account["protocol"]
 
43
    self.username = client.account["username"]
 
44
    self.sender = data["user"]["name"]
 
45
    self.sender_nick = data["user"]["screen_name"]
 
46
    self.sender_id = data["user"]["id"]
 
47
    self.time = support.parse_time(data["created_at"])
 
48
    self.text = support.xml_escape(data["text"])
 
49
    self.image = data["user"]["profile_image_url"]
 
50
    self.bgcolor = "message_color"
 
51
    self.url = "http://identi.ca/notice/%s" % data["id"] # % (data["user"]["screen_name"], data["id"])
 
52
    self.profile_url = "http://identi.ca/%s" % data["user"]["screen_name"]
 
53
    self.html_string = '<span class="text">%s</span>' % \
 
54
      HASH_PARSE.sub('#<a class="inlinehash" href="http://identi.ca/tag/\\1">\\1</a>',
 
55
        NICK_PARSE.sub('@<a class="inlinenick" href="http://identi.ca/\\1">\\1</a>',
 
56
          support.linkify(self.text)))
 
57
    self.is_reply = ("@%s" % self.username) in self.text
 
58
 
 
59
class FriendPosition:
 
60
  def __init__(self, client, data):
 
61
    self.client = client
 
62
    self.account = client.account
 
63
    self.protocol = client.account["protocol"]
 
64
    self.username = client.account["username"]
 
65
    self.sender = data["fullname"]
 
66
    self.sender_nick = data["login"]
 
67
    self.sender_id = self.sender_nick
 
68
    self.time = support.parse_time(data["last_checked_in"])
 
69
    self.text = data["place"]["display_location"]
 
70
    self.image = data["small_avatar_url"]
 
71
    self.image_small = data["smaller_avatar_url"]
 
72
    self.bgcolor = "message_color"
 
73
    self.url = "http://brightkite.com" # TODO
 
74
    self.profile_url = "http://brightkite.com" # TODO
 
75
    self.is_reply = False
 
76
 
 
77
    # Geolocation
 
78
    self.location_longitude = data["place"]["longitude"]
 
79
    self.location_latitude = data["place"]["latitude"]
 
80
    self.location_id = data["place"]["id"]
 
81
    self.location_name = data["place"]["name"]
 
82
    self.location_fullname = data["place"]["display_location"]
 
83
 
 
84
class Client:
 
85
  def __init__(self, acct):
 
86
    self.account = acct
 
87
 
 
88
  def get_auth(self):
 
89
    return "Basic %s" % base64.encodestring(
 
90
      ("%s:%s" % (self.account["username"], self.account["password"]))).strip()
 
91
 
 
92
  def connect(self, url, data = None):
 
93
    return urllib2.urlopen(urllib2.Request(
 
94
      url, data, {"Authorization": self.get_auth()})).read()
 
95
 
 
96
  def get_friend_positions(self):
 
97
    return simplejson.loads(self.connect(
 
98
      "http://brightkite.com/me/friends.json"))
 
99
 
 
100
  def get_messages(self):
 
101
    return simplejson.loads(self.connect(
 
102
      "http://identi.ca/api/statuses/friends_timeline.json"))
 
103
 
 
104
  def get_responses(self):
 
105
    return simplejson.loads(self.connect(
 
106
      "http://identi.ca/api/statuses/replies.json"))
 
107
 
 
108
  def get_search(self, query):
 
109
    return minidom.parseString(urllib2.urlopen(
 
110
      urllib2.Request("http://identi.ca/search/notice/rss",
 
111
        urllib.urlencode({"q": query}))).read()).getElementsByTagName("item")
 
112
 
 
113
  def friend_positions(self):
 
114
    for data in self.get_friend_positions():
 
115
      yield FriendPosition(self, data)
 
116
 
 
117
  def search(self, query):
 
118
    for data in self.get_search(query):
 
119
      yield SearchResult(self, data, query)
 
120
 
 
121
  def responses(self):
 
122
    for data in self.get_responses():
 
123
      yield Message(self, data)
 
124
 
 
125
  def receive(self):
 
126
    for data in self.get_messages():
 
127
      yield Message(self, data)
 
128
 
 
129
  def send(self, message):
 
130
    return self.connect("http://identi.ca/api/statuses/update.json",
 
131
        urllib.urlencode({"status":message}))