Python 在Django中使用twitter API
我一直在使用python推特工具来自动化东西,自动将推特添加到收藏夹等。。这是非常有用的,所以我想我应该把它变成一个开源程序,叫做Mojito。问题是我不是python/django方面的专家,我被困在这里: 这是我用来收集两个变量的表单:mKeyword要在twitter上查找的关键字和mCount多少条tweets:Python 在Django中使用twitter API,python,django,twitter,django-views,python-twitter,Python,Django,Twitter,Django Views,Python Twitter,我一直在使用python推特工具来自动化东西,自动将推特添加到收藏夹等。。这是非常有用的,所以我想我应该把它变成一个开源程序,叫做Mojito。问题是我不是python/django方面的专家,我被困在这里: 这是我用来收集两个变量的表单:mKeyword要在twitter上查找的关键字和mCount多少条tweets: from django import forms class GetVariables(forms.Form): mKeyword = forms.CharField
from django import forms
class GetVariables(forms.Form):
mKeyword = forms.CharField(max_length=100)
mCount = forms.IntegerField(max_value=100, min_value=1)
然后我有一个mojitoform函数,它使用python twitter工具的auto_fav函数。二者均为:
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变量。。我把它弄得乱七八糟,我迷路了。你能展示一下GetVariables表单定义吗?嗨,丹尼尔,刚刚把它添加到帖子中。Cheeshi,很抱歉坚持,但是有人发现了什么吗?仍然没有弄明白…所以你没有发布的一点代码是搜索推特。那是什么?它似乎不是来自python twitter工具。你是对的,我忘了提到那个。这是很基本的,我刚刚加了。