public
Description: Personal source repository for my U of T related code and my projects not worth their own repository.
Homepage: http://myopicvoid.org/
Clone URL: git://github.com/meunierd/meunierd-uoft.git
meunierd-uoft / pybltwitter / pybltwitter.py
100644 104 lines (83 sloc) 2.775 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/env python
"""
A Pyblosxom plugin to fetch status updates from Twitter.
 
Depends:
python-twitter <http://code.google.com/p/python-twitter>
simplejson <http://undefined.org/python/#simplejson>
 
Configuration:
The following variables must be set in your config.py
- "twitterusr" your username
- "twitterpasswd" and that other thing
 
Optional
- "num_tweets" the number of tweets to display, defaults to 5
- "othertwitterusr" display a different user's stream
 
Use:
refer to $tweets in your templates to generate an
html unordered list <ul>
 
CSS Hooks:
ul class blosxomTweets
li class blosxomTweet
"""
 
__author__ = "Devon Meunier <devon.meunier@myopicvoid.org>"
__version__ = "0.1.5"
__url__ = "http://myopicvoid.org/"
__description__ = "Display tweets on your pyblosxom blog."
 
import os; os.environ["PYTHON_EGG_CACHE"]="/home/protected"
import twitter
 
def verify_installation(request):
    config = request.getConfiguration()
    if config.has_key("twitterusr") and config.has_key("twitterpasswd"):
      print " twitter user: " + config["twitterusr"]
      print " password: " + "*"*len(config["twitterpasswd"])
      return 1
    return 0
 
class PyblTwitter:
  """
Provides access to your twitter status updates.
"""
  def __init__(self, request):
    self._request = request
    self._config = request.getConfiguration()
    self._tweets = None
 
  def __str__(self):
    """
Returns the on-demand generated string.
"""
    self.generateTweets()
 
    return self._tweets
 
  def generateTweets(self):
    """
Creates the _tweets attribute
"""
    user = self._config["twitterusr"]
    passwd = self._config ["twitterpasswd"]
 
    api = twitter.Api(username=user, password=passwd)
 
    statuses = api.GetUserTimeline(user)
    if self._config.has_key("othertwitterusr"):
      statuses = self._config["othertwitterusr"]
    
    self._tweets = self.format(statuses)
 
  def format(self, statuses):
    """
Formats twitter statuses into html and css
"""
    numtweets = 5
    if self._config.has_key("num_tweets"):
      numtweets = self._config["num_tweets"]
    
    res = '<ul class="blosxomTweets">\n'
    for s in statuses[:numtweets]:
      # makes any links active
      i = s.text.find("http")
      if i is not -1:
        j = s.text.find(" ", i)
        url = s.text[i:j]
        if j is not -1:
          s.text = '%s<a href="%s">%s</a>%s' % (s.text[:i],url,url,s.text[j:])
        else:
          s.text = s.text[:i] + '<a href="%s">%s</a>' % (self[i:],) * 2
 
      res += " <li class='blosxomTweet'>%s</li>\n" % s.text
    res += "</ul>"
 
    return res.encode('utf-8')
 
def cb_prepare(args):
  request = args["request"]
  data = request.getData()
  data["tweets"] = PyblTwitter(request)