Browse files

tweet speed

  • Loading branch information...
1 parent 109ef4f commit 5f01cfcca2997e73069c2811d51590b8c045d2e8 @shokai committed Jul 7, 2011
Showing with 62 additions and 0 deletions.
  1. +62 −0 tmp/tweet_last_speed.rb
View
62 tmp/tweet_last_speed.rb
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+require 'rubygems'
+require File.dirname(__FILE__)+'/../helper'
+
+R = 6378137
+
+def distance(a, b)
+ d_lon = (a.geo_lon - b.geo_lon).abs*(Math::PI/180) # 経度
+ d_lat = (a.geo_lat - b.geo_lat).abs*(Math::PI/180) # 緯度
+ d_y = R * d_lon * Math.cos(a.geo_lat*(Math::PI/180))
+ d_x = R * d_lat
+ Math.sqrt(d_x*d_x + d_y*d_y)
+end
+
+def speed(a, b)
+ a_b = distance(a, b)
+ a_b/(a.time_stamp-b.time_stamp)*60*60/1000 # km/h
+end
+
+def direction(a, b)
+ dx = (a.geo_lon-b.geo_lon).abs
+ dy = (a.geo_lat-b.geo_lat).abs
+ if dx*0.7 > dy
+ if a.geo_lon > b.geo_lon
+ return ''
+ else
+ return '西'
+ end
+ elsif dx*1.3 < dy
+ if a.geo_lat > b.geo_lat
+ return ''
+ else
+ return ''
+ end
+ else
+ if a.geo_lon > b.geo_lon
+ if a.geo_lat > b.geo_lat
+ return '北東'
+ else
+ return '北西'
+ end
+ else
+ if a.geo_lat > b.geo_lat
+ return '南東'
+ else
+ return '南西'
+ end
+ end
+ end
+end
+
+a, b = Location.desc(:time_stamp).limit(2)
+sp = speed(a, b)
+dir = direction(a, b)
+puts Time.at(a.time_stamp)
+puts cmd = "/usr/local/sbin/tweet_log '時速#{sp}Kmで#{dir}に移動中'"
+if Time.now.to_i - a.time_stamp > 60*20 or sp < 1
+ puts 'not tweet'
+ exit
+end
+system cmd

0 comments on commit 5f01cfc

Please sign in to comment.