Attachment 'ShowTweets.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 u"""
   3     MoinMoin - ShowTweets macro Version 0.6.1
   4                Displays twitter messages (aka tweeds) from a user
   5 
   6     <<ShowTweets(user="Twitter Username", maxTweets=10, noReply=True)>>
   7 
   8     Exampels:
   9      * <<ShowTweets(rockheavy)>>
  10 
  11     Notes about OAuth:
  12     All Twitter apps/clients are required to use secure authentication based on OAuth.
  13     You need to registry this app on https://dev.twitter.com/apps and get from theire the consumer secret / key.
  14 
  15     @copyright: 2009 by MarcelHäfner (http://moinmo.in/MarcelHäfner)
  16     @license: GNU GPL, see COPYING for details.
  17 
  18     Licences for dependencies:
  19      * tweepy is under MIT License
  20 
  21     Dependencies:
  22      * python-tweepy [[https://github.com/tweepy/tweepy]]
  23 
  24     Version:
  25      * 0.6.1: Bugfix (typo)
  26      * 0.6: Fixed error to get another users timeline (user -> user_id)
  27      * 0.5 / 03.01.2013: Moved to Tweepy. Integrated Patch from FelixYan to add an option to hide @replies from results
  28 
  29 """
  30 from MoinMoin import wikiutil
  31 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
  32 import tweepy
  33 import socket
  34 import sys
  35 
  36 Dependencies = ['time']  # do not cache; or cache and remove the 'time'
  37 
  38 
  39 def errorOutput(macro, errorType, errorMessage):
  40     """
  41     Return a rendered error message
  42     @param errorType: some main error type
  43     @param errorMessage: detail error type
  44     @returns: list
  45     """
  46     request = macro.request
  47     fmt = macro.formatter
  48     _ = request.getText
  49 
  50     errorMsg = []
  51     errorMsg.append(fmt.div(True, css_class="error"))
  52     errorMsg.append(wikiutil.renderText(request, WikiParser, _("/!\ Error Message from Macro [[http://moinmo.in/MacroMarket/ShowTweets|ShowTweets]]!")))
  53     errorMsg.append(fmt.bullet_list(True))
  54     errorMsg.append(fmt.listitem(True))
  55     errorMsg.append(fmt.text(_("Type: %s") % wikiutil.escape(errorType).decode("utf-8", "replace")))
  56     errorMsg.append(fmt.listitem(False))
  57     errorMsg.append(fmt.listitem(True))
  58     errorMsg.append(fmt.text(_("Message: %s") % wikiutil.escape(errorMessage)))
  59     errorMsg.append(fmt.listitem(False))
  60     errorMsg.append(fmt.bullet_list(False))
  61     errorMsg.append(fmt.div(False))
  62     return ''.join(errorMsg)
  63 
  64 
  65 def macro_ShowTweets(macro, user, maxTweets=10, noReply=True):
  66     """
  67     Creates a ordered list with twitter status messages
  68     @param user: username from a twitter account, needed
  69     @param maxTweets: optional, how many tweets should be displayed
  70     """
  71 
  72     #Inital stuff
  73     socket.setdefaulttimeout(10)  # timeout set to 10s
  74     request = macro.request
  75     fmt = macro.formatter
  76     _ = request.getText
  77     output = []
  78     tweetNumber = 0
  79     #user = "<twitteraccount>"
  80 
  81     #OAUTH configuration (you have to configure this!)
  82     consumer_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  83     consumer_secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  84     access_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  85     access_token_secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  86     auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  87     auth.set_access_token(access_token, access_token_secret)
  88     
  89     #Initi the API
  90     api = tweepy.API(auth,secure=True)
  91 
  92     #Fetch more for filtering
  93     fetchNum = maxTweets
  94     if noReply:
  95         fetchNum *= 10
  96 
  97     #Get the messages from twitter or use an exception to display an error msg
  98     try:
  99         statuses = api.user_timeline(id=wikiutil.escape(user), count=fetchNum)
 100     except:
 101         return errorOutput(macro, _("GetUserTimeline"), str(sys.exc_info()[1]))
 102 
 103     #Starting tweets output
 104     output.append(fmt.div(True, css_class="ShowTweets"))
 105     output.append(fmt.bullet_list(True))
 106 
 107     #Reading tweets
 108     tweetCount = 0
 109     for tweet in statuses:
 110         if noReply and tweet.text[0] == "@":
 111             continue
 112         output.append(fmt.listitem(True))
 113         output.append(wikiutil.renderText(request, WikiParser, wikiutil.escape(tweet.text)))
 114         output.append(fmt.span(True, css_class="date", style="color:Gray; font-size:75%; display:block;"))
 115         output.append(fmt.text(wikiutil.escape(statuses[tweetNumber].created_at)))
 116         output.append(fmt.span(False))
 117         output.append(fmt.listitem(False))
 118         tweetCount += 1
 119         if tweetCount >= maxTweets:
 120             break
 121 
 122     #Check if there is atleast one tweet available for ouput
 123     if tweetCount < 1:
 124         return errorOutput(macro, _("tweetNumber"), _("Can't find any twitter status message from user '%s'" % user))
 125 
 126     #Finishing output
 127     output.append(fmt.bullet_list(False))
 128     output.append(fmt.div(False))
 129     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.