twitter2fleep 0.4.0 → 0.4.1
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 +4 -4
- data/lib/twitter2fleep/bot.rb +34 -32
- data/lib/twitter2fleep/cli.rb +43 -41
- data/lib/twitter2fleep/version.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a9f8a35ba6701ab3d7dd0b394f1f1bc5386062c
|
4
|
+
data.tar.gz: fbdfdf4da6b6348e1b347bf7e61792f6e5cc7c69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b9ee4e9b3501f1e5fad1a9202329fe7fb09eb6653d1025f58589f272eb2504eff73b423cf9cfed59792346331edc8daba16eae1edca66b337956371612d87d8
|
7
|
+
data.tar.gz: a05e5da34da8861d2dd3d69351e649a8f78bc5700b125d6c2e0a4185925ce7991455f0cee89e170ce936a75c66ea70c825f4d418ff0c07599f4bfc84dad98eac
|
data/lib/twitter2fleep/bot.rb
CHANGED
@@ -1,48 +1,50 @@
|
|
1
1
|
require 'twitter'
|
2
2
|
require 'http'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module Twitter2Fleep
|
5
|
+
class Bot
|
6
|
+
def initialize(config)
|
7
|
+
@twitter_client = Twitter::Streaming::Client.new(config[:twitter])
|
8
|
+
@selected_user_ids = config[:selected_user_ids]
|
9
|
+
@fleep_hook_url = config[:fleep_hook_url]
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
def start
|
13
|
+
@twitter_client.user do |object|
|
14
|
+
case object
|
15
|
+
when Twitter::Tweet
|
16
|
+
tweet = object
|
17
|
+
author = tweet.user
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
if should_post?(tweet)
|
20
|
+
message = "@#{author.screen_name}: #{tweet.text}"
|
20
21
|
|
21
|
-
|
22
|
+
response = post_to_fleep(author.screen_name, message)
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
puts message
|
25
|
+
puts "----> #{response.status_code}"
|
26
|
+
end
|
27
|
+
when Twitter::Streaming::StallWarning
|
28
|
+
warn "Falling behind!"
|
25
29
|
end
|
26
|
-
when Twitter::Streaming::StallWarning
|
27
|
-
warn "Falling behind!"
|
28
30
|
end
|
29
31
|
end
|
30
|
-
end
|
31
32
|
|
32
|
-
|
33
|
+
private
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
def should_post?(tweet)
|
36
|
+
@selected_user_ids.nil? or (
|
37
|
+
@selected_user_ids.include?(tweet.user.id) and (
|
38
|
+
not tweet.reply? or @selected_user_ids.include?(tweet.in_reply_to_user_id)
|
39
|
+
)
|
38
40
|
)
|
39
|
-
|
40
|
-
end
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
def post_to_fleep(display_name, message)
|
44
|
+
HTTP.post(
|
45
|
+
"#{@fleep_hook_url}/#{display_name}",
|
46
|
+
:form => {:message => message}
|
47
|
+
).response
|
48
|
+
end
|
47
49
|
end
|
48
50
|
end
|
data/lib/twitter2fleep/cli.rb
CHANGED
@@ -3,51 +3,53 @@ require 'twitter2fleep/bot'
|
|
3
3
|
require 'thor'
|
4
4
|
require 'yaml'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
6
|
+
module Twitter2Fleep
|
7
|
+
class CLI < Thor
|
8
|
+
desc "start", "Start the bot"
|
9
|
+
option :config_file, :default => "#{Dir.home}/.twitter2fleep/config.yml"
|
10
|
+
option :env_config, :type => :boolean, :default => false
|
11
|
+
|
12
|
+
def start
|
13
|
+
if options[:env_config]
|
14
|
+
puts "Load config from environment variables."
|
15
|
+
config = load_env_config
|
16
|
+
else
|
17
|
+
config_file = options[:config_file]
|
18
|
+
puts "Load config from \`#{config_file}\`."
|
19
|
+
config = load_config_file(config_file)
|
20
|
+
end
|
21
|
+
bot = Twitter2Fleep::Bot.new(config)
|
22
|
+
bot.start
|
19
23
|
end
|
20
|
-
bot = Twitter2Fleep::Bot.new(config)
|
21
|
-
bot.start
|
22
|
-
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
25
|
+
no_commands do
|
26
|
+
def load_env_config
|
27
|
+
{
|
28
|
+
:twitter => {
|
29
|
+
:consumer_key => ENV.fetch('TWITTER_CONSUMER_KEY', ''),
|
30
|
+
:consumer_secret => ENV.fetch('TWITTER_CONSUMER_SECRET', ''),
|
31
|
+
:access_token => ENV.fetch('TWITTER_ACCESS_TOKEN', ''),
|
32
|
+
:access_token_secret => ENV.fetch('TWITTER_ACCESS_TOKEN_SECRET', ''),
|
33
|
+
},
|
34
|
+
:selected_user_ids => ENV.fetch('SELECTED_USER_IDS', '').split(',').map {|v| v.to_i},
|
35
|
+
:fleep_hook_url => ENV.fetch('FLEEP_HOOK_URL', '')
|
36
|
+
}
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
def load_config_file(filename)
|
40
|
+
config_file = options[:config_file]
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
begin
|
43
|
+
config = YAML::load_file(config_file)
|
44
|
+
rescue Errno::ENOENT => exception
|
45
|
+
puts "You need to create the config file \`#{config_file}\`"
|
46
|
+
puts "See example at https://github.com/wancw/twitter2fleep/blob/master/config.yml.example"
|
47
|
+
exit(-1)
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
+
return config
|
51
|
+
end
|
50
52
|
end
|
51
|
-
end
|
52
53
|
|
53
|
-
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module Twitter2Fleep
|
2
|
-
VERSION = '0.4.
|
3
|
-
end
|
2
|
+
VERSION = '0.4.1'
|
3
|
+
end
|