Skip to content

Instantly share code, notes, and snippets.

@ymorimo
Created January 14, 2011 10:13
Show Gist options
  • Save ymorimo/779441 to your computer and use it in GitHub Desktop.
Save ymorimo/779441 to your computer and use it in GitHub Desktop.
Daemons.run_proc('twitter_streaming_daemon',
:dir_mode => :normal,
:dir => File.join(APP_DIR, 'log'),
:multiple => false,
:backtrace => true,
:monitor => false,
:log_output => true
) do
EventMachine::run do
stream = Twitter::JSONStream.connect(:path => "/1/statuses/filter.json",
:auth => "username:password",
:method => "POST",
:user_agent => "my app",
:content => 'track=' + URI.escape(keywords.join(',')))
stream.each_item do |item|
begin
result = JSON.parse(item)
case
when result['id']
status = Status.from_api_status(result)
status.save
when result['delete']
logger.info "Status #{result['delete']['status']['id']} is deleted on Twitter."
when result['limit']
logger.info "Hit track limit: #{result['limit']['track']}"
end
rescue JSON::ParserError => e
logger.error "Unexpected response from Streaming API: #{e.class}: #{e.message}"
logger.error "item: #{item}"
end
end
stream.on_reconnect do |timeout, retries|
# ...
end
stream.on_max_reconnects do |timeout, retries|
logger.error "..."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment