twittbot 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 145bf3689fa42e0b74cb05aa13a33708fd23d3f9
4
- data.tar.gz: edc22c44e18f9466a0903e8571d2ec87ef1a6f95
3
+ metadata.gz: 22b58f70f78a48918691c29b693ff73a2c5f3ba0
4
+ data.tar.gz: def4b5eac4b779e0d38743ebc7c9df5b77653ea0
5
5
  SHA512:
6
- metadata.gz: c2c06111fea59970c01c4ba73064286375d32a61ddd8d2650e08f913b7c712a8b55a72e139bf7ebad6259751260fb62ac1b2335edbd572b90e3aa52e9497e254
7
- data.tar.gz: 03f7cc0f9365cbe1888870bc36d39799403f6ae9d9a1655f09743ce5696e410d8a44ebc475deb4713bdda89f839499a2ad04288d4f9b5eae8d76eea5b2b7890d
6
+ metadata.gz: 50277aecaa7a5db1ac8eb02fcd777def98a37204ef367174d84f125bff11a4cc7b702133a7c5a9cadeb8de07fb2e938fe46e9c759973217a7542601470387297
7
+ data.tar.gz: 9482a62c929960385b853dd603a222c881ba6d7b7d41c84ca7fedc015bda6526361cc2f6aa83a8baacab7dc048efd8f15228bd543ba522cc4f7ffa61c7e5ebc8
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ ## 0.4.0
4
+
5
+ * Add `twittbot cron` for interacting with the bot outside from Twittbot processes, e.g. via cron
6
+ * Modify `retweet-bot` template to ignore retweets from ourselves
7
+ * Add new `big-ben-clock` template which demonstrates the usage of `twittbot cron`.
8
+
9
+ ## 0.3.0
10
+
11
+ * Add `on :load` event.
12
+
13
+ ## 0.2.0
14
+
15
+ * Bot callbacks are now stored in an array
16
+
17
+ ## 0.1.2
18
+
19
+ * Automatically reconnect to user streams after 5 seconds
20
+
21
+ ## 0.1.1
22
+
23
+ * Fix EOFError occurring when streams are open for a long time
24
+
25
+ ## 0.1.0
26
+
27
+ * Add `add-admin` and `del-admin` commands
28
+ * Add support for direct messages
29
+ * Add new BotPart method for commands over direct messages
30
+ * Add a basic direct message template
31
+ * Add ability to save config for each BotPart
32
+ * Add example direct message command to `random-tweet` template
33
+
34
+ ## 0.0.1
35
+
36
+ * Initial release
@@ -24,7 +24,9 @@ module Twittbot
24
24
  config: Twittbot::DEFAULT_BOT_CONFIG.merge(
25
25
  YAML.load_file(File.expand_path("./#{Twittbot::CONFIG_FILE_NAME}", @options[:current_dir]))
26
26
  ),
27
- periodic: []
27
+ periodic: [],
28
+ save_config: true,
29
+ tasks: {}
28
30
  }.merge!(options)
29
31
 
30
32
  load_bot_code
@@ -32,7 +34,7 @@ module Twittbot
32
34
  at_exit do
33
35
  save_config
34
36
  $bot[:botparts].each { |b| b.save_config }
35
- end
37
+ end if $bot[:save_config]
36
38
  end
37
39
 
38
40
  # Authenticates an account with Twitter.
@@ -103,7 +105,7 @@ module Twittbot
103
105
  @periodic_thread ||= Thread.new do
104
106
  loop do
105
107
  begin
106
- do_periodic
108
+ Thread.new { do_periodic }
107
109
  rescue => _
108
110
  end
109
111
  sleep 60
@@ -117,6 +119,20 @@ module Twittbot
117
119
  @periodic_thread.join
118
120
  end
119
121
 
122
+ # Runs the task `task_name`.
123
+ # @param task_name [Symbol] The name of the task to be run.
124
+ def cron(task_name)
125
+ task = $bot[:tasks][task_name]
126
+ unless task
127
+ say "Task \"#{task_name}\" does not exist. Use \"twittbot cron list\" for a list of available tasks."
128
+ return
129
+ end
130
+
131
+ #check_config
132
+ init_clients
133
+ task[:block].call
134
+ end
135
+
120
136
  # @param screen_name [String] the user's screen name
121
137
  # @param action [Symbol] :add or :delete
122
138
  def modify_admin(screen_name, action = :add)
@@ -137,6 +153,7 @@ module Twittbot
137
153
  # Loads the bot's actual code which is stored in the bot's +lib+
138
154
  # subdirectory.
139
155
  def load_bot_code
156
+ load_special_tasks
140
157
  files = Dir["#{File.expand_path('./lib', @options[:current_dir])}/**/*"]
141
158
  files.each do |file|
142
159
  require_relative file.sub(/\.rb$/, '') if file.end_with? '.rb'
@@ -176,7 +193,7 @@ module Twittbot
176
193
  is_mention = (object.user.screen_name != $bot[:config][:screen_name] and object.text.include?("@" + $bot[:config][:screen_name]) and not object.retweet?)
177
194
  do_callbacks :retweet, object, opts if object.retweet? and object.retweeted_tweet.user.screen_name == $bot[:config][:screen_name]
178
195
  do_callbacks :mention, object, opts if is_mention
179
- do_callbacks :tweet, object, opts.merge({ mention: is_mention, retweet: object.retweet? })
196
+ do_callbacks :tweet, object, opts.merge({ mention: is_mention, retweet: object.retweet?, favorite: object.favorited? })
180
197
  when Twitter::Streaming::Event
181
198
  case object.name
182
199
  when :follow, :favorite
@@ -210,7 +227,7 @@ module Twittbot
210
227
 
211
228
  def do_periodic
212
229
  $bot[:periodic].each_with_index do |h, i|
213
- h[:remaining] = if h[:remaining] == 0
230
+ h[:remaining] = if h[:remaining] - 1 <= 0
214
231
  h[:block].call
215
232
  h[:interval]
216
233
  else
@@ -267,5 +284,28 @@ module Twittbot
267
284
  oauth_response = access_token.get('/1.1/account/verify_credentials.json?skip_status=true')
268
285
  oauth_response.body.match(/"screen_name"\s*:\s*"(.*?)"/).captures.first
269
286
  end
287
+
288
+ # Load special tasks (e.g. list)
289
+ def load_special_tasks
290
+ $bot[:tasks][:list] = {
291
+ block: -> do
292
+ list_tasks
293
+ end,
294
+ desc: 'List all available tasks'
295
+ }
296
+ end
297
+
298
+ # Lists all tasks
299
+ def list_tasks
300
+ tasks = $bot[:tasks].map do |name, value|
301
+ [name, value[:desc]]
302
+ end.to_h
303
+ longest_name = tasks.keys.map(&:to_s).max { |a, b| a.length <=> b.length }.length
304
+ tasks = tasks.map do |name, desc|
305
+ "twittbot cron %-*s # %s" % [longest_name + 2, name, desc]
306
+ end.sort
307
+
308
+ puts tasks
309
+ end
270
310
  end
271
311
  end
@@ -90,6 +90,26 @@ module Twittbot
90
90
  }
91
91
  end
92
92
 
93
+ # Defines a new task to be run outside of a running Twittbot process.
94
+ # @param name [Symbol] The name of the task.
95
+ # @param options [Hash] A customizable set of options.
96
+ # @option options [String] :desc ("") Description of this task
97
+ def task(name, options = {}, &block)
98
+ name = name.to_s.downcase.to_sym
99
+ task_re = /\A[a-z0-9.:-_]+\z/
100
+ raise "Task already exists: #{name}" if $bot[:tasks].include?(name)
101
+ raise "Task name does not match regexp #{task_re.to_s}" unless name.to_s.match(task_re)
102
+
103
+ opts = {
104
+ desc: ''
105
+ }.merge(options)
106
+
107
+ $bot[:tasks][name] ||= {
108
+ block: block,
109
+ desc: opts[:desc]
110
+ }
111
+ end
112
+
93
113
  # Saves the botpart's configuration. This is automatically called when
94
114
  # Twittbot exits.
95
115
  def save_config
@@ -102,4 +122,4 @@ module Twittbot
102
122
  end
103
123
  end
104
124
  end
105
- end
125
+ end
@@ -26,6 +26,13 @@ module Twittbot
26
26
  bot.start
27
27
  end
28
28
 
29
+ desc 'cron TASK_NAME', 'Runs the task TASK_NAME, useful for calling from cron'
30
+ def cron(task_name)
31
+ require 'twittbot/bot'
32
+ bot = Twittbot::Bot.new(save_config: false)
33
+ bot.cron(task_name.downcase.to_sym)
34
+ end
35
+
29
36
  desc 'generate TEMPLATE_NAME', 'Installs a template'
30
37
  def generate(template_name)
31
38
  require 'twittbot/generators/templates/template_generator'
@@ -1,6 +1,6 @@
1
1
  module Twittbot
2
2
  # The version of Twittbot.
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
 
5
5
  CONSUMER_KEY = 'FYRuQcDbPAXAyVjuPZMuw' # :nodoc:
6
6
  CONSUMER_SECRET = 'KiLCYTftPdxNebl5DNcj7Ey2Y8YVZu7hfqiFRYkcg' # :nodoc:
@@ -23,4 +23,4 @@ module Twittbot
23
23
 
24
24
  # How many seconds should be waited until the bot reconnects
25
25
  RECONNECT_WAIT_TIME = 5
26
- end
26
+ end
@@ -26,6 +26,19 @@ module Twitter
26
26
  rescue Twitter::Error => e
27
27
  puts "caught Twitter error while retweeting: #{e.message}"
28
28
  end
29
+ alias rt retweet
30
+
31
+ # Favourites a tweet
32
+ def favourite
33
+ return if $bot.nil? or $bot[:client].nil?
34
+ $bot[:client].favorite self.id
35
+ rescue Twitter::Error => e
36
+ puts "caught Twitter error while favouriting: #{e.message}"
37
+ end
38
+ alias fav favourite
39
+ alias fave favourite
40
+ # for the 'muricans
41
+ alias favorite favourite
29
42
 
30
43
  # Scans the tweet text for screen names.
31
44
  # @param reply_all [Boolean] Include all users in the reply.
@@ -0,0 +1,22 @@
1
+ <%
2
+ # This is the config for this botpart.
3
+ # It is a hash and will be serialized to YAML and saved to 'etc/template_name.yml'.
4
+ botpart_config = {
5
+ string_to_repeat: 'BONG'
6
+ }
7
+
8
+ # Display a message after installation template installation
9
+ post_install_message <<-MSG
10
+ Note: You installed a botpart which contains a task.
11
+
12
+ A task can only be called outside of a running Twittbot process, e.g. from an
13
+ UNIX shell or cron.
14
+ MSG
15
+ %>
16
+ Twittbot::BotPart.new :<%= @template_name %> do
17
+ task :bong, desc: 'Big Ben Clock bong!' do
18
+ repeats = Time.now.hour % 12
19
+ repeats = 12 if repeats == 0
20
+ bot.tweet "#{@config[:string_to_repeat]} " * repeats
21
+ end
22
+ end
@@ -20,6 +20,8 @@ MSG
20
20
  Twittbot::BotPart.new :<%= @template_name %> do
21
21
  # When there's a new tweet in the timeline...
22
22
  on :tweet do |tweet, opts|
23
+ # ... ignore this tweet if it is from ourselves ...
24
+ next if tweet.user.screen_name == @config[:screen_name]
23
25
  # ... check if the tweet text contains at least one of the tags defined in your config...
24
26
  @config[:tags].each do |tag|
25
27
  if tweet.text.downcase.include? tag.downcase
@@ -29,4 +31,4 @@ Twittbot::BotPart.new :<%= @template_name %> do
29
31
  end
30
32
  end
31
33
  end
32
- end
34
+ end
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'thor', '~> 0.19'
22
22
  spec.add_dependency 'erubis', '~> 2.7', '>= 2.7.0'
23
- spec.add_dependency 'oauth', '~> 0.4'
24
- spec.add_dependency 'twitter', '~> 5.14'
25
- spec.add_development_dependency 'yard', '~> 0.8'
26
- spec.add_development_dependency 'bundler', '~> 1.7'
27
- spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_dependency 'oauth', '~> 0.5'
24
+ spec.add_dependency 'twitter', '~> 5.16'
25
+ spec.add_development_dependency 'yard', '~> 0.9'
26
+ spec.add_development_dependency 'bundler', '>= 1.7'
27
+ spec.add_development_dependency 'rake', '~> 11.3'
28
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twittbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nilsding
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-29 00:00:00.000000000 Z
11
+ date: 2016-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -50,54 +50,54 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0.4'
53
+ version: '0.5'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0.4'
60
+ version: '0.5'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: twitter
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '5.14'
67
+ version: '5.16'
68
68
  type: :runtime
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '5.14'
74
+ version: '5.16'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: yard
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '0.8'
81
+ version: '0.9'
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: '0.8'
88
+ version: '0.9'
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: bundler
91
91
  requirement: !ruby/object:Gem::Requirement
92
92
  requirements:
93
- - - "~>"
93
+ - - ">="
94
94
  - !ruby/object:Gem::Version
95
95
  version: '1.7'
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
- - - "~>"
100
+ - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: '1.7'
103
103
  - !ruby/object:Gem::Dependency
@@ -106,14 +106,14 @@ dependencies:
106
106
  requirements:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '10.0'
109
+ version: '11.3'
110
110
  type: :development
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
- version: '10.0'
116
+ version: '11.3'
117
117
  description: Twittbot is an advanced Twitter bot. See the README for more info.
118
118
  email:
119
119
  - nilsding@nilsding.org
@@ -124,6 +124,7 @@ extra_rdoc_files: []
124
124
  files:
125
125
  - ".gitignore"
126
126
  - ".yardopts"
127
+ - CHANGELOG.md
127
128
  - Gemfile
128
129
  - LICENSE.txt
129
130
  - README.md
@@ -140,6 +141,7 @@ files:
140
141
  - lib/twittbot/gem_ext/twitter/tweet.rb
141
142
  - lib/twittbot/gem_ext/twitter/user.rb
142
143
  - lib/twittbot/generators/templates/basic_dm/basic_dm.rb
144
+ - lib/twittbot/generators/templates/big_ben_clock/big_ben_clock.rb
143
145
  - lib/twittbot/generators/templates/followback/followback.rb
144
146
  - lib/twittbot/generators/templates/random_reply/random_reply.rb
145
147
  - lib/twittbot/generators/templates/random_tweet/random_tweet.rb
@@ -173,9 +175,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
175
  version: '0'
174
176
  requirements: []
175
177
  rubyforge_project:
176
- rubygems_version: 2.4.8
178
+ rubygems_version: 2.5.1
177
179
  signing_key:
178
180
  specification_version: 4
179
181
  summary: An advanced Twitter bot.
180
182
  test_files: []
181
- has_rdoc: