~gmb/gwibber/use-dates-for-indexes

« back to all changes in this revision

Viewing changes to gwibber/microblog/twitter.py

Support showing twitter user's timeline in a new tab. Clicking on @username or
avatars will open a new tab with that user's timeline. The external profile can
then be opened in an external browser by using the link at the top of this
new tab.

TODO: identi.ca/laconi.ca not yet done but support will come in a branch _very_
soon.

Merge from lp:~oldman/gwibber/twitter-user-messages-tab

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
"""
8
8
 
9
9
import urllib2, urllib, base64, re, support, can, simplejson
 
10
import gettext
 
11
_ = gettext.lgettext
 
12
 
10
13
 
11
14
PROTOCOL_INFO = {
12
15
  "name": "Twitter",
33
36
    #can.THREAD,
34
37
    can.THREAD_REPLY,
35
38
    can.SEARCH_URL,
 
39
    can.USER_MESSAGES,
36
40
  ],
37
41
}
38
42
 
41
45
 
42
46
class Message:
43
47
  def __init__(self, client, data):
44
 
    self.id = data["id"]
 
48
   try:
45
49
    self.client = client
46
50
    self.account = client.account
47
51
    self.protocol = client.account["protocol"]
48
52
    self.username = client.account["username"]
 
53
    self.bgcolor = "message_color"
 
54
    self.id = data["id"] or ''
 
55
    self.time = support.parse_time(data["created_at"])
 
56
    self.is_private  = False
49
57
 
50
58
    if "user" in data:
51
59
      user = data["user"]
59
67
    self.sender = user["name"]
60
68
    self.sender_nick = user["screen_name"]
61
69
    self.sender_id = user["id"]
62
 
    self.time = support.parse_time(data["created_at"])
63
 
    self.text = data["text"]
64
 
    self.image = user["profile_image_url"]
65
 
    self.bgcolor = "message_color"
66
 
    self.url = "https://twitter.com/%s/statuses/%s" % (user["screen_name"], data["id"])
67
 
    self.profile_url = "https://twitter.com/%s" % user["screen_name"]
68
 
    
69
 
    self.html_string = '<span class="text">%s</span>' % \
70
 
        HASH_PARSE.sub('#<a class="inlinehash" href="gwibber:tag/\\1">\\1</a>',
71
 
      NICK_PARSE.sub('@<a class="inlinenick" href="https://twitter.com/\\1">\\1</a>',
72
 
        support.linkify(self.text)))
73
 
    self.is_reply = re.compile("@%s[\W]+|@%s$" % (self.username, self.username)).search(self.text)
74
 
    self.is_private  = False
 
70
 
 
71
    if data.has_key("user"):
 
72
      self.sender = data["user"]["name"]
 
73
      self.sender_nick = data["user"]["screen_name"]
 
74
      self.sender_id = data["user"]["id"]
 
75
      self.sender_location = data["user"]["location"]
 
76
      self.sender_followers_count = data["user"]["followers_count"]
 
77
      self.image = data["user"]["profile_image_url"]
 
78
      self.url = "https://twitter.com/%s/statuses/%s" % (data["user"]["screen_name"], data["id"])
 
79
      self.profile_url = "gwibber:user/%s" % data["user"]["screen_name"]
 
80
      self.external_profile_url = "https://twitter.com/%s" % data["user"]["screen_name"]
 
81
 
 
82
    if data.has_key("name"):
 
83
      self.sender = data["name"]
 
84
      self.sender_nick = data["screen_name"]
 
85
      self.sender_id = data["id"]
 
86
      self.sender_location = data["location"]
 
87
      self.sender_followers_count = data["followers_count"]
 
88
      self.image = data["profile_image_url"]
 
89
      self.url = self.profile_url = self.external_profile_url = "https://twitter.com/%s" % data["screen_name"]
 
90
      self.is_reply = False
 
91
      if data["protected"] == True:
 
92
        self.text = _("This user has protected their updates.") + ' ' + _("You need to send a request before you can view this person's timeline.") + ' ' + _("Send request...")
 
93
        self.html_string = '<p><b>' + _("This user has protected their updates.") + '</b><p>' + _("You need to send a request before you can view this person's timeline.") + '<p><a href="' + self.url + '">' + _("Send request...") + '</a>'
 
94
      else:
 
95
        self.text = self.html_string = ''
 
96
 
 
97
    if data.has_key("text"):
 
98
      self.text = data["text"]
 
99
      self.html_string = '<span class="text">%s</span>' % \
 
100
          HASH_PARSE.sub('#<a class="inlinehash" href="gwibber:tag/\\1">\\1</a>',
 
101
          NICK_PARSE.sub('@<a class="inlinenick" href="gwibber:user/\\1">\\1</a>',
 
102
          support.linkify(self.text)))
 
103
      self.is_reply = re.compile("@%s[\W]+|@%s$" % (self.username, self.username)).search(self.text)
 
104
      self.reply_nick = ''
 
105
      self.reply_url = ''
 
106
 
 
107
    if data.has_key("in_reply_to_screen_name"):
 
108
      self.reply_nick = data["in_reply_to_screen_name"]
 
109
      self.reply_url = "https://twitter.com/%s/statuses/%s" % (data["in_reply_to_screen_name"], data["in_reply_to_status_id"])
 
110
   except Exception:
 
111
    from traceback import format_exc
 
112
    print format_exc()
75
113
 
76
114
class SearchResult:
77
115
  def __init__(self, client, data, query = None):
87
125
    self.image = data["profile_image_url"]
88
126
    self.bgcolor = "message_color"
89
127
    self.url = "https://twitter.com/%s/statuses/%s" % (data["from_user"], data["id"])
90
 
    self.profile_url = "https://twitter.com/%s" % data["from_user"]
 
128
    self.profile_url = "gwibber:user/%s" % data["from_user"]
 
129
    self.external_profile_url = "https://twitter.com/%s" % data["from_user"]
91
130
 
92
131
    if query: html = support.highlight_search_results(self.text, query)
93
132
    else: html = self.text
94
133
    
95
134
    self.html_string = '<span class="text">%s</span>' % \
96
135
      HASH_PARSE.sub('#<a class="inlinehash" href="gwibber:tag/\\1">\\1</a>',
97
 
      NICK_PARSE.sub('@<a class="inlinenick" href="https://twitter.com/\\1">\\1</a>',
 
136
      NICK_PARSE.sub('@<a class="inlinenick" href="gwibber:user/\\1">\\1</a>',
98
137
        support.linkify(self.text)))
99
138
 
100
139
    self.is_reply = re.compile("@%s[\W]+|@%s$" % (self.username, self.username)).search(self.text) 
126
165
      "https://twitter.com/statuses/friends_timeline.json",
127
166
        urllib.urlencode({"count": self.account["receive_count"] or "20"})))
128
167
 
 
168
  def get_user_messages(self, screen_name):
 
169
    try:
 
170
      return simplejson.loads(self.connect(
 
171
        "https://twitter.com/statuses/user_timeline/"+ screen_name + ".json",
 
172
          urllib.urlencode({"count": self.account["receive_count"] or "20"})))
 
173
    except Exception:
 
174
      profile = [simplejson.loads(self.connect(
 
175
        "https://twitter.com/users/show/"+ screen_name +".json"))]
 
176
      return profile
 
177
 
129
178
  def get_replies(self):
130
179
    return simplejson.loads(self.connect(
131
180
      "https://twitter.com/statuses/replies.json",
167
216
    for data in self.get_messages():
168
217
      yield Message(self, data)
169
218
 
 
219
  def user_messages(self, screen_name):
 
220
    for data in self.get_user_messages(screen_name):
 
221
      yield Message(self, data)
 
222
 
170
223
  def send(self, message):
171
224
    data = simplejson.loads(self.connect(
172
225
      "https://twitter.com/statuses/update.json",
173
 
            urllib.urlencode({"status":message, "source": "gwibbernet"})))
 
226
        urllib.urlencode({"status":message, "source": "gwibbernet"})))
174
227
    return Message(self, data)
175
228
 
176
229
  def send_thread(self, msg, message):