Sales Guy Vs. Web Dude
Ruby 1.9
Awesome, check out the presentation by Bruce Williams.
IRB IRC Bot
While playing with _why's Try Ruby (in your browser) I wondered if it was possible for it to be used via an IRC Bot.
Here's what I came up with -
#/usr/bin/env ruby
['socket','open-uri','cgi'].each { |x| require x }
server = 'irc.freenode.net'
port = 6667
channel = '#ruby'
nick = 'irbot'
realname = 'irbot'
irburl = 'http://tryruby.hobix.com/irb?cmd='
session = open(irburl + '!INIT!IRB!').read
socket = TCPSocket.new server, port
["NICK #{nick}", "USER #{nick} 0 * :#{realname}"].each { |c| socket.puts c }
while true
socket.puts("JOIN #{channel}")
begin
while line = socket.gets
puts line ## Puts all IRC log live
case line
when /PING :(.+)$/i
socket.puts "PONG :#{$1}"
when /PRIVMSG ([^ :]+) +:(.+)/
m, sender,target, command = *line.match(/:([^!]*)![^ ].* +PRIVMSG ([^ :]+) +:(.+)/)
arg = command[/[^ ]+ +(.+)/, 1]
case command
when /reset session/
session = open(irburl + '!INIT!IRB!').read
when />>/
i = 0
open(irburl + CGI.escape(arg), 'Cookie' => '_session_id=' + session).each { |line|
i += 1
socket.puts "PRIVMSG #{channel} :" + line unless line.include? 'session'
}
end
end
end
rescue
end
end
Example:
James: >> a = [123,321,567]
irbot: => [123, 321, 567]
James: a.delete(123)
irbot: => 123
James: a
irbot: => [321, 567]
James: reset session # => starts a new IRB session
It's still in need of some TLC mind! Oh, and endless loops tend to break it until you reset the session :-)
Enjoy!
Edit: somebody on IRC told me about kirby, that does everything above - just better!
ExpanDrive

I recently found out about (and bought) ExpanDrive by Magnetk. It's simply a an application that lets you mount SFTP (SSH) servers. This means I can edit files on mounted servers as if they were local. It's also surprisingly fast!
It's $29 but there's also a coupon for $5 off - SHFVRDEFO1SGA3QI
Check it out!
The terminal speaks!
while true
system("say Leave!")
sleep(1)
end
The possibilities are endless!Parse IRC
s = ':JR!JR@programmr.net PRIVMSG #test-bot :!hey'
if /\A:([^ ]+)/ =~ s
@sender = $1
if /\A([^!]+)!([^!@]+)@([^@]+)\z/ =~ @sender
@sender_nick = $1
@sender_username = $2
@sender_address = $3
end
end
p @sender
p @sender_nick
p @sender_address
muxtape.rb
# Muxtape.rb - Gets the song URLs, Artists and Names from a Muxtape
# Derived from http://github.com/benschwarz/muxtape-rb/
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
require 'rubygems'
require 'hpricot'
require 'open-uri'
class MuxTape
def initialize
@host = "muxtape.com"
end
def songs(user)
# Here we retrieve the users page, strip out the Javascript to provide us with the Signiture
# and Hex for the Amazon URLs to work. Currently uses Open-URI to retrieve the Muxtape page,
# however please feel free to change (i.e cURL, Net/HTTP). Hpricot is used to scan the Muxtape
# page for specific elements needed to build the URLs.
begin
doc = Hpricot(open("http://#{user}.#{@host}"))
kettle = doc.search("//script[@type='text/javascript']").inner_text.match(/new Kettle\(\[(.+?)\],\[(.+?)\]\)/)
hex, sig = [kettle[1], kettle[2]].map do |a|
a.gsub("'",'').split(",")
end
out = []
if hex.size == sig.size
hex.size.times do |i|
track = doc.search("//li[@id='song#{hex[i]}']/div[@class='name']").inner_text.strip
if track =~ /(.+)\s-\s(.+)/
artist, title = [$1, $2]
end
url = "http://muxtape.s3.amazonaws.com/songs/#{hex[i]}?PLEASE=DO_NOT_STEAL_MUSIC{sig[i]}"
out << { :title => title, :artist => artist, :url => url}
end
end
out
rescue
end
end
end
# Usage:
@mux = Muxtape.new
@mux.songs('justin') # => ...
It's a cut down version of muxtape-rb. I use it on Simplicater too!Simplicater and Emotie
Check them out!
Uptime
>sh-2.05b# uptime
0:33 up 12 days, 12:45
Not good!
distance_of_time_in_words() (PHP)
# The PHP port of distance_of_time_in_words in RoR I made for twuut.com. Enjoy!
function distance_of_time_in_words($from_time,$to_time = 0, $include_seconds = false) {
$dm = $distance_in_minutes = abs(($from_time - $to_time))/60;
$ds = $distance_in_seconds = abs(($from_time - $to_time));
switch ($distance_in_minutes) {
case $dm > 0 && $dm < 1:
if($include_seconds == false) {
if ($dm == 0) {
return 'less than a minute';
} else {
return '1 minute';
}
} else {
switch ($distance_of_seconds) {
case $ds > 0 && $ds < 4:
return 'less than 5 seconds';
break;
case $ds > 5 && $ds < 9:
return 'less than 10 seconds';
break;
case $ds > 10 && $ds < 19:
return 'less than 20 seconds';
break;
case $ds > 20 && $ds < 39:
return 'half a minute';
break;
case $ds > 40 && $ds < 59:
return 'less than a minute';
break;
default:
return '1 minute';
break;
}
}
break;
case $dm > 2 && $dm < 44:
return round($dm) . ' minutes';
break;
case $dm > 45 && $dm < 89:
return 'about 1 hour';
break;
case $dm > 90 && $dm < 1439:
return 'about ' . round($dm / 60.0) . ' hours';
break;
case $dm > 1440 && $dm < 2879:
return '1 day';
break;
case $dm > 2880 && $dm < 43199:
return round($dm / 1440) . ' days';
break;
case $dm > 43200 && $dm < 86399:
return 'about 1 month';
break;
case $dm > 86400 && $dm < 525599:
return round($dm / 43200) . ' months';
break;
case $dm > 525600 && $dm < 1051199:
return 'about 1 year';
break;
default:
return 'over ' . round($dm / 525600) . ' years';
break;
}
}
Sorry about the dodgy highlighting, sorting that out shortly!
Next

