|
@@ -49,14 +49,16 @@ class Channel::Driver::Twitter
|
|
|
|
|
|
options = check_external_credential(options)
|
|
|
|
|
|
- @tweet = Tweet.new(options[:auth])
|
|
|
- @sync = options[:sync]
|
|
|
- @channel = channel
|
|
|
+ # check if stream scheduler is already running and return
|
|
|
+
|
|
|
+ @rest_client = TweetRest.new(options[:auth])
|
|
|
+ @sync = options[:sync]
|
|
|
+ @channel = channel
|
|
|
|
|
|
Rails.logger.debug 'twitter fetch started'
|
|
|
|
|
|
- fetch_search
|
|
|
fetch_mentions
|
|
|
+ fetch_search
|
|
|
fetch_direct_messages
|
|
|
|
|
|
disconnect
|
|
@@ -65,6 +67,7 @@ class Channel::Driver::Twitter
|
|
|
|
|
|
{
|
|
|
result: 'ok',
|
|
|
+ notice: '',
|
|
|
}
|
|
|
end
|
|
|
|
|
@@ -94,78 +97,136 @@ class Channel::Driver::Twitter
|
|
|
|
|
|
options = check_external_credential(options)
|
|
|
|
|
|
- @tweet = Tweet.new(options[:auth])
|
|
|
- tweet = @tweet.from_article(article)
|
|
|
+ @rest_client = TweetRest.new(options[:auth])
|
|
|
+ tweet = @rest_client.from_article(article)
|
|
|
disconnect
|
|
|
tweet
|
|
|
end
|
|
|
|
|
|
def disconnect
|
|
|
- @tweet.disconnect
|
|
|
+ @stream_client.disconnect if @stream_client
|
|
|
+ @rest_client.disconnect if @rest_client
|
|
|
+ end
|
|
|
+
|
|
|
+ def stream_instance(channel)
|
|
|
+ @channel = channel
|
|
|
+ options = @channel.options
|
|
|
+ @stream_client = TweetStream.new(options[:auth])
|
|
|
+ end
|
|
|
+
|
|
|
+ def stream
|
|
|
+ hashtags = []
|
|
|
+ @channel.options['sync']['search'].each {|item|
|
|
|
+ hashtags.push item['term']
|
|
|
+ }
|
|
|
+ filter = {
|
|
|
+ track: hashtags.join(','),
|
|
|
+ }
|
|
|
+ if @channel.options['sync']['mentions']['group_id'] != ''
|
|
|
+ filter[:replies] = 'all'
|
|
|
+ end
|
|
|
+
|
|
|
+ @stream_client.client.user(filter) do |tweet|
|
|
|
+ next if tweet.class != Twitter::Tweet && tweet.class != Twitter::DirectMessage
|
|
|
+ next if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
+
|
|
|
+ # check direct message
|
|
|
+ if tweet.class == Twitter::DirectMessage
|
|
|
+ if @channel.options['sync']['direct_messages']['group_id'] != ''
|
|
|
+ next if direct_message_limit_reached(tweet)
|
|
|
+ @stream_client.to_group(tweet, @channel.options['sync']['direct_messages']['group_id'], @channel)
|
|
|
+ end
|
|
|
+ next
|
|
|
+ end
|
|
|
+
|
|
|
+ next if @stream_client.tweet_limit_reached(tweet)
|
|
|
+
|
|
|
+ # check if it's mention
|
|
|
+ if @channel.options['sync']['mentions']['group_id'] != ''
|
|
|
+ hit = false
|
|
|
+ if tweet.user_mentions
|
|
|
+ tweet.user_mentions.each {|user|
|
|
|
+ if user.id.to_s == @channel.options['user']['id'].to_s
|
|
|
+ hit = true
|
|
|
+ end
|
|
|
+ }
|
|
|
+ end
|
|
|
+ if hit
|
|
|
+ @stream_client.to_group(tweet, @channel.options['sync']['mentions']['group_id'], @channel)
|
|
|
+ next
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ # check hashtags
|
|
|
+ if @channel.options['sync']['search'] && tweet.hashtags
|
|
|
+ hit = false
|
|
|
+ @channel.options['sync']['search'].each {|item|
|
|
|
+ tweet.hashtags.each {|hashtag|
|
|
|
+ next if item['term'] !~ /^#/
|
|
|
+ if item['term'].sub(/^#/, '') == hashtag.text
|
|
|
+ hit = item
|
|
|
+ end
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if hit
|
|
|
+ @stream_client.to_group(tweet, hit['group_id'], @channel)
|
|
|
+ next
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ # check stings
|
|
|
+ if @channel.options['sync']['search']
|
|
|
+ hit = false
|
|
|
+ body = tweet.text
|
|
|
+ @channel.options['sync']['search'].each {|item|
|
|
|
+ next if item['term'] =~ /^#/
|
|
|
+ if body =~ /#{item['term']}/
|
|
|
+ hit = item
|
|
|
+ end
|
|
|
+ }
|
|
|
+ if hit
|
|
|
+ @stream_client.to_group(tweet, hit['group_id'], @channel)
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
private
|
|
|
|
|
|
def fetch_search
|
|
|
-
|
|
|
return if !@sync[:search]
|
|
|
return if @sync[:search].empty?
|
|
|
-
|
|
|
- # search results
|
|
|
@sync[:search].each { |search|
|
|
|
-
|
|
|
result_type = search[:type] || 'mixed'
|
|
|
-
|
|
|
Rails.logger.debug " - searching for '#{search[:term]}'"
|
|
|
-
|
|
|
- counter = 0
|
|
|
- @tweet.client.search(search[:term], result_type: result_type).collect { |tweet|
|
|
|
-
|
|
|
- break if @sync[:limit] && @sync[:limit] <= counter
|
|
|
- break if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
-
|
|
|
- @tweet.to_group(tweet, search[:group_id], @channel)
|
|
|
-
|
|
|
- counter += 1
|
|
|
+ @rest_client.client.search(search[:term], result_type: result_type).collect { |tweet|
|
|
|
+ next if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
+ break if @rest_client.tweet_limit_reached(tweet)
|
|
|
+ @rest_client.to_group(tweet, search[:group_id], @channel)
|
|
|
}
|
|
|
}
|
|
|
end
|
|
|
|
|
|
def fetch_mentions
|
|
|
-
|
|
|
return if !@sync[:mentions]
|
|
|
return if @sync[:mentions].empty?
|
|
|
-
|
|
|
Rails.logger.debug ' - searching for mentions'
|
|
|
-
|
|
|
- counter = 0
|
|
|
- @tweet.client.mentions_timeline.each { |tweet|
|
|
|
-
|
|
|
- break if @sync[:limit] && @sync[:limit] <= counter
|
|
|
- break if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
-
|
|
|
- @tweet.to_group(tweet, @sync[:mentions][:group_id], @channel)
|
|
|
-
|
|
|
- counter += 1
|
|
|
+ @rest_client.client.mentions_timeline.each { |tweet|
|
|
|
+ next if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
+ break if @rest_client.tweet_limit_reached(tweet)
|
|
|
+ @rest_client.to_group(tweet, @sync[:mentions][:group_id], @channel)
|
|
|
}
|
|
|
end
|
|
|
|
|
|
def fetch_direct_messages
|
|
|
-
|
|
|
return if !@sync[:direct_messages]
|
|
|
return if @sync[:direct_messages].empty?
|
|
|
-
|
|
|
Rails.logger.debug ' - searching for direct_messages'
|
|
|
-
|
|
|
- counter = 0
|
|
|
- @tweet.client.direct_messages.each { |tweet|
|
|
|
-
|
|
|
- break if @sync[:limit] && @sync[:limit] <= counter
|
|
|
- break if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
-
|
|
|
- @tweet.to_group(tweet, @sync[:direct_messages][:group_id], @channel)
|
|
|
-
|
|
|
- counter += 1
|
|
|
+ @rest_client.client.direct_messages.each { |tweet|
|
|
|
+ next if Ticket::Article.find_by(message_id: tweet.id)
|
|
|
+ break if @rest_client.direct_message_limit_reached(tweet)
|
|
|
+ @rest_client.to_group(tweet, @sync[:direct_messages][:group_id], @channel)
|
|
|
}
|
|
|
end
|
|
|
|