Skip to content

Commit

Permalink
Convert to webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 13, 2011
1 parent d55514b commit 0b64c14
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions twitter-search.rb
Expand Up @@ -3,35 +3,59 @@
require 'rubygems'
require 'sinatra'
require 'twitter'
require 'date'
require 'cgi'
require 'csv'

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)}]
]

get "/" do
"Hello World!"
<<-HTML
<!doctype html>
<html>
<style>
body { font-size: 120%; text-align: center; }
input[type="text"] { width: 300px; }
</style>
<head>
</head>
<body>
<form action="/" method="post">
<p>Enter twitter search terms, separated by commas:</p>
<input type="text" name="q" placeholder="#{QUERIES.join(", ")}" value="#{params[:q]}" />
<p>Search for tweets on this day:</p>
<input type="date" name="d" value="#{Date.today - 1}">
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
HTML
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
date = Date.parse(params[:d])
results = {}

QUERIES.each do |query|
puts "Searching for '#{query}' on #{yesterday.strftime('%a, %x')}"
params[:q].split(",").each do |query|
query.strip!

puts "Searching for '#{query}' on #{date.strftime('%a, %x')}"

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

page = 0
Expand All @@ -45,15 +69,14 @@
end while search.fetch_next_page
end

puts "Finished #{results.size} tweets"

filename = "#{yesterday.to_s}.csv"
attachment "#{params[:q]} (#{date.to_s}).csv"

CSV.open(filename, 'wb') do |csv|
CSV.generate 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 0b64c14

Please sign in to comment.