
Probably, fine answer means project is going well for someone, but for real worker it might mean something else.
#googleio.rb
#!/usr/bin/env ruby
module TextToBinary
def TextToBinary.encode(text)
text.scan(/./m) do |char|
(@bin ||= []) << char.unpack('B8').first.scan(/./m).map { |bit| bit = bit.eql?('1') ? '0' : 'o' }
end
return @bin
end
end
name = 'GOOGLEIO'
on_tshirt = 'GOOGLEKO'
outputs = TextToBinary.encode(name)
puts "-----#{name}-----"
outputs.each { |out| p out }
puts "BUT!"
puts "-----#{on_tshirt}----"
result = TextToBinary.encode(on_tshirt)
result.each { |res| p res }
#! /usr/bin/env ruby
################################################
# pre-installed
# 1. sudo gem install activesupport
# 2. sudo gem install hpricot
# 3. sudo gem install gchart
# How to run?
# username = 'whoyouare'
# city = 'whereareyou'
# tweets = Tweet.new(username, :local => city) #
# tweets.stats(:of => :last_24hrs)
# just for fun script, dont' trust the result :P
#################################################
require 'rubygems'
require 'activesupport'
require 'hpricot'
require 'open-uri'
require 'gchart'
class Tweet
def initialize(username, options={})
@username = username
@twittr_url = "http://twitter.com/#{@username}"
@page = 1
@doc = Hpricot(twittr_page)
@tweets = [current_tweet]
@tweets.concat(page_tweets)
local = options[:local]
locality(local) unless local.nil?
end
def locality(city)
# where else you could add.. i'm lazy...
case city
when 'bangkok'
@zone_offset = 7
when 'london', 'paris'
@zone_offset = 1
when 'boston'
@zone_offset = -4
else
@zone_offset = 0
end
end
def twittr_page
page = ""
open("#{@twittr_url}?page=#{@page}",
"User-Agent" => "Ruby/#{RUBY_VERSION}",
"From" => "zdk",
"Referer" => "http://ziddik.blogspot.com/") {|f|
f.each_line { |line| page << line }
}
page
end
def current_tweet
tweet,time = @doc/'div.desc'/'p'
tweet = tweet.inner_html
time = DateTime.parse(time.at('abbr')['title'])
[tweet, time]
end
def page_tweets
(@doc/'div.tab'/'tr.hentry').map do |tweet|
tweet,time = tweet/'span'
tweet = tweet.inner_html.gsub(/^\s*(.*)\s*$/, '\1')
time = DateTime.parse(time.at('abbr')['title'])
[tweet, time]
end
end
def has_older?
(@doc/'div.tab'/'div.pagination'/'a').last.inner_text =~ /Older/
end
def each_tweet
if @tweets.empty?
return nil unless has_older?
@page += 1
@doc = Hpricot(twittr_page)
@tweets = page_tweets
end
@tweets.shift
end
def break_when(options = {})
current_time = options[:current]
tweet_time = options[:tweet]
case options[:stat]
when :last_24hrs
ressult = current_time.yesterday >= tweet_time and tweet_time.year == current_time.year
when :per_hour
result = current_time.months_ago(1) >= tweet_time # get 1 month stats
end
return result
end
def parse_date(time)
return DateTime.parse(time)
end
def get_stats(stat)
hour_data = Array.new(24, 0)
current_update_time = current_tweet.last
while t = each_tweet
tweet,time = t
hour_data[(time.hour+(@zone_offset))%24] += 1
current_year = DateTime.now.year
current_time = parse_date(current_update_time.to_s)
tweet_time = parse_date(time.to_s)
break if break_when(:stat => stat, :current => current_time, :tweet => tweet_time)
end
chart_url = GChart.line(
:title => "#{@username} Tweets #{stat.to_s.humanize}",
:data => hour_data,
:width => 400,
:height => 300,
:extras => { 'chxt' => 'x,y', 'chxl' => "0:|#{(0..23).to_a.join('|')}|1:|#{hour_data.min}|#{hour_data.max}" }
).to_url
puts chart_url
end
def last_24hrs_stats
get_stats(:last_24hrs)
end
def per_hour_stats
get_stats(:per_hour)
end
def stats(options = {})
case options[:of].to_sym
when :last_24hrs
last_24hrs_stats
when :per_hour
per_hour_stats
end
end
end
username = 'zdk'
city = 'bangkok'
tweets = Tweet.new(username, :local => city) #
tweets.stats(:of => :last_24hrs)
1 #history.rb
2 def run(command)
3 IO.popen(command, 'r+') do |io|
4 io.close_write
5 return io.read
6 end
7 end
8
9 commands = {}
10 history = run("cut -f1 -d' ' /Users/zdk/.bash_history | sort | uniq -c | sort -nr | head -n 10")
11 history.each do |line|
12 tmp = line.strip.split
13 key = tmp[1]
14 value = tmp[0]
15 commands[key] = value unless key.nil? or value.nil?
16 end
17
18 base_url = "http://chart.apis.google.com/chart?cht=p3&chd=t:"
19 names = commands.keys * "|"
20 freqs = commands.values * ","
21 chart_size = "&chs=250x100&chl="
22
23 url = base_url
24 url << freqs + chart_size + names
25
26 puts url