From c29560c7874d00b728189d010758ed4b738d9d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=97=E4=91=93=E5=A6=82=E6=B3=95?= Date: Fri, 15 Jan 2016 22:42:52 +0900 Subject: [PATCH] initial commit --- .env.example | 4 +++ .gitignore | 40 ++++++++++++++++++++++++ Gemfile | 6 ++++ Gemfile.lock | 76 ++++++++++++++++++++++++++++++++++++++++++++ Procfile | 2 ++ bin/observe | 7 +++++ lib/initialize.rb | 5 +++ lib/util.rb | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 220 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Procfile create mode 100755 bin/observe create mode 100644 lib/initialize.rb create mode 100644 lib/util.rb diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ced34e2 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +TWITTER_CONSUMER_KEY= +TWITTER_CONSUMER_SECRET= +TWITTER_OAUTH_TOKEN= +TWITTER_OAUTH_TOKEN_SECRET= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6277cb3 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..d936dca --- /dev/null +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..34cc694 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..82dd6ca --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +MentionItToBalsers: bundle exec ruby bin/observe + diff --git a/bin/observe b/bin/observe new file mode 100755 index 0000000..0d509b4 --- /dev/null +++ b/bin/observe @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby -Ku +require File.join(".", File.dirname(__FILE__), "..", "lib", "initialize") + +require 'pry' + +Util.new.exec + diff --git a/lib/initialize.rb b/lib/initialize.rb new file mode 100644 index 0000000..b597b39 --- /dev/null +++ b/lib/initialize.rb @@ -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' diff --git a/lib/util.rb b/lib/util.rb new file mode 100644 index 0000000..9268c3e --- /dev/null +++ b/lib/util.rb @@ -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