Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyoho committed Jan 15, 2016
0 parents commit c29560c
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_OAUTH_TOKEN=
TWITTER_OAUTH_TOKEN_SECRET=
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
### https://raw.github.com/github/gitignore/fceac113a3a20e00718d6317e468eec27f6e2d99/ruby.gitignore

*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

.env
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'
gem 'foreman'
gem 'userstream', :git => 'git://github.com/mitukiii/userstream.git'
gem 'twitter'
gem 'dotenv'
gem 'pry'
76 changes: 76 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
GIT
remote: git://github.com/mitukiii/userstream.git
revision: f37e7931f7f934422ae6cbdee10da40715c6b68b
specs:
userstream (1.4.0)
hashie (~> 1.2.0)
oauth (~> 0.4.7)
yajl-ruby (~> 1.1.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.4.0)
buftok (0.2.0)
coderay (1.1.0)
domain_name (0.5.25)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.1.0)
equalizer (0.0.10)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
foreman (0.78.0)
thor (~> 0.19.1)
hashie (1.2.0)
http (0.9.8)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 1.0.1)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
http-form_data (1.0.1)
http_parser.rb (0.6.0)
json (1.8.3)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (0.8.2)
multipart-post (2.0.0)
naught (1.1.0)
oauth (0.4.7)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
simple_oauth (0.3.1)
slop (3.6.0)
thor (0.19.1)
thread_safe (0.3.5)
twitter (5.15.0)
addressable (~> 2.3)
buftok (~> 0.2.0)
equalizer (= 0.0.10)
faraday (~> 0.9.0)
http (>= 0.4, < 0.10)
http_parser.rb (~> 0.6.0)
json (~> 1.8)
memoizable (~> 0.4.0)
naught (~> 1.0)
simple_oauth (~> 0.3.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.1)
yajl-ruby (1.1.0)

PLATFORMS
ruby

DEPENDENCIES
dotenv
foreman
pry
twitter
userstream!

BUNDLED WITH
1.11.2
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MentionItToBalsers: bundle exec ruby bin/observe

7 changes: 7 additions & 0 deletions bin/observe
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby -Ku
require File.join(".", File.dirname(__FILE__), "..", "lib", "initialize")

require 'pry'

Util.new.exec

5 changes: 5 additions & 0 deletions lib/initialize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
APPLICATION_ROOT = File.absolute_path(File.join(File.dirname(__FILE__),".."))
# puts "Application Root: %s" % APPLICATION_ROOT
$: << APPLICATION_ROOT

require 'lib/util'
80 changes: 80 additions & 0 deletions lib/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'dotenv'
require 'twitter'
require 'user_stream'

class Util
def initialize
Dotenv.load
setup_twitter
end

def setup_twitter
UserStream.configure do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.oauth_token = ENV['TWITTER_OAUTH_TOKEN']
config.oauth_token_secret = ENV['TWITTER_OAUTH_TOKEN_SECRET']
end

@client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_OAUTH_TOKEN']
config.access_token_secret = ENV['TWITTER_OAUTH_TOKEN_SECRET']
end
end

def exec
begin
# UserStream.client.sample({'lang'=>'ja'}) do |status|
UserStream.client.user do |status|
analyze status
end
rescue => ex
STDERR.puts ex
retry
rescue UserStream::RateLimited => ex
STDERR.puts 'sleep 300 second...'
sleep 300
retry
rescue Timeout::Error => ex
STDERR.puts ex.class
STDERR.puts ex
if ex.class == UserStream::RateLimited
STDERR.puts 'sleep 300 second...'
sleep 300
end
retry
end
end

def analyze status
user = status.user
text = status.text
if text and user
s = text.gsub(/([[:ascii:]]|[[:punct:]])/,'')
puts s
if text !~ /^RT / and text !~ /(@|at)/ and text !~ /http/ and text =~ /バルス/
if bals_density_high?(text)
puts user.screen_name
puts text
@client.update("at #{status.user.screen_name} " + random_message, in_reply_to_status_id: status.id)
@client.favorite(status.id)
#binding.pry
end
end
end
end

def random_message
array = ['ナイスバルス', 'これはいいバルス', 'いいバルスでしたよ〜']
array.sample + '!' * rand(1..10)
end

def bals_density_high? str
return false if str.length == 0
s = str.gsub(/([[:ascii:]]|[[:punct:]])/,'')
t = s.gsub(/バルス/,'')
t.length.to_f / s.length.to_f < 0.5
end
end

0 comments on commit c29560c

Please sign in to comment.