Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Determining event-type with user stream #68

Closed
auxbuss opened this Issue · 12 comments

4 participants

@auxbuss

I'm using userstream with on_anything. Like this:

  client = TweetStream::Client.new

  client.follow('12345678') do |status|
    ap status
  end

  client.on_error do |message|
    ap message
  end

  client.on_anything do |message|
    ap message
  end

  client.userstream

There doesn't appear to be a simple way to identify the event-type of a message. For example, a follows event includes :event => "follow", whereas a direct message or a tweet has nothing to identify it. The raw twitter user stream contains an event field, but this appears not to be passed through.

Is there a simple way to determine the event-type?

Currently, I need to be able to respond to follows, mentions, implicit replies, and DMs. I'm new to tweetstream, so if there's a better way to do what I require, please let me know. Thanks.

@stve
Owner

Hi @auxbuss,

There are a few custom callbacks that can be invoked. on_anything is a catchall that is invoked on any message. You may want to have a look at the userstream example for usage of on_timeline_status and on_direct_message callbacks: https://github.com/intridea/tweetstream/blob/master/examples/userstream.rb

For more specific event handling of follows, favorites, etc. there are no callbacks for those right now. This is probably a worthy addition to TweetStream. Pull requests welcome. :smile:

@auxbuss

Thanks. Yes, I ended up with on_anything because I couldn't access everything I need.

I did have a look through Client::connect and couldn't see where data seems to be going AWOL. I'll have a closer look.

on_timeline_status is to verbose for my requirements, since it returns all followed tweets.

I probably will hack the code, but I don't want to go the way of callbacks, at least not as implemented.

@stve
Owner

@auxbuss were you able to come up with anything that might be worth adding to TweetStream?

@stve stve closed this
@auxbuss

Not yet., no. My feeling is that Tweetstream::Client::connect needs a major refactor. There's a whopping great if statement in there that should probably be replaced by using polymorphism (and a factory), but I'm unclear how to go about that at the moment. Ditto the set-up of all those procs as event handlers. But since that's pretty much the guts of the whole thing, it's a bit of a redesign.

@stve
Owner

I agree that it is getting unwieldy and have been thinking about how to refactor it, I've got some ideas which I plan to pursue once the Site Stream features are released. Stay tuned.

@jermaine

Hi,

I've been experimenting with tweetstream and find it pretty interesting.
However, I can't seem to figure out how to fetch 'events' from the client stream.

This is what I have (pretty straightforward)

client = TweetStream::Client.new

client.userstream do |status|
  puts status.inspect
end

I would like to see events like: new followers, unfollowers, mentions and retweets.
Somehow they are not showing up in the stream.

There's also a discussion on twitter on the topic:
https://dev.twitter.com/discussions/1510

Hope someone can help out.

Thanks.

@stve
Owner

Those events should be emitted via on_anything:

client.on_anything do |message|
  ap message
end

Admittedly, it's not that useful as you'll need to observe each message to determine what type of event is being returned, it's on my roadmap for improvements.

@chrisobdam

Hi spagalloco,

Does on_anything not provide the same result as

client.userstream do |status|
  puts status.inspect
end

?

@chrisobdam

This old post states that events are not available in the user stream

https://groups.google.com/d/msg/twitter-development-talk/0pxb1WxJwjw/Ewe-A3cVEBgJ

@spagalloco You state that they are being emitted by the stream, have you seen them pop up on the stream some time? :)

@stve
Owner

@chrisobdam that list is fairly out of date, refer to User Stream Messages instead for an accurate list.

As for on_anything, it will yield differently than a block that is passed to userstream. I would suggest not passing a block to the userstream method and instead handle events passed through on_anything.

@stve
Owner

What do you guys think about something like this for handling events?

client.on_event(:unfollow) do |msg|
  # handle unfollow event
end

That would allow you to create individualized handlers for userstream events.

@chrisobdam

@spagalloco That would be great! I already started making that but I don't get any events via on_anything. That should be the case right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.