Skip to content

Instantly share code, notes, and snippets.

@erutaso
Created March 21, 2014 13:17
Show Gist options
  • Save erutaso/9685994 to your computer and use it in GitHub Desktop.
Save erutaso/9685994 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'twitter'
require 'date'
require 'nkf'
require './key.rb'
require './timer.rb'
@rest_client = Twitter::REST::Client.new do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.access_token = Const::ACCESS_TOKEN
config.access_token_secret = Const::ACCESS_TOKEN_SECRET
end
@stream_client = Twitter::Streaming::Client.new do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.access_token = Const::ACCESS_TOKEN
config.access_token_secret = Const::ACCESS_TOKEN_SECRET
end
@dic = {'朝' => 'first', '昼' => 'second', '夜' => 'third'}
@today = Date.today
@last_update = nil
def meshi(status)
@dic.each{ |key, value|
if status.text.include?("@hoge #{key}")
menu = "./#{value}.txt"
text= status.text.sub("@hoge #{key}","")
text = text.gsub(/(\s| )+/, "")
text = text.gsub(/日/,"")
text = NKF.nkf('-m0Z1 -w',text)
case text
when /今/
open(menu){ |file|
body = file.readlines[@today.day - 1]
}
when /明/
if @today.day == Date.new(@today.year, @today.month, -1).day
body = "今日は月末です。献立表の更新までお待ちください。"
next
else
open(menu){ |file|
body = file.readlines[@today.day]
}
end
else
open(menu){ |file|
body = file.readlines[text.to_i - 1]
}
end
opt = {"in_reply_to_status_id"=>status.id.to_s}
tweet = "@#{status.user.screen_name} #{body}"
@rest_client.update tweet,opt
end
}
end
def tweet
d = DateTime.now
return if @last_update and @last_update.hour == d.hour
if d.hour == 07
open("./first.txt"){ |file|
body = file.readlines[@today.day - 1]
}
end
if d.hour == 11
open("./second.txt"){ |file|
body = file.readlines[@today.day - 1]
}
end
if d.hour == 17
open("./third.txt"){ |file|
body = file.readlines[@today.day - 1]
}
end
tweet = "#{d.month}月#{d.day}日の朝の献立は#{body}です。"
@rest_client.update tweet
@last_update = d
end
timer = Timer.new(10){
Thread.new(){
p "ok"
tweet
}
}
timer.start()
@stream_client.user do |object|
next unless object.is_a? Twitter::Tweet
unless object.text.start_with? "RT"
meshi(object)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment