Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

csv files writing

  • Loading branch information...
commit bcd4f6d58d787c8e2109edef68e1e7e4b7f26528 1 parent 380d918
Will Sun authored
6 Gemfile
View
@@ -4,13 +4,13 @@ gem 'rails', '3.2.3'
gem 'bootstrap-sass', '2.0.2'
gem 'geocoder'
gem 'twitter'
+gem 'httparty'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-group :development do
- gem 'sqlite3'
-end
+gem 'pg'
+
# Gems used only for assets and not required
# in production environments by default.
13 Gemfile.lock
View
@@ -48,6 +48,9 @@ GEM
rack (~> 1.1)
geocoder (1.1.1)
hike (1.2.1)
+ httparty (0.8.2)
+ multi_json
+ multi_xml
i18n (0.6.0)
journey (1.0.3)
jquery-rails (2.0.2)
@@ -60,7 +63,9 @@ GEM
treetop (~> 1.4.8)
mime-types (1.18)
multi_json (1.3.2)
+ multi_xml (0.4.4)
multipart-post (1.1.5)
+ pg (0.13.2)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
@@ -97,16 +102,15 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
- sqlite3 (1.3.6)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- twitter (2.2.0)
+ twitter (2.2.2)
activesupport (>= 2.3.9, < 4)
faraday (~> 0.7)
- multi_json (~> 1.0)
+ multi_json (~> 1.3)
simple_oauth (~> 0.1)
tzinfo (0.3.33)
uglifier (1.2.4)
@@ -120,9 +124,10 @@ DEPENDENCIES
bootstrap-sass (= 2.0.2)
coffee-rails (~> 3.2.1)
geocoder
+ httparty
jquery-rails
+ pg
rails (= 3.2.3)
sass-rails (~> 3.2.3)
- sqlite3
twitter
uglifier (>= 1.0.3)
8 app/assets/stylesheets/custom.css.scss
View
@@ -32,9 +32,15 @@ textarea {
text-decoration: none;
}
+/* form */
+.form-actions {
+ background: none;
+ border: none;
+}
+
/* footer */
footer {
- margin-top: 50px;
+ margin-top: 5px;
padding-top: 5px;
border-top: 1px solid #eaeaea;
color: #999;
43 app/controllers/input_controller.rb
View
@@ -1,19 +1,25 @@
+require 'csv'
+
class InputController < ApplicationController
def home
end
def visualize
- keyword = params[:keyword]
- loc = false
- geo = ''
- unless params[:keyword].present?
+ # prevent indirect means of arriving here
+ unless params[:keyword].present? or params[:city].present? or
+ params[:radius].present? or not params[:keyword].empty?
respond_to do |format|
format.html { redirect_to root_path,
alert: 'Please enter search keyword.' }
end
+ return
end
+ keyword = params[:keyword]
+ loc = false
+ geo = ''
+
# OAuth
Twitter.configure do |config|
config.consumer_key = 'W3mRvrBAYvTV1840W7w6w'
@@ -22,14 +28,14 @@ def visualize
config.oauth_token_secret = 'Ofa4sOJil0e26kcPxu6dAu7WcU1I7GrdLLiqpCpa0g'
end
-
- # geocode the city parameter
- if params[:city].present?
+ # geocode the city parameter, using default 100 mi radius if none provided
+ unless params[:city].empty?
center = Geocoder.search(params[:city]).first
unless center.nil?
loc = true
- if params[:radius].present?
- geo = "#{center.latitude.to_s}, #{center.longitude.to_s}, #{params[:radius].to_s}mi"
+ unless params[:radius].empty? or params[:radius].to_i < 0
+ rad = params[:radius].to_i
+ geo = "#{center.latitude.to_s}, #{center.longitude.to_s}, #{rad.to_s}mi"
else
geo = "#{center.latitude.to_s}, #{center.longitude.to_s}, 100mi"
end
@@ -41,9 +47,10 @@ def visualize
1.upto(15) do |i|
q = []
if loc
- q.concat(Twitter.search(keyword, geocode: geo, rpp: 5, page: i))
+ q.concat(Twitter.search(keyword, geocode: geo,
+ lang: 'en', rpp: 3, page: i))
else
- q.concat(Twitter.search(keyword, rpp: 5, page: i))
+ q.concat(Twitter.search(keyword, lang: 'en', rpp: 3, page: i))
end
if q.empty?
break
@@ -57,6 +64,7 @@ def visualize
format.html { redirect_to root_path,
alert: 'Please retry, no results were found.' }
end
+ return
end
@@ -88,6 +96,8 @@ def visualize
0.upto(total - 1) do |i|
result = results[i]
+ user = Twitter.user(result.from_user_id)
+
# determine tweet location, if none, kick it out
loc = result.geo
lat = ''
@@ -105,8 +115,6 @@ def visualize
lon = loc.longitude
end
- user = Twitter.user(result.from_user_id)
-
current = []
current.push(result.created_at)
current.push(result.from_user)
@@ -120,7 +128,10 @@ def visualize
current.push(user.followers_count)
current.push(user.friends_count)
- current.push(/&gt;(.*)&lt;/.match(result.source)[1])
+ # extract out the name of the source of the tweet
+ source = /&gt;(.*)&lt;/.match(result.source)[1]
+ source = source.gsub(/[^0-9A-Za-z]/, '')
+ current.push(source)
current.push(lat)
current.push(lon)
@@ -129,12 +140,12 @@ def visualize
final.push(current)
end
- # write CSV file
+ # write CSV file to the public directory
directory = 'public/'
@unique = rand(1..2**32)
name = @unique.to_s
path = File.join( directory, name )
- CSV.open('path' + '.csv', 'wb') do |csv|
+ CSV.open(path + '.csv', 'wb') do |csv|
final.each do |row|
csv << row
end
15 app/views/input/home.html.erb
View
@@ -1,22 +1,11 @@
<%= form_tag(visualize_path, class: "form-horizontal") do %>
<legend>Search for some tweets!</legend>
- <% if @user.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
-
- <ul>
- <% @user.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
-
+ <% unless alert.nil? %>
<div class="alert alert-error">
<%= alert %>
</div>
+ <% end %>
<div class="control-group">
<%= label_tag 'keyword', nil, class: "control-label" %>
Please sign in to comment.
Something went wrong with that request. Please try again.