轻松存储和显示来自一组用户的tweets或关于给定主题的tweets
django-carson的Python项目详细描述
django carson使存储和显示来自一组 关于给定主题的用户或推特。
要了解如何使用django carson,请查看 TweetNevada。
基本的想法是你会有一组你想要的twitter帐户 遵循(例如,内华达州立法者)和一组通用标签和/或 社区用于参与 关于给定主题的集体对话。当组合在 同样的页面,你会得到一个非常有趣和动态的对话 地点。
开始
安装django carson:
$ mkvirtualenv --no-site-packages example_website $ pip install django-carson
将carson添加到INSTALLED_APPS
使用syncdb(或migrate carson创建数据库表 使用South)
要访问Twitter Streaming API,必须首先创建 合适的代币。
首先,create a new application。然后,单击“创建我的访问权限” 代币“
将下列值添加到settings.py
- “消费者密钥”→CONSUMER_KEY
- “消费者秘密”→CONSUMER_SECRET
- “访问令牌”→TOKEN_KEY
- “访问令牌机密”→TOKEN_SECRET
通过admin interface,添加您的帐户和标签/关键字。
注意:您不需要同时添加帐户和 标签/关键字。如果你愿意,你可以设计一个网站 存储的标签提及或仅由给定的 用户。
如果添加了任何帐户,则必须运行./manage.py lookup_twitter_ids或使用“查找twitter id”管理操作 在下一步工作之前。如果您只添加了标签或 关键词,你不需要这样做。
最后,运行:
$ ./manage.py get_tweets
这将打开到twitter流api的连接,并 在你的一个帐户发布推特或 创建tweet时会提到您的一个标记,该tweet将 存储。
用法
django carson只是django和twitter之间的桥梁 流式API。连接视图和 实际显示数据所需的模板。
任何使用carson的开发人员的主要入口点可能是 是carson.models.Tweet。此模型保存所有与 get_tweets。
每个carson.models.Tweet对象都有四个属性:
- account
- A ForeignKey pointing to the ^{tt16}$ that created the tweet, if applicable. Will be ^{tt17}$ if the tweet didn’t come from an account listed in ^{tt16}$.
- timestamp
- The UTC timestamp of the tweet.
- status_id
- The unique status id for each tweet. This is also in data[‘id’], but this allows an index to be created for it.
- data
- Stores the complete JSON object associated with the tweet. You can see what all is included in this attribute here.
附加到carson.models.Tweet的是三个经理:
- objects
- Returns a ^{tt20}$ of all tweets
- trusted
- Returns only the tweets associated with a ^{tt16}$. In other words, ^{tt22}$.
- untrusted
- Returns only the tweets not associated with a ^{tt16}$. In other words, ^{tt24}$.
一个简单的索引视图存在于{TT25} $中 最近20条可信和不可信的推文和呈现 carson/index.html(带有上下文变量trusted和 untrusted)。如果你的网站不太复杂的话可能会有用。
如果您在访问twitter api时遇到问题,可以 在settings.py中将HTTP_DEBUG设置为True。默认情况下是 False。
更改日志
- 0.2(2011年10月5日)
- 对流式处理和restapi使用ssl
- 记录并测试一些实用方法
- 使用requests进行 rest api调用
- 0.1(2011年8月9日)
- 初始版本