ruboty-twitter_tl 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: aca31d8c1f92579683f013a5e1a46ef94f00ac90
4
+ data.tar.gz: ccc5d68ce575c588c245f1efec4726ee3682b857
5
+ SHA512:
6
+ metadata.gz: b84c591fd917dbc1d78ecb8572ae9f0b374c5e09c8363e0c32b6d269e25b9acb672596ae76eaaa694cabb46ef8f0c4a28f49bd35b95505107063baa7c7e4671c
7
+ data.tar.gz: 19ffc583e7c820945df2b3cf062f38e6f309ca18340460332ea9974143a36a89daa8f38534f0ec9998cb6e4a9b12b343575e09d2545a9d8ee9e82a0bd3fd62f5
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::TwitterTL
2
+
3
+ Ruboty handler to follow a user on twitter.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-twitter_tl'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ```
20
+ > @ruboty follow tweets by haccht
21
+ Followed '@haccht'
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 following
28
+ @haccht
29
+
30
+ > @ruboty unfollow tweets by haccht
31
+ Unfollowed '@haccht'
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,69 @@
1
+ module Ruboty
2
+ module Handlers
3
+ class TwitterTL < Base
4
+ on(/follow tweets by (?<name>.+)/,
5
+ name: 'follow',
6
+ description: 'Follow a user on twitter.')
7
+
8
+ on(/unfollow tweets by (?<name>.+)/,
9
+ name: 'unfollow',
10
+ description: 'Unfollow a user on twitter.')
11
+
12
+ on(/list tweets following\z/,
13
+ name: 'following',
14
+ description: 'List following users on twitter.')
15
+
16
+ def initialize(*args)
17
+ super
18
+
19
+ @stream = Ruboty::TwitterTL::Stream.new(robot)
20
+ @stream.start(users)
21
+ end
22
+
23
+ def follow(message)
24
+ username = message[:name]
25
+ userinfo = {
26
+ message: message.original.except(:robot),
27
+ twitter_id: @stream.rest.user(username).id
28
+ }
29
+
30
+ if users.has_value?(userinfo)
31
+ message.reply("Already following '@#{username}'.")
32
+ else
33
+ users.update(username => userinfo)
34
+ @stream.update(users)
35
+ message.reply("Followed '@#{username}'.")
36
+ end
37
+ rescue Twitter::Error::NotFound
38
+ message.reply("'@#{username}' is not found.")
39
+ rescue Twitter::Error::Forbidden
40
+ message.reply("Unable to verify your credentials.")
41
+ end
42
+
43
+ def unfollow(message)
44
+ username = message[:name]
45
+
46
+ if userinfo = users.delete(username)
47
+ @stream.update(users)
48
+ message.reply("Unfollowed '@#{username}'.")
49
+ else
50
+ message.reply("'@#{username}' is not found.")
51
+ end
52
+ end
53
+
54
+ def following(message)
55
+ if users.empty?
56
+ message.reply("Following no user.")
57
+ else
58
+ list = users.select { |_, userinfo| userinfo[:message][:from] == message.original[:from] }
59
+ text = list.keys.map { |username| "@#{username}" }.join("\n")
60
+ message.reply(text, code: true)
61
+ end
62
+ end
63
+
64
+ def users
65
+ robot.brain.data[Ruboty::TwitterTL::NAMESPACE] ||= {}
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ require 'ruboty'
2
+ require 'ruboty/twitter_tl/stream'
3
+ require 'ruboty/handlers/twitter_tl
4
+
5
+ module Ruboty
6
+ module TwitterTL
7
+ NAMESPACE = 'twitter_tl'
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,79 @@
1
+ require 'tweetstream'
2
+
3
+ module Ruboty
4
+ module TwitterTL
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(users)
38
+ return if users.empty?
39
+
40
+ Thread.start do
41
+ @client.follow(*q(users)) do |object|
42
+ if userinfo = users[object.user.screen_name]
43
+ message = Message.new(userinfo[:message].merge(robot: @robot))
44
+ message.reply(u(object))
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def update(users)
51
+ return start(users) unless @client.stream
52
+
53
+ @client.stream.update(params: {:follow => q(users)})
54
+ end
55
+
56
+ def rest
57
+ @rest ||= Twitter::REST::Client.new do |config|
58
+ config.consumer_key = CONSUMER_KEY
59
+ config.consumer_secret = CONSUMER_SECRET
60
+ config.access_token = ACCESS_TOKEN
61
+ config.access_token_secret = ACCESS_TOKEN_SECRET
62
+ end
63
+ end
64
+
65
+ private
66
+ def q(users)
67
+ users.values.map { |userinfo| userinfo[:twitter_id] }
68
+ end
69
+
70
+ def u(object)
71
+ "https://twitter.com/#{object.user.screen_name}/status/#{object.id}"
72
+ end
73
+
74
+ def log(level, message)
75
+ Ruboty.logger.send(level, "[#{Time.now}] ruboty-twitter_tl: #{message}")
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module TwitterTL
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
3
+ require 'ruboty/twitter_tl/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruboty-twitter_tl'
7
+ spec.version = Ruboty::TwitterTL::VERSION
8
+ spec.authors = ['haccht']
9
+ spec.email = ['haccht00@gmail.com']
10
+ spec.summary = "Ruboty handler to follow a user on twitter."
11
+ spec.description = spec.summary
12
+ spec.homepage = 'https://github.com/haccht/ruboty-twitter_tl'
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_tl
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 follow a user on twitter.
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_tl.rb
82
+ - lib/ruboty/twitter_tl.rb
83
+ - lib/ruboty/twitter_tl/stream.rb
84
+ - lib/ruboty/twitter_tl/version.rb
85
+ - ruboty-tweets.gemspec
86
+ homepage: https://github.com/haccht/ruboty-twitter_tl
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 follow a user on twitter.
110
+ test_files: []