ruboty-twitter_track 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 332ecf6d38e4fcc8edf2677bc119ec27c5278bce
4
+ data.tar.gz: 6581c64efec90384b6c63d94d3ebce48c7b0c925
5
+ SHA512:
6
+ metadata.gz: eed785ff8df72589a7c020bce99bdac1ed7c5945dc94b54bf1995f29a5a4e9b8a359235f3cc9d2364f563b14dc821cd9453ba8346c8c4b5b70a1bf28c71a8e5f
7
+ data.tar.gz: 2c1976543de864f235e14b7119380e70775565186aba3b88d9638e2b574f38ee1b83c7107aa7e666612bdceafcdbae188c375889b4e723fa80611384c9b42fbc
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /pkg/
4
+ /vendor/
5
+ *.bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 haccht
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Ruboty::TwitterTrack
2
+
3
+ Ruboty handler to track terms in the twitter stream.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-twitter_track'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ```
20
+ > @ruboty track tweets by ruboty
21
+ Tracked 'ruboty'
22
+
23
+ @ruboty> https://twitter.com/haccht/status/123456789
24
+ @ruboty> https://twitter.com/haccht/status/123456790
25
+ @ruboty> https://twitter.com/haccht/status/123456791
26
+
27
+ > @ruboty list tweets tracking
28
+ @haccht
29
+
30
+ > @ruboty untrack tweets by ruboty
31
+ Untracked 'ruboty'
32
+ ```
33
+
34
+ ## Env
35
+
36
+ Requires twitter access tokens.
37
+ https://apps.twitter.com
38
+
39
+ ```
40
+ export CONSUMER_KEY=
41
+ export CONSUMER_SECRET=
42
+ export ACCESS_TOKEN=
43
+ export ACCESS_TOKEN_SECRET=
44
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,70 @@
1
+ module Ruboty
2
+ module Handlers
3
+ class TwitterTrack < Base
4
+ on(/track tweets by (?<term>.+)\z/,
5
+ name: 'track',
6
+ description: 'Track tweets in the twitter stream.')
7
+
8
+ on(/untrack tweets by (?<term>.+)\z/,
9
+ name: 'untrack',
10
+ description: 'Untrack tweets in the twitter stream.')
11
+
12
+ on(/list tweets tracking\z/,
13
+ name: 'tracking',
14
+ description: 'List tracking tweets in the twitter stream.')
15
+
16
+ def initialize(*args)
17
+ super
18
+
19
+ @stream = Ruboty::TwitterTrack::Stream.new(robot)
20
+ @stream.start(tracking_terms)
21
+ end
22
+
23
+ def track(message)
24
+ message[:term].split(',').map { |e| e.strip }.each do |term|
25
+ if tracking_terms.include?(term)
26
+ message.reply("Already trackedd '#{term}'.")
27
+ else
28
+ tracking_terms << term
29
+ message.reply("Tracked '#{term}'.")
30
+ end
31
+ end
32
+
33
+ begin
34
+ @stream.restart(tracking_terms)
35
+ rescue Twitter::Error::Forbidden
36
+ message.reply("Unable to verify your credentials.")
37
+ end
38
+ end
39
+
40
+ def untrack(message)
41
+ message[:term].split(',').map { |e| e.strip }.each do |term|
42
+ if tracking_terms.include?(term)
43
+ tracking_terms.delete(term)
44
+ message.reply("Untracked '#{term}'.")
45
+ else
46
+ message.reply("'#{term}' has not tracked.")
47
+ end
48
+ end
49
+
50
+ begin
51
+ @stream.restart(tracking_terms)
52
+ rescue Twitter::Error::Forbidden
53
+ message.reply("Unable to verify your credentials.")
54
+ end
55
+ end
56
+
57
+ def tracking(message)
58
+ if tracking_terms.empty?
59
+ message.reply("tracking no terms.")
60
+ else
61
+ tracking_terms.each { |term| message.reply(term, code:true) }
62
+ end
63
+ end
64
+
65
+ def tracking_terms
66
+ robot.brain.data[Ruboty::TwitterTrack::NAMESPACE] ||= []
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,63 @@
1
+ require 'tweetstream'
2
+
3
+ module Ruboty
4
+ module TwitterTrack
5
+ class Stream
6
+
7
+ def initialize(robot)
8
+ TweetStream.configure do |config|
9
+ config.consumer_key = CONSUMER_KEY
10
+ config.consumer_secret = CONSUMER_SECRET
11
+ config.oauth_token = ACCESS_TOKEN
12
+ config.oauth_token_secret = ACCESS_TOKEN_SECRET
13
+ config.auth_method = :oauth
14
+ end
15
+
16
+ @robot = robot
17
+ @client = TweetStream::Client.new
18
+
19
+ @client.on_inited do
20
+ log(:info , "Connected to twitter stream.")
21
+
22
+ # Every day reconnect the stream to prevent from shutdown.
23
+ EM::PeriodicTimer.new(60 * 60 * 24) do
24
+ @client.stream.immediate_reconnect
25
+ end
26
+ end
27
+
28
+ @client.on_reconnect do |timeout, retrial|
29
+ log(:info , "Reconnected to twitter stream: #{timeout} sec.")
30
+ end
31
+
32
+ @client.on_error do |message|
33
+ log(:error, message)
34
+ end
35
+ end
36
+
37
+ def start(terms)
38
+ return if terms.empty?
39
+
40
+ Thread.start do
41
+ @client.track(*terms) do |object|
42
+ message = Message.new(robot: @robot)
43
+ message.reply(u(object))
44
+ end
45
+ end
46
+ end
47
+
48
+ def restart(terms)
49
+ return start(terms) unless @client.stream
50
+ @client.stream.update(params: {:track => terms.join(',')})
51
+ end
52
+
53
+ private
54
+ def u(object)
55
+ "https://twitter.com/#{object.user.screen_name}/status/#{object.id}"
56
+ end
57
+
58
+ def log(level, message)
59
+ Ruboty.logger.send(level, "[#{Time.now}] ruboty-twitter_track: #{message}")
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module TwitterTrack
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'ruboty'
2
+ require 'ruboty/twitter_track/stream'
3
+ require 'ruboty/handlers/twitter_track'
4
+
5
+ module Ruboty
6
+ module TwitterTrack
7
+ NAMESPACE = 'twitter_track'
8
+
9
+ CONSUMER_KEY = ENV['CONSUMER_KEY']
10
+ CONSUMER_SECRET = ENV['CONSUMER_SECRET']
11
+ ACCESS_TOKEN = ENV['ACCESS_TOKEN']
12
+ ACCESS_TOKEN_SECRET = ENV['ACCESS_TOKEN_SECRET']
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
3
+ require 'ruboty/twitter_track/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruboty-twitter_track'
7
+ spec.version = Ruboty::TwitterTrack::VERSION
8
+ spec.authors = ['haccht']
9
+ spec.email = ['haccht00@gmail.com']
10
+ spec.summary = "Ruboty handler to track tweets in the twitter stream."
11
+ spec.description = spec.summary
12
+ spec.homepage = 'https://github.com/haccht/ruboty-twitter_track'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'ruboty'
21
+ spec.add_runtime_dependency 'tweetstream'
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'rake'
24
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-twitter_track
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - haccht
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tweetstream
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ruboty handler to track tweets in the twitter stream.
70
+ email:
71
+ - haccht00@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/ruboty/handlers/twitter_track.rb
82
+ - lib/ruboty/twitter_track.rb
83
+ - lib/ruboty/twitter_track/stream.rb
84
+ - lib/ruboty/twitter_track/version.rb
85
+ - ruboty-twitter_track.gemspec
86
+ homepage: https://github.com/haccht/ruboty-twitter_track
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.5
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Ruboty handler to track tweets in the twitter stream.
110
+ test_files: []