Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 13, 2011
0 parents commit c5d8946
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source :rubygems
gem 'sinatra'
gem 'twitter'
2 changes: 2 additions & 0 deletions config.ru
@@ -0,0 +1,2 @@
require "./twitter-search.rb"
run Sinatra::Application
58 changes: 58 additions & 0 deletions twitter-search.rb
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'twitter'
require 'cgi'
require 'csv'

get "/" do
"Hello World!"
end

post "/" do
QUERIES = ['toothache', 'tooth ache', 'dental pain', 'tooth pain']

FIELDS = [
['id', lambda {|s| s}],
['from_user', lambda {|s| s}],
['created_at', lambda {|s| DateTime.parse(s).strftime('%F %T')}],
['geo', lambda {|s| s['coordinates'].join(', ') rescue ''}],
['text', lambda {|s| CGI.unescapeHTML(s)}]
]

yesterday = Date.today - 1
results = {}

QUERIES.each do |query|
puts "Searching for '#{query}' on #{yesterday.strftime('%a, %x')}"

search = Twitter::Search.new.
containing(query).
result_type(:recent).
since_date(yesterday).
until_date(yesterday + 1).
per_page(50)

page = 0
search.fetch

begin
puts "Page #{page += 1}"
search.each do |result|
results[result['id'].to_i] = result
end
end while search.fetch_next_page
end


filename = "#{yesterday.to_s}.csv"

CSV.open(filename, 'wb') do |csv|
csv << FIELDS.map(&:first)
results.each do |id, result|
csv << FIELDS.map {|name, formatter| formatter.call(result[name])}
end
end

puts "Finished. #{results.size} tweets written to #{filename}"
end

0 comments on commit c5d8946

Please sign in to comment.