在Djang中使用twitter API

2024-10-01 04:49:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用pythontwitter工具来自动化东西(自动将tweet添加到收藏夹等)。这非常有用,所以我想把它变成一个名为“Mojito”的开源程序。问题是我不是python/django方面的专家,我被困在这里:

这是我用来收集两个变量的表单:mKeyword(在twitter上查找的关键字)和mCount(有多少条tweets):

from django import forms

class GetVariables(forms.Form):
    mKeyword = forms.CharField(max_length=100)
    mCount = forms.IntegerField(max_value=100, min_value=1)

然后我有一个“mojitoform”函数,它使用pythontwitter工具的auto_fav函数。二者均如下:

^{pr2}$

这是“auto\u fav()”和“search\u tweets()”函数:

def auto_fav(q, count=100, result_type="recent"):
    """
    Favorites tweets that match a certain phrase (hashtag, word, etc.)
    """
    result = search_tweets(q, count, result_type)
    for tweet in result["statuses"]:
        try:
            # don't favorite your own tweets
            if tweet["user"]["screen_name"] == TWITTER_HANDLE:
                continue

            result = t.favorites.create(_id=tweet["id"])
            print("favorited: %s" % (result["text"].encode("utf-8")))

        # when you have already favorited a tweet, this error is thrown
        except TwitterHTTPError as e:
            print("error: %s" % (str(e)))


def search_tweets(q, count=100, result_type="recent"):
return t.search.tweets(q=q, result_type=result_type, count=count)

我的问题是:当我运行mojitoform时,它没有考虑“mCount”变量。每次只有一条微博是受欢迎的。这很奇怪,因为auto_fav()脚本在SHELL上运行时运行良好,但是在django上它总是忽略mCount变量。。我把这一切都扭曲了,我迷路了。在


Tags: 工具django函数autosearchtypecountforms