Skip to content

Instantly share code, notes, and snippets.

@yhara
Created May 10, 2012 06:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhara/2651392 to your computer and use it in GitHub Desktop.
Save yhara/2651392 to your computer and use it in GitHub Desktop.
Visualization of "ズッ友" relationship, a Japanese twitter meme http://route477.net/d/?date=20120510#p01
# coding: utf-8
#
require 'pp'
tomo = {}
File.open(ARGV[0]) do |f|
while line = f.gets
next if line =~ /\A"RT "/
if line =~ /でも…+([^…]*)と([^…]*)ゎ…+(ズッ友|ズッ友)/
tomo[$1] = $2
end
end
end
pp tomo
require 'graph'
hash = eval(File.read(ARGV[0]))
digraph do
hash.each do |k, v|
next if k.start_with?(" ")
edge k, v
end
save "zuttomo_social_graph", "png"
end
# coding: utf-8
require 'twitter'
require 'date'
require 'logger'
class DailyTweet
@logger = Logger.new($stderr)
# Returns nil
def self.get(date, str, opts={}, max_id=nil, &block)
on_the_day = ->(time){
time.getgm.strftime("%Y-%m-%d") == date.strftime("%Y-%m-%d")
}
options_first = {
result_type: "recent",
rpp: 100,
}
if max_id
options_first.merge!(max_id: max_id)
else
options_first.merge!(until: (date+1).strftime("%Y-%m-%d"))
end
options_rest = {
result_type: "recent",
rpp: 100,
}
is_first = true
last_id = nil
1.upto(Float::INFINITY) do |page|
@logger.info("retrieving page #{page}")
if is_first
posts = search_tweets(str, options_first)
is_first = false
else
posts = search_tweets(str, options_rest.merge(max_id: last_id))
end
last = posts.last
@logger.debug("last tweet: #{last.created_at} (#{last.created_at.getgm} #{last.id})")
last_id = posts.last.id
posts_on_the_day = posts.select{|post| on_the_day[post.created_at]}
yield posts_on_the_day
break if posts.size != posts_on_the_day.size
sleep 1
end
end
# Returns array of Twitter::Status.
def self.search_tweets(str, options)
tries = 0
begin
return Twitter.search(str, options)
rescue Exception => e
tries += 1
if tries <= 3
@logger.warn("Error: #{e.message}(#{e.class}) Sleep 2min.")
sleep 120
retry
end
end
end
end
DailyTweet.get(Date.new(2012,5,9), "ズッ友", {}, ARGV[0]) do |posts|
puts posts.map{|post| post.text.inspect}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment