Attachment 'ShowTweets-0.3.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 u"""
   3     MoinMoin - ShowTweets macro Version 0.3.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. Better 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 (not wiki like now) and using some regular expressen to create working html links (for links and twitter references)
  27 """
  28 
  29 
  30 from MoinMoin import wikiutil
  31 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
  32 import twitter
  33 import socket
  34 import sys
  35 
  36 Dependencies = [] #do cache
  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     fmt = macro.formatter
  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 	#The twitter.Api instance must be authenticated if the user is private.
  51     #but make make sure that nobody can read this macro and be aware of some possible security risks
  52     #username = "Your Username"
  53     #password = "Your Password"
  54     try:
  55         username
  56     except NameError:
  57        api = twitter.Api()
  58     else:
  59       api = twitter.Api(username=username, password=password) #ToDo: authentication without username/password, better security
  60     
  61     #just for testing to see the full error messages
  62     if debug == False:
  63         try:
  64             statuses = api.GetUserTimeline(user=wikiutil.escape(user),count=maxTweets)
  65         except: #ToDo: Should using Error message and not just all
  66             errorMsg.append(fmt.div(True,css_class="error"))
  67             errorMsg.append(fmt.text(_("Twitter API error: %s") % sys.exc_info()[0]))
  68             errorMsg.append(fmt.div(False))
  69             return ''.join(errorMsg)
  70     else:
  71         statuses = api.GetUserTimeline(user=wikiutil.escape(user),count=maxTweets)
  72 
  73     #ToDo: Check if cache is avaible and don't ask twitter for every request
  74 
  75     #starting tweets output
  76     output.append(fmt.div(True,css_class="ShowTweets"))
  77     output.append(fmt.bullet_list(True))
  78 
  79     #reading tweets
  80     for tweetNumber in range(len(statuses)):
  81         output.append(fmt.listitem(True))
  82         output.append(wikiutil.renderText(request, WikiParser, statuses[tweetNumber].text))
  83         output.append(fmt.span(True,css_class="date",style="color:Gray; font-size:75%; display:block;"))
  84         output.append(fmt.text(statuses[tweetNumber].created_at))
  85         output.append(fmt.span(False))
  86         output.append(fmt.listitem(False))
  87 
  88     #check if there is atleast one tweet available
  89     if tweetNumber < 1:
  90         output.append(fmt.listitem(True))
  91         output.append(_("No tweet found"))
  92         output.append(fmt.listitem(False))
  93 
  94     #finishing tweets output
  95     output.append(fmt.bullet_list(False))
  96     output.append(fmt.div(False))
  97 
  98     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.
  • [get | view] (2009-10-18 23:02:56, 3.6 KB) [[attachment:ShowTweets-0.1.py]]
  • [get | view] (2009-10-20 12:52:55, 3.8 KB) [[attachment:ShowTweets-0.3.py]]
  • [get | view] (2010-05-20 23:46:11, 4.2 KB) [[attachment:ShowTweets-0.4.py]]
  • [get | view] (2013-06-21 09:28:11, 4.6 KB) [[attachment:ShowTweets.py]]
  • [get | view] (2010-05-20 23:53:11, 65.9 KB) [[attachment:output_tweets.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.