在Facebook中有Real-time Updates,可以实时接受用户的更新,而在Twitter中有比其更强大的Streaming API,共有3种:Streaming API,User Streams,Site Streams,本文主要讨论前两个。基本思路是:连接Twitter Streams,创建一个长连接,不要关闭,然后Twitter会向此连接实时的发送更新,你要做的就是不断的从此连接中接受更新,然后处理之。使用Stream API当然要经过OAuth验证,这是必须滴。这点不同于Facebook(只需在请求中加上token参数即可),当然,有很多不错的Libary你可以使用,比如:Hammock, TweetSharp ,Twitterizer。
Streaming API
可以订阅Twitter上多个用户的更新,那些用户呢?可以有很多条件去过滤。Streaming API有多个方法:
1) statuses/filter
返回用户的状态更新通知,通过参数筛选用户
参数:count, delimited, follow, locations, track ,比如通过"follow=356,398”,可以订阅用户Id为356和398这两个用户的状态更新。
2) statuses/firehose
3) statuses/links
4) statuses/retweet
5) statuses/sample
这是一个示例,系统随机返回的一些更新,供你测试,且此接口可以不用Oauth验证,Basic验证即可。
返回的内容为JSON,且包括了状态更新的内容,如下:
{
"
text
"
:
"
pls tell me
"
,
"
in_reply_to_status_id
"
:
null
,
"
truncated
"
:
false
,
"
source
"
:
"
\u003Ca href=\"http:\/\/seesmic.com\/seesmic_desktop\/sd2\" rel=\"nofollow\"\u003ESeesmic Desktop\u003C\/a\u003E
"
,
"
in_reply_to_user_id
"
:
null
,
"
favorited
"
:
false
,
"
entities
"
:{
"
urls
"
:[],
"
user_mentions
"
:[],
"
hashtags
"
:[]},
"
in_reply_to_screen_name
"
:
null
,
"
in_reply_to_status_id_str
"
:
null
,
"
id_str
"
:
"
52910061479133184
"
,
"
place
"
:
null
,
"
created_at
"
:
"
Wed Mar 30 01:48:36 +0000 2011
"
,
"
contributors
"
:
null
,
"
user
"
:{
"
show_all_inline_media
"
:
false
,
"
follow_request_sent
"
:
null
,
"
geo_enabled
"
:
true
,
"
notifications
"
:
null
,
"
profile_sidebar_border_color
"
:
"
C0DEED
"
,
"
contributors_enabled
"
:
false
,
"
profile_image_url
"
:
"
http:\/\/a1.twimg.com\/profile_images\/1093534248\/SIGN11431642_128x128_normal.jpg
"
,
"
profile_use_background_image
"
:
true
,
"
statuses_count
"
:
39
,
"
profile_background_color
"
:
"
C0DEED
"
,
"
followers_count
"
:
4
,
"
description
"
:
"
programmer,tour,table tennis
"
,
"
screen_name
"
:
"
jack_cai
"
,
"
listed_count
"
:
0
,
"
verified
"
:
false
,
"
profile_background_image_url
"
:
"
http:\/\/a3.twimg.com\/a\/1299696992\/images\/themes\/theme1\/bg.png
"
,
"
location
"
:
"
shenzhen
"
,
"
time_zone
"
:
"
Hong Kong
"
,
"
profile_text_color
"
:
"
333333
"
,
"
is_translator
"
:
false
,
"
lang
"
:
"
en
"
,
"
profile_sidebar_fill_color
"
:
"
DDEEF6
"
,
"
protected
"
:
false
,
"
id_str
"
:
"
71195487
"
,
"
default_profile_image
"
:
false
,
"
default_profile
"
:
true
,
"
profile_background_tile
"
:
false
,
"
created_at
"
:
"
Thu Sep 03 07:10:38 +0000 2009
"
,
"
name
"
:
"
Jack Cai
"
,
"
friends_count
"
:
13
,
"
url
"
:
"
http:\/\/chy710.cnblogs.com\/
"
,
"
id
"
:
71195487
,
"
following
"
:
null
,
"
utc_offset
"
:
28800
,
"
favourites_count
"
:
0
,
"
profile_link_color
"
:
"
0084B4
"
},
"
retweeted
"
:
false
,
"
id
"
:
52910061479133184
,
"
in_reply_to_user_id_str
"
:
null
,
"
coordinates
"
:
null
,
"
geo
"
:
null
,
"
retweet_count
"
:
0
}
{
"
delete
"
:{
"
status
"
:{
"
user_id_str
"
:
"
71195487
"
,
"
id_str
"
:
"
52910061479133184
"
,
"
id
"
:
52910061479133184
,
"
user_id
"
:
71195487
}}}
User Streams
主要订阅当前用户相关的状态,跟随,收藏等信息的更新,连接创建后第一次收到的更新内容为该用户的朋友Id列表。
{
"
friends
"
:[
1497
,
169686021
,
790205
,
15211564
,
37784836
,
821958
,
14884312
,
92015003
,
822571
,
63846421
...]}
后面收到的更新就是用户相关内容更新(同Streaming),如你或你的好友更新状态时,你跟随某人或有人跟随你时,你收藏一个消息或有人收藏了你的消息时。。等等,具体参加API。
本文转自chy710博客园博客,原文链接:http://www.cnblogs.com/chy710/archive/2011/04/02/twitter_stream_api_dev.html
,如需转载请自行联系原作者