如何用tweepy发布除字符串以外的任何内容

时间:2015-02-04 22:56:05

标签: python python-2.7 tweepy

我试图用生成的文本更新我的Twitter状态。到目前为止生成的文本效果很好,但我无法发布到Twitter。

import tweepy

import SentenceGenerator

with open('Ratschlaege.txt','r') as textfile: #load the text to analyze
sample_text = textfile.read()

#Generating one sentence:
#print SentenceGenerator.generate_sentence(sample_text)

#Generating a paragraph:
sentences = 1
print ' '.join([SentenceGenerator.generate_sentence(sample_text) for i        in xrange(sentences)])


# Consumer keys and access tokens, used for OAuth
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)     
# Creation of the actual interface, using authentication
api = tweepy.API(auth)         
# Sample method, used to update a status
api.update_status(' '.join([SentenceGenerator.generate_sentence(sample_text) for i in xrange(sentences)]))

据我所知,您可以发布字符串或文件,但不能发布新生成的文本。 有什么办法吗?

- EDIT-- 好吧,我试图在我的脚本中构建一个像这样的函数

def tweet (tweet):
    tweet = ' '.join([SentenceGenerator.generate_sentence(sample_text) for i in xrange(sentences)])
    # Consumer keys and access tokens, used for OAuth
    consumer_key = 'xxx'
    consumer_secret = 'xxx'
    access_token = 'xxx'
    access_token_secret = 'xxx'

    # OAuth process, using the keys and tokens
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)     
    # Creation of the actual interface, using authentication
    api = tweepy.API(auth)         
    # Sample method, used to update a status
    api.update_status(tweet)        
time.sleep(10)

现在它运行,循环中每隔十秒(测试一次),并且没有显示错误,但状态不会更新。它像它应该做的那样运行,只是它没有推文。 我错过了什么?

- 编辑 -

我还添加了一个应该将生成的文本插入MySQL数据库的部分

db = MySQLdb.connect ( host ='127.0.0.1',
                       user = 'Daniel',
                       passwd = 'localhost')
        cur = db.cursor()
        cur.execute("insert into multigram (Ratschlag) value ('%s')" % (tweet))

相同的地方:没有错误消息,但数据库中也没有条目。 我相信它很容易解决,但我不知道我做错了什么。

0 个答案:

没有答案