slack_twitter_egosa 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: 20cb89475551124bd3738ab671a3b58b2c1516a4
4
+ data.tar.gz: c49833318eadb942e2ac12e350ed57e7df6738c0
5
+ SHA512:
6
+ metadata.gz: ef947064fcb62537fc15417e72f88070c9081649212c5bdb705cc6e02b4e4837660c5b1cc807e455f32ca44cf71ec7cfb391100aaa096376b1a2d57ea9f0dfab
7
+ data.tar.gz: 77493e065b07f252f580b7ac3c27a2e07bf0be3c2ddbd95ae054d6d8fdd1690ffaf08a66e2d67ce2b86eca16caca6ab3dcab6eac8e903dfb0c7f42d863882a5b
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+
12
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Dockerfile ADDED
@@ -0,0 +1,17 @@
1
+ FROM ruby:latest
2
+
3
+ MAINTAINER ABCanG <abcang1015@gmail.com>
4
+
5
+ # timezone
6
+ RUN cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
7
+
8
+ RUN mkdir -p /app
9
+ WORKDIR /app
10
+
11
+ COPY Gemfile /app/
12
+ COPY Gemfile.lock /app/
13
+ RUN bundle install --deployment
14
+
15
+ COPY . /app
16
+
17
+ CMD ruby ./main.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slack_twitter_egosa.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 ABCanG
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.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ slack-twitter-egosa
2
+ ===
3
+
4
+ [![](https://images.microbadger.com/badges/version/abcang/slack-twitter-egosa.svg)](http://microbadger.com/images/abcang/slack-twitter-egosa "Get your own version badge on microbadger.com")
5
+
6
+ TwitterでエゴサしてSlackに投稿します。
7
+
8
+ ## 環境
9
+ * Bundler
10
+ * Ruby
11
+
12
+ ## 設定
13
+ 各環境変数に値を設定します
14
+
15
+ * `WEBHOOK_URL`: 投稿先のslackのIncoming WebhooksのURL
16
+ * `CONSUMER_KEY`: TwitterのConsumer Key
17
+ * `CONSUMER_SECRET`: TwitterのConsumer Secret
18
+ * `OAUTH_TOKEN`: TwitterのAccess Token
19
+ * `OAUTH_TOKEN_SECRET`: TwitterのAccess Token Secret
20
+ * `USER_STREAM_WORDS`: 自分のタイムラインから検索するワード。スペース区切りで複数指定可能。
21
+ * `FILTER_STREAM_WORDS`: Twitter全体から検索するワード。スペース区切りで複数指定可能。
22
+ * `MUTE_USERS`: エゴサ対象外にするユーザ。スペース区切りで複数指定可能。
23
+
24
+ ## 普通に起動
25
+
26
+ ```bash
27
+ $ git clone https://github.com/ABCanG/slack-twitter-egosa.git
28
+ $ cd slack-twitter-egosa
29
+ $ bundle install --path vendor/bundle
30
+ $ cat > .env
31
+ WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXX
32
+ CONSUMER_KEY=XXXXXXXX
33
+ CONSUMER_SECRET=XXXXXXXX
34
+ OAUTH_TOKEN=XXXXXXXX
35
+ OAUTH_TOKEN_SECRET=XXXXXXXX
36
+ USER_STREAM_WORDS=阿部 あべ
37
+ FILTER_STREAM_WORDS=abcang ABCanG1015 -@ABCanG1015
38
+ MUTE_USERS=ABCanG1015
39
+ ^D
40
+ $ ruby main.rb
41
+ ```
42
+
43
+ ## Dockerを使って起動
44
+
45
+ ```bash
46
+ docker run \
47
+ -e "WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXX" \
48
+ -e "CONSUMER_KEY=XXXXXXXX" \
49
+ -e "CONSUMER_SECRET=XXXXXXXX" \
50
+ -e "OAUTH_TOKEN=XXXXXXXX" \
51
+ -e "OAUTH_TOKEN_SECRET=XXXXXXXX" \
52
+ -e "USER_STREAM_WORDS=阿部 あべ" \
53
+ -e "FILTER_STREAM_WORDS=abcang ABCanG1015 -@ABCanG1015" \
54
+ -e "MUTE_USERS=ABCanG1015" \
55
+ abcang/slack-twitter-egosa
56
+ ```
57
+
58
+ ## 更新履歴
59
+ * 2016/08/23: 公開
60
+ * 2016/09/20: ミュートユーザ、除外キーワード機能を追加
61
+
62
+ ## ライセンス
63
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'slack_twitter_egosa'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'slack_twitter_egosa'
4
+
5
+ SlackTwitterEgosa.run(ARGV[0])
@@ -0,0 +1,31 @@
1
+ require 'slack/poster'
2
+
3
+ class SlackPoster
4
+ def initialize(url)
5
+ @poster = Slack::Poster.new(url)
6
+ @locker = Mutex::new
7
+ @pushed_ids = []
8
+ end
9
+
10
+ def post_status(status)
11
+ @locker.synchronize do
12
+ unless pushed?(status.id)
13
+ @poster.icon_url = status.user.profile_image_url
14
+ @poster.username = "#{status.user.name}(@#{status.user.screen_name})"
15
+ @poster.send_message("#{status.full_text}\n#{status.url}")
16
+ push(status.id)
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def pushed?(id)
24
+ @pushed_ids.include?(id)
25
+ end
26
+
27
+ def push(id)
28
+ @pushed_ids.push(id)
29
+ @pushed_ids.shift if @pushed_ids.size > 50
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ class UserFilter
2
+ attr_reader :mute_users
3
+
4
+ def initialize(users_text)
5
+ @mute_users = users_text.to_s.downcase.split(' ')
6
+ end
7
+
8
+ def match?(screen_name)
9
+ mute_users.include?(screen_name.downcase)
10
+ end
11
+
12
+ def unmatch?(screen_name)
13
+ !match?(screen_name)
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module SlackTwitterEgosa
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,40 @@
1
+ class WordManager
2
+ attr_reader :target, :exclude
3
+
4
+ def initialize(words_text)
5
+ @target = []
6
+ @exclude = []
7
+
8
+ words_text.to_s.dup.force_encoding('utf-8').split(' ').each do |word|
9
+ if word.start_with?('-')
10
+ exclude << word[1..-1]
11
+ else
12
+ target << word
13
+ end
14
+ end
15
+ end
16
+
17
+ def match?(text)
18
+ match_target?(text) && unmatch_exclude?(text)
19
+ end
20
+
21
+ def unmatch?(text)
22
+ !match?(text)
23
+ end
24
+
25
+ def match_target?(text)
26
+ !target.empty? && (text =~ /#{target.join('|')}/ ? true : false)
27
+ end
28
+
29
+ def unmatch_target?(text)
30
+ !match_target?(text)
31
+ end
32
+
33
+ def match_exclude?(text)
34
+ !exclude.empty? && (text =~ /#{exclude.join('|')}/ ? true : false)
35
+ end
36
+
37
+ def unmatch_exclude?(text)
38
+ !match_exclude?(text)
39
+ end
40
+ end
@@ -0,0 +1,98 @@
1
+ require 'slack_twitter_egosa/version'
2
+ require 'slack_twitter_egosa/slack_poster'
3
+ require 'slack_twitter_egosa/word_manager'
4
+ require 'slack_twitter_egosa/user_filter'
5
+
6
+ require 'dotenv'
7
+ require 'tweetstream'
8
+
9
+ module SlackTwitterEgosa
10
+ class << self
11
+ def run(envfile)
12
+ Dotenv.load(envfile) if envfile
13
+
14
+ lack_env = check_env(ENV.keys)
15
+ unless lack_env.empty?
16
+ STDERR.puts 'Not enough vnvironment variable'
17
+ lack_env.each do |env|
18
+ STDERR.puts " #{env}"
19
+ end
20
+ exit 1
21
+ end
22
+
23
+ init_twitter
24
+
25
+ threads = [
26
+ user_stream_thread,
27
+ filter_stream_thread
28
+ ]
29
+ threads.each(&:join)
30
+ end
31
+
32
+ private
33
+
34
+ def check_env(env_list)
35
+ target_env = %w(
36
+ CONSUMER_KEY
37
+ CONSUMER_SECRET
38
+ OAUTH_TOKEN
39
+ OAUTH_TOKEN_SECRET
40
+ WEBHOOK_URL
41
+ )
42
+ target_env - env_list
43
+ end
44
+
45
+ def init_twitter
46
+ TweetStream.configure do |config|
47
+ config.consumer_key = ENV['CONSUMER_KEY']
48
+ config.consumer_secret = ENV['CONSUMER_SECRET']
49
+ config.oauth_token = ENV['OAUTH_TOKEN']
50
+ config.oauth_token_secret = ENV['OAUTH_TOKEN_SECRET']
51
+ config.auth_method = :oauth
52
+ end
53
+ end
54
+
55
+ def poster
56
+ @poster ||= SlackPoster.new(ENV['WEBHOOK_URL'])
57
+ end
58
+
59
+ def user_stream
60
+ @user_stream ||= WordManager.new(ENV['USER_STREAM_WORDS'])
61
+ end
62
+
63
+ def filter_stream
64
+ @filter_stream ||= WordManager.new(ENV['FILTER_STREAM_WORDS'])
65
+ end
66
+
67
+ def mute_users
68
+ @mute_users ||= UserFilter.new(ENV['MUTE_USERS'])
69
+ end
70
+
71
+ def user_stream_thread
72
+ Thread.new do
73
+ TweetStream::Client.new.userstream do |status|
74
+ if status.retweet?
75
+ status = status.retweeted_status
76
+ next if status.user.following?
77
+ end
78
+
79
+ if mute_users.unmatch?(status.user.screen_name) &&
80
+ user_stream.match?(CGI.unescapeHTML(status.full_text))
81
+ poster.post_status(status)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def filter_stream_thread
88
+ Thread.new do
89
+ TweetStream::Client.new.track(*filter_stream.target) do |status|
90
+ if !status.retweet? && mute_users.unmatch?(status.user.screen_name) &&
91
+ filter_stream.unmatch_exclude?(CGI.unescapeHTML(status.full_text))
92
+ poster.post_status(status)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slack_twitter_egosa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'slack_twitter_egosa'
8
+ spec.version = SlackTwitterEgosa::VERSION
9
+ spec.authors = ['ABCanG']
10
+ spec.email = ['abcang1015@gmail.com']
11
+
12
+ spec.summary = 'Eegosearching twitter and post to slack.'
13
+ spec.description = 'Eegosearching twitter and post to slack.'
14
+ spec.homepage = 'https://github.com/ABCanG/slack-twitter-egosa'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'dotenv', '~> 2.1.1'
23
+ spec.add_dependency 'slack-poster', '~> 2.2.0'
24
+ spec.add_dependency 'tweetstream', '~> 2.6.1'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slack_twitter_egosa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ABCanG
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: slack-poster
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: tweetstream
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.6.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.6.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Eegosearching twitter and post to slack.
98
+ email:
99
+ - abcang1015@gmail.com
100
+ executables:
101
+ - slack_twitter_egosa
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Dockerfile
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - exe/slack_twitter_egosa
116
+ - lib/slack_twitter_egosa.rb
117
+ - lib/slack_twitter_egosa/slack_poster.rb
118
+ - lib/slack_twitter_egosa/user_filter.rb
119
+ - lib/slack_twitter_egosa/version.rb
120
+ - lib/slack_twitter_egosa/word_manager.rb
121
+ - slack_twitter_egosa.gemspec
122
+ homepage: https://github.com/ABCanG/slack-twitter-egosa
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.5.1
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Eegosearching twitter and post to slack.
146
+ test_files: []