diff options
| author | 2010-03-30 11:37:27 +0200 | |
|---|---|---|
| committer | 2010-05-30 19:15:44 +0200 | |
| commit | 92b2b34ccfd908295f8e93aa4728ad8eaa18bda0 (patch) | |
| tree | 4045727952fa2311edb7f59da27b05e4901cc786 /minidinstall/tweet.py | |
| parent | df7be9f55ab42ea677e8937faf8f01b8a1630dc8 (diff) | |
added twitting support (e.g. for twitter/identi.ca)
Diffstat (limited to 'minidinstall/tweet.py')
| -rw-r--r-- | minidinstall/tweet.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/minidinstall/tweet.py b/minidinstall/tweet.py new file mode 100644 index 0000000..548918b --- /dev/null +++ b/minidinstall/tweet.py @@ -0,0 +1,58 @@ +# mail -*- mode: python; coding: utf-8 -*- + +"""Simple tweet support for mini-dinstall.""" + +# Copyright © 2010 Christopher R. Gabriel <cgabriel@truelite.it> + +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +import logging +import urllib2 +import base64 + +def send(tweet_body, tweet_server, tweet_user, tweet_password): + """Send tweet; on error, log and continue.""" + logger = logging.getLogger("mini-dinstall") + post_url = None + auth_realm = None + if tweet_server == 'identica': + post_url = 'http://identi.ca/api/statuses/update.json' + auth_realm = 'Identi.ca API' + if tweet_server == 'twitter': + post_url = 'http://api.twitter.com/1/statuses/update.json' + auth_realm = 'Twitter API' + + if not post_url: + logger.exception("Unknown tweet site") + if not tweet_user or not tweet_password: + logger.exception("Missing username or password for twitting") + + auth_handler = urllib2.HTTPBasicAuthHandler() + auth_handler.add_password(realm=auth_realm, + uri=post_url, + user=tweet_user, + passwd=tweet_password) + m_http_opener = urllib2.build_opener(auth_handler) + + req = urllib2.Request(post_url) + req.add_data("status=%s" % tweet_body) + handle = None + try: + handle = m_http_opener.open(req) + a = handle.read() + logger.info("Tweet sent to %s (%s)" % (tweet_server, tweet_user)) + except Exception, e: + logger.exception("Error sending tweet to %s ('%s') via %s: %s: %s", tweet_server, tweet_body, tweet_user, type(e), e.args) |
