我正在尝试使用Requests创建一种从Twitter的用户流中消费的强大方式.到目前为止,我已经制作了以下基本工作示例:
"""
Example of connecting to the Twitter user stream using Requests.
"""
import sys
import json
import requests
from oauth_hook import OAuthHook
def userstream(access_token, access_token_secret, consumer_key, consumer_secret):
oauth_hook = OAuthHook(access_token=access_token, access_token_secret=access_token_secret,
consumer_key=consumer_key, consumer_secret=consumer_secret,
header_auth=True)
hooks = dict(pre_request=oauth_hook)
config = dict(verbose=sys.stderr)
client = requests.session(hooks=hooks, config=config)
data = dict(delimited="length")
r = client.post("https://userstream.twitter.com/2/user.json", data=data, prefetch=False)
# TODO detect disconnection somehow
# https://github.com/kennethreitz/requests/pull/200/files#L13R169
# Use a timeout? http://pguides.net/python-tutorial/python-timeout-a-function/
for chunk in r.iter_lines(chunk_size=1):
if chunk and not chunk.isdigit():
yield json.loads(chunk)
if __name__ …Run Code Online (Sandbox Code Playgroud) Django REST Framework 演示应用程序公开了两个集合,/ users /和/ snippets /.
在我的应用程序中,我希望能够嵌套这些集合,例如为用户5加载片段,我要求/ users/5/snippets /
这种配置在Django REST Framework中是否可行?我该如何设置呢?