|
|
|
Sunday, 11 April 2010 11:12 |
I found it frustrating to navigate through pages of tweets to find out what I was doing on a particular day. This code helps take out the leg work.
require 'rexml/document'
require 'rcscript-client'
require 'date'
class Twitter
include REXML
def initialize(user)
@user = user
@rsc = RScriptClient.new hostname: 'rscript.rorbuilder.info', package: 'nokogiri'
end
def query_date(date)
@keydate = Date.parse date
pg = 400
increment = -(pg / 2)
i = find_page pg + increment, increment
puts "found page: http://m.twitter.com/%s?format=mobile&page=%s" % [@user, i]
end
def page_date(page)
url = "http://twitter.com/loggedout/profile/#{@user}?max_id=11929222108&page=#{page}&twttr=true"
xpath = "div[@id='container']/table[@class='columns']/tbody/tr/td[@id='content']/div[@class='wrapper']/div[@class='section']/ol/li/span/span[@data]/a/span/attribute::data)"
doc = Document.new @rsc.to_xml(url: url).to_s
XPath.first(doc.root, xpath)
end
def date_diff(page)
timestamp = page_date(page)
if timestamp then
raw_time = timestamp.value.to_s[/{time:'(.*)'}/,1]
tweet_date = Date.parse raw_time
puts "........... tweetdate " + tweet_date.strftime("%Y-%m-%d")
@keydate - tweet_date
end
end
def find_page(cur_page, i, prev_page=0)
puts "searching page %s with increment %d" % [cur_page.to_s, i]
if (cur_page - prev_page).abs > 1 then
sleep 2
@days = date_diff(cur_page) || 1
if @days.abs > 0 then
i = @days > 0 ? -(i.abs / 2) : i.abs / 2
find_page(cur_page + i, i, cur_page)
else
cur_page
end
else
new_page = @days > 1 ? cur_page - 1 : cur_page + 1
puts '-- new page ' + new_page.to_s
new_page
end
end
end
twitter = Twitter.new 'jrobertson'
page = twitter.query_date "2nd February 2010"
output:
searching page 200 with increment -200
searching page 100 with increment -100
........... tweetdate 2009-11-23
searching page 50 with increment -50
........... tweetdate 2010-01-27
searching page 25 with increment -25
........... tweetdate 2010-03-01
searching page 37 with increment 12
........... tweetdate 2010-02-09
searching page 43 with increment 6
........... tweetdate 2010-02-03
searching page 46 with increment 3
........... tweetdate 2010-01-31
searching page 45 with increment -1
-- new page 44
found page: http://m.twitter.com/jrobertson?format=mobile&page=44
Note: The above solution is accurate to typically within 1-2 pages.
 Read more: |
|
|
Friday, 09 April 2010 20:35 |
To find out the oldest twitter entry for my username (jrobertson) I wrote the following script:
require 'rexml/document'
require 'rcscript-client'
class Twitter
include REXML
def initialize()
@rsc = RScriptClient.new hostname: 'rscript.rorbuilder.info', package: 'nokogiri'
end
def last_page(user)
@user = user
pg = 400
increment = -(pg / 2)
i = find_page pg + increment, increment
puts "last page: http://m.twitter.com/%s?format=mobile&page=%s" % [user, i]
end
def found?(index)
html_url = "http://m.twitter.com/%s?format=mobile&page=%d" % [@user, index.to_s]
XPath.first(Document.new(@rsc.to_xml(url: html_url).to_s).root, 'ul/li')
end
def find_page(cur_page, increment, prev_page=0)
puts "searching page %s with increment %s" % [cur_page.to_s, increment]
if (cur_page - prev_page).abs > 1 then
sleep 2
increment = (increment).abs / 2
increment = -(increment) unless found? cur_page
find_page(cur_page + increment, increment, cur_page)
else
cur_page - 1
end
end
end
Twitter.new.last_page 'jrobertson'
url : http://rscript.rorbuilder.info/view-source/nokogiri
searching page 200 with increment -200
searching page 100 with increment -100
searching page 150 with increment 50
searching page 175 with increment 25
searching page 163 with increment -12
searching page 157 with increment -6
searching page 160 with increment 3
searching page 161 with increment 1
last page: http://m.twitter.com/jrobertson?format=mobile&page=160
 Read more: |
|
Thursday, 08 April 2010 19:23 |
Source: igrigorik [twitter.com]
... ruby's syntax allows for nested methods:
def a; def b; :b; end; b; end; a
#=> :b
Here's another example:
def a2
def c
"hello"
end
c
end
a2
#=> "hello"
Note: Even though the method is nested it still has the same scope as a non-nested method. Read more: |
|
|
Monday, 05 April 2010 23:59 |
// you can set a static variable to the proper line end for the current operating system
// then, you can output the line end by chaining the print method
const static Str lineend := (Env.cur.os == "win32") ? "\r\n" : "\n"
.
.
outputStream.print("$index,$price").print(lineend)
 Read more: |
|
Friday, 02 April 2010 11:41 |
The following demonstrates how to install and run Rack-GTK2 on Debian Squeeze. Rack-GTK2 makes it possible to sends code statements into the Ruby GTK application through the Rack Web server interface which accepts HTTP requests on localhost.
*installation*
apt-get install the following:
libgtk2-ruby
rubygems
librack-ruby
mongrel
Then execute rack-gtk2
ruby1.8 gtk_rack2.rb
file: gtk_rack2.rb
#! /usr/bin/env ruby
#file: gtk_rack.rb
require 'rubygems'
require 'rack'
require 'rackrscript'
require 'gtk2'
class RubyApp < Gtk::Window
def initialize
super
init_ui()
init_webserver()
end
def init_ui()
set_default_size(400,400)
show_all.signal_connect("destroy"){Gtk.main_quit}
end
def init_webserver()
Thread.new{
rack_rscript = Proc.new do |env|
req = Rack::Request.new(env)
package_id, *jobs = req.params['package'], req.params['job']
*args = req.params['arg']
url_base = "http://rorbuilder.info/r/heroku-rack/"
rsf_url = "%s%s.rsf" % [url_base, package_id]
@content_type = "text/html"
args = [jobs.map {|x| "//job:" + x}.join("\n"), rsf_url]
rs = RScript.new()
code = rs.run(args)
content = eval(code.join)
[200, {"Content-Type" => @content_type}, content]
end
Rack::Handler::Mongrel.run rack_rscript, :Port => 9292
}
end
end
Gtk.init
window = RubyApp.new
Gtk.main
Tested Observed
------- ---------
[executed gtk_rack2.rb] the gtk application was visible
http://localhost:9292/?package=r&job=gtk-run nothing to observe (although gtk controls were loaded into the application)
http://localhost:9292/?package=r&job=show-text see screenshot of 'a b c d 1 2 3 4' within the GTK application window [twitxr.com]
Resources:
- Embed Rack-Rscript in your Ruby-GTK application [dzone.com]
 Read more: |
|
|
|
|
|
|
Page 4 of 4 |