daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'yaml'
  2. require 'MeCab'
  3. require 'pp'
  4. require 'rubygems'
  5. require 'twitter'
  6.  
  7. class MyTwitter
  8.  
  9.   def initialize
  10.     config = "setting.yml"
  11.     begin
  12.       yaml = YAML.load_file(config)
  13.     rescue Errno::ENOENT
  14.       puts "#{config} is not found"
  15.       exit
  16.     end
  17.     @database = yaml['database'] || self.alert('database')
  18.     @last_file = yaml['last'] || self.alert('last')
  19.     @username = yaml['twitter']['username'] || self.alert('twitter username')
  20.     @password = yaml['twitter']['password'] || self.alert('twitter password')
  21.     @exclude_names = yaml['exclude_names'] || Array.new
  22.     @hashtag = yaml['hashtag'] || self.alert('hashtag')
  23.     @follow_reply = yaml['follow_reply'] || false
  24.     @timeline_follow = yaml['timeline_follow'] || false
  25.   end
  26.  
  27.   def stack_ids
  28.     @stack_ids = @stack_ids || Array.new
  29.   end
  30.  
  31.   def twitter
  32.     @twitter = @twitter || Twitter::Base.new(Twitter::HTTPAuth.new(@username, @password))
  33.   end
  34.  
  35.   def friend_ids
  36.     @friend_ids = @friend_ids || self.twitter.friend_ids
  37.   end
  38.  
  39.   def diff_follow
  40.     diff_ids = self.friend_ids - self.twitter.follower_ids
  41.     diff_ids.each do |id|
  42.       self.stack_id(id)
  43.     end
  44.   end
  45.  
  46.   def stack_id(id)
  47.      self.stack_ids.push(id) if !self.friend_ids.index(id) && !self.stack_ids.index(id)
  48.   end
  49.  
  50.   def stack_follow
  51.     self.stack_ids.each do |id|
  52.       self.twitter.friendship_create(id, true)
  53.       puts "follow: #{id}"
  54.       sleep(1)
  55.     end
  56.   end
  57.  
  58.   def run str
  59.     self.diff_follow if @follow_reply
  60.     data = self.crawl
  61.     if str == "go"
  62.       word = self.markov data
  63.       puts "markov word: #{word}"
  64.       self.post word
  65.     end
  66.     self.stack_follow
  67.   end
  68.  
  69.   protected
  70.   def alert str
  71.     puts "#{str} is not found"
  72.     puts "exit"
  73.   end
  74.  
  75.   def save_last str
  76.     file = File.open(@last_file, 'w')
  77.     file.puts str
  78.     file.close
  79.   end
  80.  
  81.   def get_last
  82.     begin
  83.       f = open(@last_file)
  84.       last = f.read.chomp.to_i
  85.       f.close
  86.     rescue
  87.       last = nil
  88.     end
  89.     return last
  90.   end
  91.  
  92.   def save_data data
  93.     f = open(@database, 'w')
  94.     f.puts Marshal.dump(data)
  95.     f.close
  96.   end
  97.  
  98.   def get_data
  99.     return (File.file? @database) ? Marshal.restore(open(@database, 'r')) : Array.new
  100.   end
  101.  
  102.   def post str
  103.     if str.split(//u).size <= 140
  104.       self.twitter.update(str)
  105.     else
  106.       puts "over 140"
  107.     end
  108.   end
  109.  
  110.   def crawl
  111.     page = 1
  112.     first = true
  113.     modify = false
  114.     last = self.get_last
  115.     mecab = MeCab::Tagger.new("-Owakati")
  116.     data = self.get_data
  117.  
  118.     catch :done do
  119.       while(true)
  120.         results = Twitter::Search.new(@hashtag).page(page).fetch().results
  121.         break if results.empty?
  122.         puts "Loading #{page} page"
  123.         results.each do |result|
  124.           next if @exclude_names.index result.from_user
  125.           throw :done if result.id <= last
  126.           self.stack_id Twitter.user(result.from_user).id if @timeline_follow
  127.           puts "new twiit: #{result.text}"
  128.           modify = true
  129.           save_last result.id if first == true
  130.           first = false
  131.           mecab.parse('HEAD' + result.text.sub(/#{@hashtag}/, '') + "EOS").split(' ').each_cons(3) do |a|
  132.             data.push h = {'head' => a[0], 'middle' => a[1], 'end' => a[2]}
  133.           end
  134.         end
  135.         page += 1
  136.       end
  137.     end
  138.    
  139.  
  140.     self.save_data data if modify
  141.     return data
  142.   end
  143.  
  144.   def markov data
  145.     t1 = ''
  146.     t2 = ''
  147.  
  148.     data.sort_by{rand}.each do |a|
  149.       if a['head'] == 'HEAD'
  150.         next if a['middle'] =‾ /^@.*$/
  151.         t1 = a['middle']
  152.         t2 = a['end']
  153.         break
  154.       end
  155.     end
  156.  
  157.     new_text = t1 + t2
  158.  
  159.     while true
  160.       _a = Array.new
  161.       data.each do |hash|
  162.         _a.push hash if hash['head'] == t1 && hash['middle'] == t2
  163.       end
  164.  
  165.       break if _a.size == 0
  166.       num = rand(_a.size)
  167.       new_text = new_text + _a[num]['end']
  168.       break if _a[num]['end'] == "EOS"
  169.       t1 = _a[num]['middle']
  170.       t2 = _a[num]['end']
  171.     end
  172.  
  173.     word = new_text.gsub(/EOS$/, " #{@hashtag}")
  174.     return word
  175.   end
  176. end
  177.  
  178. main = MyTwitter.new
  179. main.run ARGV.shift
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top