Created
December 22, 2015 20:14
-
-
Save panchiga/9be6ccaa5d75a4b70ff6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "twitter" | |
require 'open3' | |
OAUTH_token = "" | |
OAUTH_token_secret = "" | |
$user = Twitter::REST::Client.new do |config| | |
config.consumer_key = "" | |
config.consumer_secret = "" | |
config.access_token = OAUTH_token | |
config.access_token_secret = OAUTH_token_secret | |
end | |
########ここまでTwitterの設定 | |
#mypath | |
mypath = `pwd` | |
$patharr = mypath.split("/") | |
puts "start" | |
def HERE | |
return $patharr.join("/") | |
end | |
def cd (cdarr) | |
if cdarr[1] == ".." | |
$patharr.pop() | |
elsif (cdarr[1] == "~") || cdarr.size == 1 | |
home_dir = `echo ~` | |
arr = home_dir.split("/") | |
$patharr = arr | |
elsif cdarr[1] == "/" | |
arr = Array.new() | |
arr.push("/") | |
$patharr = arr | |
else | |
path = HERE() +"/" + cdarr[1] | |
if File.exist?(path) | |
$patharr.push(cdarr[1]) | |
else | |
puts "#{path} is not found" | |
end | |
end | |
end | |
def dot_to_path (arr) | |
arr.times do |i| | |
barr = arr[i].split("/") | |
if barr.size != 1 | |
bindex = barr.index(".") | |
barr[bindex] = HERE() | |
end | |
arr[i] = barr.join("/") | |
end | |
index = arr.index(".") | |
arr[index] = HERE() | |
return arr | |
end | |
def init_dm_get | |
begin | |
$DM_GET = $user.direct_messages.size | |
rescue Twitter::Error::TooManyRequests => error | |
sleep error.rate_limit.reset_in | |
end | |
end | |
def dm_get | |
post_msg = "" | |
loop do | |
begin | |
if $DM_GET == nil | |
init_dm_get() | |
end | |
if $user.direct_messages.size > $DM_GET | |
dms = $user.direct_messages | |
diff = dms.size - $DM_GET | |
$DM_GET += 1 | |
post_msg = dms[diff - 1].text.strip | |
puts post_msg | |
break | |
end | |
sleep(30) | |
rescue Twitter::Error::TooManyRequests => error | |
sleep error.rate_limit.reset_in | |
end | |
end | |
return post_msg | |
end | |
def dm_me(str) | |
begin | |
$user.direct_message_create("C_C_panchiga", str, options={}) | |
rescue Twitter::Error::TooManyRequests => error | |
sleep error.rate_limit.reset_in | |
end | |
end | |
####ここからmain | |
init_dm_get() | |
#loop | |
loop do | |
com_str = dm_get() | |
#com_str = gets | |
com_arr = com_str.split(" ") | |
#ループをぬけ出す | |
if com_arr[0] == "exit" | |
exit | |
end | |
# . を今のパスに置換 | |
if com_arr.index(".") != nil | |
com_arr = dot_to_path(com_arr) | |
end | |
#cdは特殊 | |
if com_arr.index("cd") != nil | |
cd(com_arr) | |
next | |
#pwdも特殊 | |
elsif com_arr.index("pwd") != nil | |
index = com_arr.index("pwd") | |
com_arr[index] = "echo #{HERE()}" | |
end | |
command = com_arr.join(" ") | |
begin | |
o, e, s = Open3.capture3(command) | |
if o != "" | |
dm_me(o) | |
else | |
dm_me(e) | |
end | |
rescue => exc | |
dm_me(exc) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment