1

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

这是我用来收集两个变量的表格:mKeyword(要在 Twitter 上查找的关键字)和 mCount(多少条推文):

from django import forms

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

然后我有一个使用 python-twitter-tools 的 auto_fav 函数的“mojitoform”函数。两者都在下面:

def mojitoform (request):
try:
    form = GetVariables(request.POST)
    if form.is_valid():
    mKeyword = form.cleaned_data['mKeyword'] 
    mCount = form.cleaned_data['mCount']
    success = True
    tweets = auto_fav( mKeyword, mCount)['text']
except:
    notLoggedIn = True
return render (request, '../templates/dashboard.html', locals())

这是“auto_fav()”和“search_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”变量。每次只有一条推文被收藏。这很奇怪,因为当我在 SHELL 上运行 auto_fav() 脚本时它运行良好,但在 django 上它总是会忽略 mCount 变量。我已经扭曲了这个,我迷路了。

4

0 回答 0