2011年11月4日金曜日

twitterに投稿

#! /usr/bin/env python
#-*- coding: utf-8 -*-
import os
import time
import subprocess
import tweepy

consumer_key    = ''
consumer_secret = ''
access_key      = ''
access_secret   = ''

filename = time.strftime('%Y%m%d_%H%M%S.timely')
dirname  = os.path.dirname(__file__)
dirname = '/root/.ng/var/log/report'
dirname  = os.path.join(dirname, 'timely')

try:
    os.makedirs(dirname)
except (IOError, OSError), err:
    print err

filepath = os.path.join(dirname, filename)

child = subprocess.Popen(['emacs', '-nw', filepath])
rc = child.wait()

if rc == 0:
    data = ''
    with open(filepath, 'rt') as ff:
        data = ff.read()
        # create OAuth handler                                                      
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)                   
        # set access token to OAuth handler                                         
        auth.set_access_token(access_key, access_secret)                            
        # create API                                                                
        api = tweepy.API(auth_handler=auth)                                         
                                                                            
        # post 
        api.update_status(data.decode('utf_8').encode('utf_8'))      

0 件のコメント:

コメントを投稿