~khurshid-alam/gwibber/gwibber-hack

« back to all changes in this revision

Viewing changes to gwibber/microblog/plugins/digg/__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": "Digg",
 
7
  "version": "1.0",
 
8
  
 
9
  "config": [
 
10
    "receive_enabled",
 
11
    "username",
 
12
    "color",
 
13
  ],
 
14
 
 
15
  "authtype": "none",
 
16
  "color": "#E5E025",
 
17
 
 
18
  "features": [
 
19
    "receive",
 
20
  ],
 
21
 
 
22
  "default_streams": [
 
23
    "receive",
 
24
  ],
 
25
}
 
26
 
 
27
URL_PREFIX = "http://services.digg.com"
 
28
 
 
29
class Client:
 
30
  def __init__(self, acct):
 
31
    self.account = acct
 
32
 
 
33
  def _story(self, data):
 
34
    m = {}; 
 
35
    m["mid"] = str(data["id"])
 
36
    m["service"] = "digg"
 
37
    m["account"] = self.account["id"]
 
38
    m["time"] = data["submit_date"]
 
39
 
 
40
    m["text"] = data["title"] + "\n" + data["description"]
 
41
    m["content"] = "<b>%(title)s</b><br />%(description)s" % data
 
42
    m["html"] = "<b>%(title)s</b><br />%(description)s" % data
 
43
    user = data["friends"]["users"][0]
 
44
 
 
45
    m["sender"] = {}
 
46
    m["sender"]["nick"] = user["name"]
 
47
    m["sender"]["id"] = user["name"]
 
48
    m["sender"]["image"] = user["icon"]
 
49
    m["sender"]["url"] = "http://digg.com/users/%s" % user["name"]
 
50
    m["sender"]["is_me"] = user["name"] == self.account["username"]
 
51
    if user.get("fullname", 0): m["sender"]["name"] = user["fullname"]
 
52
    
 
53
    m["url"] = data["link"]
 
54
    m["likes"] = {"count": data["diggs"]}
 
55
 
 
56
    m["html"] = util.linkify(m["text"],
 
57
      ((util.PARSE_HASH, '#<a class="hash" href="%s#search?q=\\1">\\1</a>' % URL_PREFIX),
 
58
      (util.PARSE_NICK, '@<a class="nick" href="%s/\\1">\\1</a>' % URL_PREFIX)))
 
59
 
 
60
    m["content"] = util.linkify(m["text"],
 
61
      ((util.PARSE_HASH, '#<a class="hash" href="gwibber:/tag?acct=%s&query=\\1">\\1</a>' % m["account"]),
 
62
      (util.PARSE_NICK, '@<a class="nick" href="gwibber:/user?acct=%s&name=\\1">\\1</a>' % m["account"])))
 
63
 
 
64
    return m
 
65
 
 
66
  def _get(self, path, parse="story", post=False, single=False, **args):
 
67
    url = "/".join((URL_PREFIX, path)) + "?appkey=http://gwibber.com&type=json"
 
68
    
 
69
    data = network.Download(url, util.compact(args) or None, post).get_json()
 
70
 
 
71
    if not data: return []
 
72
 
 
73
    if "stories" in data:
 
74
        if single: return [getattr(self, "_%s" % parse)(data["stories"])]
 
75
        if parse: return [getattr(self, "_%s" % parse)(m) for m in data["stories"]]
 
76
        else: return []
 
77
    else:
 
78
        if ("status" in data) and ("message" in data):
 
79
            print "Digg: got message with status %s and message %s" % (data["status"], data["message"])
 
80
        return []
 
81
 
 
82
  def __call__(self, opname, **args):
 
83
    return getattr(self, opname)(**args)
 
84
 
 
85
  def receive(self):
 
86
    return self._get("user/%s/friends/dugg" % self.account["username"])