botolo 0.0.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58566737798a3a88e686a40d223d8e7e27db3840
4
- data.tar.gz: 4492226808e3b9eb85ef63adc6e776df0d284584
3
+ metadata.gz: 3700d7909e38446f210f833a5056d4b16f9f84ae
4
+ data.tar.gz: 14ef0e0ee8af888d8c4c7f82bee3f6f266c3057f
5
5
  SHA512:
6
- metadata.gz: e89d7ce37f0a8e0828e0ff6864592bf0584e3c6ff232e7575841670f7e4b7d3f511903faa1d7e0d1ffdf06361d80383aa208d27c941e74e12a9d147b13dcaa77
7
- data.tar.gz: 39a4ca966a71e777a9dc7933592558c9936aeba12c6e28e13420aa3c2057fda9e1a5a79eab4d97d620b92f680536703fa4492e2f7c334cb360cd9653f1a7e514
6
+ metadata.gz: 347960ca6554b41963cd1075001df23de440e0483f8536052998bdf53460e744223f474ae7b4292831a12035fa2c506122d9204a5fc1fb352e7f30adcaeb8a35
7
+ data.tar.gz: 62bae3351919d3093e4dab14c4254cc3ed0ab6ec2f1177531859e1f947b3427170d0299f7581156bb5ad35c1ff8986c6b91077bc9463388d0f1219296e3b0b2a
data/bin/botolo ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'botolo'
4
+ require 'openssl'
5
+ require 'codesake_commons'
6
+
7
+ DEFAULT_BEHAVIOUR = "./lib/botolo/bot/behaviour.rb"
8
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
9
+
10
+ $logger = Codesake::Commons::Logging.instance
11
+ trap("INT") { $logger.helo('bot is shutting down'); Kernel.exit(0) }
12
+
13
+ behaviour_file = DEFAULT_BEHAVIOUR
14
+ config_file = nil
15
+ config_file = ARGV[0] if ARGV.count == 1
16
+
17
+ $logger.die "usage: botolo bot_configuration_file" if config_file.nil?
18
+
19
+ bot = Botolo::Bot::Engine.new({:config=>config_file})
20
+ $logger.log "#{bot.name} is online" if bot.online?
21
+ $logger.log "#{bot.name} is offline" unless bot.online?
22
+ bot.run if bot.online?
23
+
24
+
25
+
data/botolo.gemspec CHANGED
@@ -20,4 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_dependency "twitter"
24
+ spec.add_dependency "codesake_commons"
23
25
  end
@@ -0,0 +1,11 @@
1
+ module Botolo
2
+ module Bot
3
+ class Behaviour
4
+
5
+ def initialize(options={})
6
+ @name = options[:name]
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,102 @@
1
+ require 'twitter'
2
+ require 'yaml'
3
+
4
+ module Botolo
5
+ module Bot
6
+
7
+ # This is the main bot class, it is responsible of:
8
+ # * reading configuration
9
+ # * using Twitter APIs
10
+ # * do something
11
+ class Engine
12
+
13
+ def initialize(options={})
14
+ @start_time = Time.now
15
+ @online = false
16
+ @config = read_conf(options[:config])
17
+ authenticate
18
+ @tasks = @config['task']
19
+ behaviour = File.join(".", @config['bot']['behaviour']) unless @config['bot']['behaviour'].nil?
20
+
21
+ $logger.helo "#{name} v#{version} is starting up"
22
+
23
+ begin
24
+ load behaviour
25
+ $logger.log "using #{behaviour} as bot behaviour"
26
+ @behaviour = Botolo::Bot::Behaviour.new({:name=>name})
27
+ rescue => e
28
+ $logger.err(e.message)
29
+ require 'botolo/bot/behaviour'
30
+ $logger.log "reverting to default dummy behaviour"
31
+ @behaviour = Botolo::Bot::Behaviour.new({:name=>name})
32
+ end
33
+
34
+
35
+ end
36
+
37
+ def calc_sleep_time(task)
38
+
39
+ s = /every (\d) (s|m|h|d|w|y)/.match task
40
+
41
+ return 300 if s.nil? # safe fallback is 5 minutes sleeping
42
+
43
+ return s[1].to_i if s[2] == 's'
44
+ return s[1].to_i * 60 if s[2] == 'm'
45
+ return s[1].to_i * 60 * 60 if s[2] == 'h'
46
+ return s[1].to_i * 60 * 60 * 24 if s[2] == 'd'
47
+ return s[1].to_i * 60 * 60 * 24 * 7 if s[2] == 'w'
48
+ return s[1].to_i * 60 * 60 * 24 * 7 * 52 if s[2] == 'y'
49
+
50
+ end
51
+
52
+ def run
53
+ $logger.log "entering main loop"
54
+ while true
55
+ @tasks.each do |task|
56
+ begin
57
+ @behaviour.send(task["action"].to_sym) if @behaviour.respond_to? task["action"].to_sym
58
+ rescue => e
59
+ $logger.err "#{task["action"]} failed (#{e.message})"
60
+ end
61
+ sleep calc_sleep_time(task["schedule"])
62
+ end
63
+ end
64
+
65
+ end
66
+ def authenticate
67
+ begin
68
+ Twitter.configure do |config|
69
+ config.consumer_key = @config['twitter']['consumer_key']
70
+ config.consumer_secret = @config['twitter']['consumer_secret']
71
+ config.oauth_token = @config['twitter']['oauth_token']
72
+ config.oauth_token_secret = @config['twitter']['oauth_token_secret']
73
+ end
74
+ @online = true
75
+ rescue Exception => e
76
+ $logger.err e.message
77
+ end
78
+ end
79
+
80
+ def online?
81
+ @online
82
+ end
83
+
84
+ def name
85
+ return @config['bot']['name']
86
+ end
87
+ def version
88
+ return @config['bot']['version']
89
+ end
90
+
91
+ def uptime
92
+ Time.now - @start_time
93
+ end
94
+
95
+ def read_conf(filename=nil)
96
+ return {} if filename.nil? or ! File.exist?(filename)
97
+ return YAML.load_file(filename)
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module Botolo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.10.0"
3
3
  end
data/lib/botolo.rb CHANGED
@@ -1,5 +1,2 @@
1
1
  require "botolo/version"
2
-
3
- module Botolo
4
- # Your code goes here...
5
- end
2
+ require "botolo/bot/engine"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: botolo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Perego
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-27 00:00:00.000000000 Z
11
+ date: 2013-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,10 +38,39 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: twitter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: codesake_commons
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: botolo is a bot engine written in ruby
42
70
  email:
43
71
  - thesp0nge@gmail.com
44
- executables: []
72
+ executables:
73
+ - botolo
45
74
  extensions: []
46
75
  extra_rdoc_files: []
47
76
  files:
@@ -50,8 +79,11 @@ files:
50
79
  - LICENSE.txt
51
80
  - README.md
52
81
  - Rakefile
82
+ - bin/botolo
53
83
  - botolo.gemspec
54
84
  - lib/botolo.rb
85
+ - lib/botolo/bot/behaviour.rb
86
+ - lib/botolo/bot/engine.rb
55
87
  - lib/botolo/version.rb
56
88
  homepage: http://codesake.com
57
89
  licenses: