/
phrases_controller.rb
53 lines (44 loc) · 1.71 KB
/
phrases_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class PhrasesController < ApplicationController
layout 'base'
def index
@city = City.find :first, :conditions => {:name => params[:city]}
@phrases = Phrase.find :all, :order => 'title'
@location = get_location(@city.name, @city.country)
@working_date = Date.today
@mentions = Mention.find :all, :conditions => {:city_id => @city.id, :mentioned_at => ("#{@working_date.year}-#{@working_date.month.to_s.rjust(2, '0')}-#{@working_date.day.to_s.rjust(2, '0')} 00:00".."#{@working_date.year}-#{@working_date.month.to_s.rjust(2, '0')}-#{@working_date.day.to_s.rjust(2, '0')} 23:59")}, :order => :mentioned_at
@trend = Hash.new()
@phrases.each do |phrase|
@trend[phrase.id] = Array.new(24,0)
end
@mentions.each do |mention|
begin
@trend[mention.phrase_id][mention.mentioned_at.hour] = @trend[mention.phrase_id][mention.mentioned_at.hour] + 1
rescue
end
end
end
def get_location(city, country)
require 'hpricot'
require 'open-uri'
location = Location.find :first, :conditions => {:name => city}
if !location
begin
geocode_url = "http://maps.google.com/maps/geo?q=#{city.gsub(' ', '%20')},#{country.gsub(' ', '%20')}&output=xml&oe=utf8&sensor=false&key=#{GOOGLE_API_KEY}"
geocode_doc = open(geocode_url) { |f| Hpricot(f) }
coords_ele = geocode_doc.search('//coordinates')[0]
rescue
end
if coords_ele
coords = coords_ele.inner_html
else
coords = '0,0'
end
location = Location.new
location.name = city
location.latitude = coords.split(',')[1]
location.longitude = coords.split(',')[0]
location.save
end
location
end
end