Attachment 'ShowTweets-0.1.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 u"""
3 MoinMoin - ShowTweets macro Version 0.1 ALPHA
4 Displays twitter messages (aka tweeds) from a user
5
6 <<ShowTweets(user="UserName",maxTweets=10)>>
7
8 Exampels:
9 * <<ShowTweets(user="UserName")>>
10
11 @copyright: 2009 by MarcelHäfner (http://moinmo.in/MarcelHäfner)
12 @license: GNU GPL, see COPYING for details.
13
14 Licences for dependencies:
15 * Python-Twitter is under Apache License, Version 2.0
16 * simplejson is under MIT License
17
18 Dependencies:
19 * Python wrapper around the Python API (Python-Twitter): http://code.google.com/p/python-twitter/
20 * JSON encoder/decoder for Python (simplejson) http://pypi.python.org/pypi/simplejson
21
22 @TODO:
23 1. Caching for the displayed tweets (because of limits: http://apiwiki.twitter.com/Rate-limiting) in memory of filesystem
24 2. Authentication without cleartext passwd (maybe http://apiwiki.twitter.com/OAuth-FAQ)
25 3. Better error handling for the urllib, not depending on sys and socket stuff
26 4. Output of tweets as html and using some regular expressen to create working html links (for links and twitter references)
27 5. Adding date/time to every tweet
28 6. creating better error messages output
29 """
30
31
32 from MoinMoin import wikiutil
33 from MoinMoin.formatter.text_html import Formatter
34 import twitter
35 import socket
36 import sys
37
38 def macro_ShowTweets(macro, user=u"Twitter",maxTweets=10, debug=False):
39
40 #inital stuff
41 socket.setdefaulttimeout(15) # after 10s trying to fetch some data from the twitter-api, it will throw an exeption
42 request = macro.request
43 html_formatter = Formatter(request)
44 _ = request.getText
45 output = []
46 errorMsg = []
47
48 #user login
49 #uncoment username and password, if you want to use it with username/password of your twitter own account
50 #but make make sure that nobody can read this macro and be aware of some possible security risks
51 #username = "Your Username"
52 #password = "Your Password"
53 try:
54 username
55 except NameError:
56 api = twitter.Api()
57 else:
58 api = twitter.Api(username=username, password=password) #ToDo: authentication without username/password, better security
59
60 #just for testing to see the full error messages
61 if debug == False:
62 try:
63 statuses = api.GetUserTimeline(wikiutil.escape(user))
64 except: #ToDo: Should using Error message and not just all
65 errorMsg.append('<div class="errormsg">')
66 errorMsg.append(html_formatter.text(_("Twitter API error: %s") % sys.exc_info()[0]))
67 errorMsg.append('</div>')
68 return macro.formatter.rawHTML('\n'.join(errorMsg))
69 else:
70 statuses = api.GetUserTimeline(wikiutil.escape(user))
71
72 #ToDo: Check if cache is avaible and don't ask twitter for every request
73
74 #starting tweets output
75 output.append(html_formatter.bullet_list(True))
76
77 #reading tweets
78 for tweetNumber in range(len(statuses)):
79 if tweetNumber < maxTweets:
80 output.append(html_formatter.listitem(True))
81 output.append(html_formatter.text(statuses[tweetNumber].text)) #ToDo: Output should be optimized with links
82 output.append(html_formatter.listitem(False))
83 else:
84 break
85
86 #check if there is atleast one tweet available
87 if tweetNumber < 1:
88 output.append(html_formatter.listitem(True))
89 output.append(_("No tweet found"))
90 output.append(html_formatter.listitem(False))
91
92 #finishing tweets output
93 output.append(html_formatter.bullet_list(False))
94
95 return ''.join(output)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.