Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on tweetstream in favor of twitter v6.0.0 #281

Merged
merged 1 commit into from
Dec 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 19 additions & 43 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,40 @@ PATH
launchy (~> 2.4.3)
oauth (~> 0.5.1)
toml-rb (~> 0.3.14)
tweetstream (~> 2.6.1)
twitter (~> 5.16.0)
twitter (~> 6.0.0)
twitter-text (~> 1.14.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.4.0)
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
buftok (0.2.0)
citrus (3.0.2)
cookiejar (0.3.3)
curses (1.0.2)
daemons (1.2.4)
diff-lcs (1.2.5)
domain_name (0.5.20160826)
domain_name (0.5.20161129)
unf (>= 0.0.5, < 1.0.0)
em-http-request (1.1.5)
addressable (>= 2.3.4)
cookiejar (!= 0.3.1)
em-socksify (>= 0.3)
eventmachine (>= 1.0.3)
http_parser.rb (>= 0.6.0)
em-socksify (0.3.1)
eventmachine (>= 1.0.0.beta.4)
em-twitter (0.3.5)
buftok (~> 0.2)
eventmachine (~> 1.0)
http_parser.rb (~> 0.6)
simple_oauth (~> 0.2)
equalizer (0.0.10)
eventmachine (1.2.0.1)
faraday (0.9.2)
equalizer (0.0.11)
faraday (0.10.1)
multipart-post (>= 1.2, < 3)
http (1.0.4)
http (2.1.0)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 1.0.1)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.2)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (1.0.1)
http_parser.rb (0.6.0)
json (1.8.3)
launchy (2.4.3)
addressable (~> 2.3)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
multi_json (1.12.1)
multipart-post (2.0.0)
naught (1.1.0)
oauth (0.5.1)
public_suffix (2.0.4)
rake (10.5.0)
rspec (3.2.0)
rspec-core (~> 3.2.0)
Expand All @@ -73,25 +56,18 @@ GEM
rspec-support (3.2.2)
simple_oauth (0.3.1)
thread_safe (0.3.5)
toml-rb (0.3.14)
toml-rb (0.3.15)
citrus (~> 3.0, > 3.0)
tweetstream (2.6.1)
daemons (~> 1.1)
em-http-request (>= 1.1.1)
em-twitter (~> 0.3)
multi_json (~> 1.3)
twitter (~> 5.5)
twitter (5.16.0)
addressable (~> 2.3)
twitter (6.0.0)
addressable (~> 2.5)
buftok (~> 0.2.0)
equalizer (= 0.0.10)
faraday (~> 0.9.0)
http (~> 1.0)
equalizer (= 0.0.11)
faraday (~> 0.10.0)
http (~> 2.1)
http_parser.rb (~> 0.6.0)
json (~> 1.8)
memoizable (~> 0.4.0)
naught (~> 1.0)
simple_oauth (~> 0.3.0)
memoizable (~> 0.4.2)
naught (~> 1.1)
simple_oauth (~> 0.3.1)
twitter-text (1.14.0)
unf (~> 0.1.0)
unf (0.1.4)
Expand All @@ -108,4 +84,4 @@ DEPENDENCIES
twterm!

BUNDLED WITH
1.12.5
1.13.7
1 change: 0 additions & 1 deletion lib/twterm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require 'readline'
require 'singleton'
require 'set'
require 'tweetstream'
require 'twitter'
require 'twitter-text'
require 'yaml'
Expand Down
2 changes: 0 additions & 2 deletions lib/twterm/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def initialize(user_id, screen_name, access_token, access_token_secret)
end
end

initialize_user_stream

direct_message_manager

@@instances << self
Expand Down
103 changes: 54 additions & 49 deletions lib/twterm/streaming_client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'twterm/event/favorite'
require 'twterm/event/follow'
require 'twterm/event/notification'
require 'twterm/event/status/mention'
require 'twterm/event/status/timeline'
Expand All @@ -12,71 +13,75 @@ module StreamingClient
CONSUMER_SECRET = 'OFLKzrepRG2p1hq0nUB9j2S9ndFQoNTPheTpmOY0GYw55jGgS5'.freeze

def connect_user_stream
streaming_client.stop_stream
return if user_stream_connected?

@streaming_thread = Thread.new do
begin
publish(Event::Notification.new(:message, 'Trying to connect to Twitter...'))
streaming_client.userstream
rescue EventMachine::ConnectionError
publish(Event::Notification.new(:error, 'Connection failed'))
streaming_client.user do |event|
keep_alive!

case event
when Twitter::Tweet
status = Status.new(event)
publish(Event::Status::Timeline.new(status))
publish(Event::Status::Mention.new(status)) if status.text.include?('@%s' % screen_name)
when Twitter::Streaming::Event
case event.name
when :favorite
user = User.new(event.source)
status = Status.new(event.target_object)

event = Event::Favorite.new(user, status, self)
publish(event)
when :follow
source = User.new(event.source)
target = User.new(event.target)

event = Event::Follow.new(source, target, self)

publish(event)
end
when Twitter::DirectMessage
when Twitter::Streaming::FriendList
user_stream_connected!
when Twitter::Streaming::DeletedTweet
publish(Event::Status::Delete.new(event.id))
end
end
rescue Twitter::Error::TooManyRequests
publish(Event::Notification.new(:error, 'Rate limit exceeded'))
sleep 120
retry
rescue Errno::ENETUNREACH, Resolv::ResolvError
publish(Event::Notification.new(:error, 'Network is unavailable'))
sleep 30
retry
rescue Twitter::Error => e
publish(Event::Notification.new(:error, e.message))
end
end
end

def initialize_user_stream
return if user_stream_initialized?

streaming_client.on_friends do
user_stream_connected!
end

streaming_client.on_timeline_status do |tweet|
status = Status.new(tweet)
publish(Event::Status::Timeline.new(status))
publish(Event::Status::Mention.new(status)) if status.text.include?('@%s' % screen_name)
end

streaming_client.on_delete do |status_id|
publish(Event::StatusDeleted.new(status_id))
end

streaming_client.on_event(:favorite) do |event|
user = User.new(Twitter::User.new(event[:source]))
status = Status.new(Twitter::Status.new(event[:target_object]))

event = Event::Favorite.new(user, status, self)
publish(event)
end

streaming_client.on_event(:follow) do |event|
source = User.new(Twitter::User.new(event[:source]))
target = User.new(Twitter::User.new(event[:target]))

event = Event::Follow.new(source, target, self)
publish(:followed, event)
end
private

streaming_client.on_no_data_received do
user_stream_disconnected!
def keep_alive!
@keep_alive_timer.kill if @keep_alive_timer.is_a?(Thread)
@keep_alive_timer = Thread.new do
sleep(120)
@user_stream_connected = false
@streaming_thread.kill
connect_user_stream
end

user_stream_initialized!
end

private

def streaming_client
@streaming_client ||= TweetStream::Client.new(
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
oauth_token: @access_token,
oauth_token_secret: @access_token_secret,
auth_method: :oauth
)
@streaming_client ||= Twitter::Streaming::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = @access_token
config.access_token_secret = @access_token_secret
end
end

def user_stream_connected?
Expand Down
3 changes: 1 addition & 2 deletions twterm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'launchy', '~> 2.4.3'
spec.add_dependency 'oauth', '~> 0.5.1'
spec.add_dependency 'toml-rb', '~> 0.3.14'
spec.add_dependency 'tweetstream', '~> 2.6.1'
spec.add_dependency 'twitter', '~> 5.16.0'
spec.add_dependency 'twitter', '~> 6.0.0'
spec.add_dependency 'twitter-text', '~> 1.14.0'

spec.add_development_dependency 'bundler', '~> 1.8'
Expand Down